Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
ainsleybilton
Regular Visitor

Find locations within a given distance

Hi All, this is my first post, so please be gentle with me.

 

I have a requirement to find locations within a fixed distance from a provided location.

 

My data model is as follows:

 

 

Capture.PNG

 

 

 

 

 

 

 

 

 

 

 

 

 

I am trying to achieve the following :-

  1. Select a 'Site' filter value
  2. Specify a 'Distance' value (parameter at the moment)
  3. Find employees within the specified distance of the site

Can anyone help with this please? I am really stuck.

 

1 ACCEPTED SOLUTION
v-lili6-msft
Community Support
Community Support

hi, @ainsleybilton 

You could try this way:

Step1:

Create a duplicate locations table as targe locations table.

and in the targe locations table, add a Site Name column
target locations = locations
target site name = LOOKUPVALUE(Sites[Site Name],Sites[ID],'target locations'[ID])
Step2:
Create the relationship as below:
2.JPG
Note: be careful the red marker.
Step3:
Use What if parameter to create a 'Distance' value
Step4:
Create a distance betweem two city measure
Kilometers = 
var Lat1 = MIN('locations'[lat])
var Lng1 = MIN('locations'[lng])

var Lat2 = MIN('target locations'[lat])
var Lng2 = MIN('target locations'[lng])
---- Algorithm here -----
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))) 
return final
Step5:
Then use 'Distance' value (parameter at the moment) to find employees within the specified distance of the site
Amount = IF([Kilometers]<=[Distance Value],CALCULATE(COUNTA(employees[Employee Name]),ALL(Sites)))
Step6:
Drag target site name field from targe locations table and these two measure in a table visual, use Site Name from Sites table as Select a 'Site' filter value.
Result:
3.JPG
and this is a similar blog for you refer to:
Here is a sample pbix file, please try it.
 
Best Regards,
Lin
 
 
Community Support Team _ Lin
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

3 REPLIES 3
v-lili6-msft
Community Support
Community Support

hi, @ainsleybilton 

You could try this way:

Step1:

Create a duplicate locations table as targe locations table.

and in the targe locations table, add a Site Name column
target locations = locations
target site name = LOOKUPVALUE(Sites[Site Name],Sites[ID],'target locations'[ID])
Step2:
Create the relationship as below:
2.JPG
Note: be careful the red marker.
Step3:
Use What if parameter to create a 'Distance' value
Step4:
Create a distance betweem two city measure
Kilometers = 
var Lat1 = MIN('locations'[lat])
var Lng1 = MIN('locations'[lng])

var Lat2 = MIN('target locations'[lat])
var Lng2 = MIN('target locations'[lng])
---- Algorithm here -----
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))) 
return final
Step5:
Then use 'Distance' value (parameter at the moment) to find employees within the specified distance of the site
Amount = IF([Kilometers]<=[Distance Value],CALCULATE(COUNTA(employees[Employee Name]),ALL(Sites)))
Step6:
Drag target site name field from targe locations table and these two measure in a table visual, use Site Name from Sites table as Select a 'Site' filter value.
Result:
3.JPG
and this is a similar blog for you refer to:
Here is a sample pbix file, please try it.
 
Best Regards,
Lin
 
 
Community Support Team _ Lin
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

@v-lili6-msftthanks for your help. You are a star, this worked a treat.

 

🙂

ainsleybilton
Regular Visitor

As an additional bit of information, I tried using a calculated table to get the distance between the locations as below:

 

DistanceFromSites =
SELECTCOLUMNS (
    ADDCOLUMNS (
        GENERATE (
            SELECTCOLUMNS (
                FILTER(Locations,Locations[Location Type]="Site Location"),
                "Site ID", 'Locations'[Location ID],
                "Latitude From", 'Locations'[Latitude],
                "Longitude From", 'Locations'[Longitude]
            ),
            SELECTCOLUMNS (
                FILTER(Locations,Locations[Location Type]="Employee Location"),
                "Employee ID", 'Locations'[Location ID],
                "Latitude To", 'Locations'[Latitude],
                "Longitude To", 'Locations'[Longitude]
            )
        ),
        "Distance",
        VAR Pie =
            DIVIDE ( PI (), 180 )
        VAR Arc =
            0.5
                - COS ( ( [Latitude To] - [Latitude From] ) * Pie )
                    / 2
                + COS ( [Latitude From] * Pie )
                    * COS ( [Latitude To] * Pie )
                    * (
                        1
- COS ( ( [Longitude To] - [Longitude From] ) * Pie )
                    )
                    / 2
        VAR KMDistance =
            12742 * ASIN ( SQRT ( Arc ) )
        RETURN
            KMDistance
    ),
    "Site", [Site ID],
    "Employee", [Employee ID],
    "Distance KM", [Distance]
)
 
The above resulted in 11M records in my table which broke my models ability to filter in the correct direction.
 
I would dearly like the solution to calculate the distance from the selected SITE only when the filter is applied and to do it against all of the rows within the Locations table. I really don't want to have to calculate the distance between all possible locations just to use the distance filter.
 
I really hope you guys can help.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.