It’s the final week of mapping month! This week we are plotting points on a map within a selected radius. It’s common to want to see locations, such as stores or restaurants that are nearby to a selected location. This week we are exploring one way to do that in Power BI. This involves a fun bit of DAX and some conditional formatting.
This week we are mapping Dutch Bros Coffee locations. This is a coffee chain based in Oregon, USA that has locations in about 13 states throughout the country. I enjoy their cold brew, but they have many hot, cold, and frozen drink options.
- Retrieve the Dutch Bros location data and the list of cities from data.world.
- Create a map visual that plots the Dutch Bros locations using longitude and latitude.
- Use the “CityLatLong.xlsx” table to populate state and city slicers.
- Create a DAX calculation that uses the haversine formula to calculate distance from the selected city in the slicers to each Dutch Bros location. (I chose to use kilometers, but feel free to do miles if you prefer.)
- Create a slicer to allow users to filter on distance from store locations to the selected city.
- Add a visual (I used a table) to list the store addresses and the distance from the selected city.
- Configure the map and table so they only show locations within the selected distance radius. (Hint1, Hint2)
This challenge uses data provided on data.world. You can use the data.world connector in Power BI (recommended) or download the Excel file to your local machine. You will need to log in to data.world to retrieve the data, but accounts are free.
17 thoughts on “2021 Week 43 | Power BI: Maps with a Distance Radius”
How would you change the distance formula to use miles instead of kilometer?
Hi, John. You can change the radius to 3956. This makes the units miles instead of kilometers.
Hi, just wanted to check if I can find distance of coffee shops from a particular location, which I can keep changing. Say distance from a Metro or Distance from a particular house number/Lat-Long or particular shop
Hi. Yes, that is possible. The challenge is that you have to provide lat and long for the source and destination. I provided the lat and long of the cities in my source data. It would be much trickier if you needed to do that on the fly (since you wouldn’t have pre-populated lat and long for your source), but maybe it could be done using a Power App embedded in the report. If you just want to show distance from a metro area, you just have to add the metro area to your dataset and assign it a lat and long value.
Is it possible to make a search radius for PowerApps and filter locations? Would be nice to hear from you. Thnaks in advance.
Are you asking if you can embed a Power BI visual in a Power App and then use the location from a device as retrieved from the app to filter locations? You could probably do that. You would need to adjust how the location is input into the map.
What if I want to apply the filter on the distance between the same coffeeshops, that is, first select the coffeeshop that I´m interested in and then get the nearest coffeeshops
The formula for calculating distance is the same either way, just change the inputs for your measure.
Hu excellent video, easy to follow. However, try as I might I cannot get the distance calculation to work properly, I must be doing something wrong in the calculation (below) but I cannot see what, any help appreciated
sDistance (KM) =
var LatPl = min(Planners[latitude])
var LngPl = min(Planners[longitude])
var LatSite = min(Sites[latitude])
var LngSite = min(Sites[longitude])
var P = DIVIDE(PI(),180)
var A = 0.5 * COS((LatSite – LatPl) * P)/2 +
COS(LatPl * P) * COS(LatSite * P) * (1-COS((LngSite-LngPl) * P))/2
var final = 12742 * ASIN((SQRT(A)))
It looks correct to me after a quick glance. Can you explain the result you are getting vs what you expected?
Permission helps that the formula you entered is wrong. It should be “0.5 – COS” not “0.5 * COS”.
Before creating the visualization do we have to create any relationship between the tables in the model?
Nope. There are no relationships in the model. Feel free to check the solution video for more details.
Hi I might have missed this in the tutorrial but can i also include a point on the map that highlights the Lat/Long of the selected city(in a different colour to the coffee shops?
THank you for all your help
That’s not addressed in the tutorial. The lat/long of the city is an arbitrary point within the city, based upon the dataset used. So the city isn’t actually a data point that gets plotted on the map. You could probably add it and use conditional formatting to change the color.
Hello, thank you for this awesome video. However, I am having trouble with my measure…
var Lat1 = min(‘City'[Latitude Decimal])
var Lng1 = min(‘City'[Longitude Decimal])
var Lat2 = min(‘Location of Shop'[Latitude Decimal])
var Lng2 = min(‘Location of Shop'[Longitude Decimal])
var P = DIVIDE (PI(),180)
var A = 0.5 * COS((Lat2-Lat1) * P)/2 +
COS(Lat1 * P) * COS(Lat2 * P) * (1-COS((Lng2-Lng1) * P))/2
var Final = 12742 * ASIN((SQRT(A)))
It comes up with an error saying the argument ‘ASIN’ has the wrong data type or the result is too large or too small. Am I missing something?
Note…when I write the measure, it does not show any errors. It is only when I put it in a visual that I get the error.
Check that the data type of your Lat and long fields are decimal. You can also return the output of var A and make sure the value is what you expect.