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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
Postman848
New Member

Closest point for each point in a same table based on Lat and Long

Hi,

 

I would like to calculate the distance to the closest point for each point in my dataset based on Lat and Long. 

My data (example below) has three columns Name, Latitude and Longitude. I would like to add 4th column "Distance to the closest point".

The closest point of course cannot be the distance to the point itself (=0km)

NameLatitudeLongitudeDistance to the closest point

A

62.171628643.865615 
B62.215908943.7522112 
C62.20983143.7682273 
D62.168262443.8669002 
E64.00230943.8041308 
F65.167866144.3022492 
G65.586969243.6146272 

 

All the help is more than welcome.

Thanks in advance!

1 ACCEPTED SOLUTION
parry2k
Super User
Super User

@Postman848 try this, if you want to add a shortest distance as a column

 

Shortest Distance as Column = 
VAR __baseLat = 'Table'[Latitude]
VAR __baseLng = 'Table'[Longitude] 
VAR __distance = 
ADDCOLUMNS ( 
    ALLSELECTED ( 'Table' ), 
    "Distance",
    var Lat1 = __baseLat
    var Lng1 = __baseLng
    var Lat2 = 'Table'[Latitude]
    var Lng2 = 'Table'[Longitude]
    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
)
return 
MINX ( __distance, IF ( [Distance] > 0, [Distance] ) )


Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

View solution in original post

9 REPLIES 9
parry2k
Super User
Super User

@Postman848 tweak this code:

 

//change following line to whatever you feel file
ALLSELECTED ( 'Table' ), 

FILTER ( ALLSELECTED ( 'Table' ), 'Table'[Type] = "Your Value" ) 

 

Follow us on LinkedIn

 

Learn about conditional formatting at Microsoft Reactor

My latest blog post The Power of Using Calculation Groups with Inactive Relationships (Part 1) (perytus.com) I would  Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make efforts to give Kudos to whoever helped to solve your problem. It is a token of appreciation!

 

Visit us at https://perytus.com, your one-stop-shop for Power BI-related projects/training/consultancy.



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

parry2k
Super User
Super User

@Postman848 and this if you want it as a measure and I prefer measures but will leave it up to you

 

Measure 2 = 
VAR __baseLat = MIN ( 'Table'[Latitude] )
VAR __baseLng = MIN ( 'Table'[Longitude] )
VAR __distance = 
ADDCOLUMNS ( 
    ALLSELECTED ('Table' ), 
    "Distance",
    var Lat1 = __baseLat
    var Lng1 = __baseLng
    var Lat2 = 'Table'[Latitude]
    var Lng2 = 'Table'[Longitude]
    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
)
return MINX ( __distance, IF ( [Distance] > 0, [Distance] ) )

 

Follow us on LinkedIn

 

Learn about conditional formatting at Microsoft Reactor

My latest blog post The Power of Using Calculation Groups with Inactive Relationships (Part 1) (perytus.com) I would  Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make efforts to give Kudos to whoever helped to solve your problem. It is a token of appreciation!

 

Visit us at https://perytus.com, your one-stop-shop for Power BI-related projects/training/consultancy.



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

parry2k
Super User
Super User

@Postman848 try this, if you want to add a shortest distance as a column

 

Shortest Distance as Column = 
VAR __baseLat = 'Table'[Latitude]
VAR __baseLng = 'Table'[Longitude] 
VAR __distance = 
ADDCOLUMNS ( 
    ALLSELECTED ( 'Table' ), 
    "Distance",
    var Lat1 = __baseLat
    var Lng1 = __baseLng
    var Lat2 = 'Table'[Latitude]
    var Lng2 = 'Table'[Longitude]
    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
)
return 
MINX ( __distance, IF ( [Distance] > 0, [Distance] ) )


Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Thanks for the solution!! Works very nicely! I am actually having one extra column that is telling the type of the location. Is there easy way to modify code to calculate the distance to the nearest of certain type?

parry2k
Super User
Super User

@Postman848 does this looks correct:

 

parry2k_0-1640023110891.png

 

 

Follow us on LinkedIn

 

Learn about conditional formatting at Microsoft Reactor

My latest blog post The Power of Using Calculation Groups with Inactive Relationships (Part 1) (perytus.com) I would  Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make efforts to give Kudos to whoever helped to solve your problem. It is a token of appreciation!

 

Visit us at https://perytus.com, your one-stop-shop for Power BI-related projects/training/consultancy.



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

I guess so. Data values in the table are modified from the original dataset. 

Could you share your solution?

parry2k
Super User
Super User

@Postman848 that's pretty small, I was thinking in millions. Anyhow, jumping on a few meetings, if someone doesn't provide a solution in the next few hours I will be on it. Hang tight.

 

 



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

parry2k
Super User
Super User

@Postman848 I have a solution in mind but before I post the solution, can you tell me how large is your dataset? Want to make sure the solution performs well.

 

 



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Looking for your solution eagerly. Dataset is a bit over 1000 rows. Thus, not a massive set. 

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Prices go up Feb. 11th.

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

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