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

Level up your Power BI skills this month - build one visual each week and tell better stories with data! Get started

Reply
PSB
Helper III
Helper III

Help calculate distance using DAX Query (using math formula)

I need help calculate distance using lat and long information.

Using math formula shown below (its' tested and working but when source and target lat and long information is in same table)

In this scenario, one table I have Source and target and need distance between them and the lat and long values needs to be looked up from another table.

Any help is appreciated. Thanks. 

 

 

FromToDistance (Miles)
BerlinTokyo?
MumbaiNew York?
LondonParis?

 

CityLatLong
Berlin39.58305-74.78388977
Mumbai39.55434-74.74279785
London39.51902-74.69287872
Tokyo39.48972-74.59777832
New York39.45287-74.63844299
Paris39.43-74.57861328

------------------------------------------

Sample

-------------------------------------------

Distance (Miles) =
var Lat1 = MIN(SA_to_SA_Market_Cell_Relation_Counters_Copy[S_Lat])
var Lng1 = MIN(SA_to_SA_Market_Cell_Relation_Counters_Copy[S_Long])

var Lat2 = MIN(SA_to_SA_Market_Cell_Relation_Counters_Copy[T_Lat])
var Lng2 = MIN(SA_to_SA_Market_Cell_Relation_Counters_Copy[T_Long])
---- 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 = 7922 * ASIN((SQRT(A))) --2*r (12742 for km
return final

 

1 ACCEPTED SOLUTION
lbendlin
Super User
Super User

lbendlin_0-1676774619991.png

Distance = 
var Lat1=LOOKUPVALUE(Locations[Lat],Locations[City],SELECTEDVALUE(Distances[From]))
var Lng1=LOOKUPVALUE(Locations[Long],Locations[City],SELECTEDVALUE(Distances[From]))
var Lat2 =LOOKUPVALUE(Locations[Lat],Locations[City],SELECTEDVALUE(Distances[To]))
var Lng2 =LOOKUPVALUE(Locations[Long],Locations[City],SELECTEDVALUE(Distances[To]))
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
return 12742 * ASIN((SQRT(A)))

 

see attached.

View solution in original post

2 REPLIES 2
lbendlin
Super User
Super User

lbendlin_0-1676774619991.png

Distance = 
var Lat1=LOOKUPVALUE(Locations[Lat],Locations[City],SELECTEDVALUE(Distances[From]))
var Lng1=LOOKUPVALUE(Locations[Long],Locations[City],SELECTEDVALUE(Distances[From]))
var Lat2 =LOOKUPVALUE(Locations[Lat],Locations[City],SELECTEDVALUE(Distances[To]))
var Lng2 =LOOKUPVALUE(Locations[Long],Locations[City],SELECTEDVALUE(Distances[To]))
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
return 12742 * ASIN((SQRT(A)))

 

see attached.

Thanks! you're the best.

Helpful resources

Announcements
April Power BI Update Carousel

Power BI Monthly Update - April 2026

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

Fabric SQL PBI Data Days

Data Days 2026 coming soon!

Sign up to receive a private message when registration opens and key events begin.

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.