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

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

Reply
Paulus
Frequent Visitor

bearing between 2 latitude longitude sets

Hi,

I need to calculate the bearing in degrees between two sets of latitude /longitude points in a DAX formula. I can do this in Power Query, but need it in DAX.

Thank you.

Paul

1 ACCEPTED SOLUTION

I believe this is it. Attached PBIX matches with my Excel file, will post both. 

 

Bearing (Degrees) = 
    VAR __FromCity = SELECTEDVALUE('From City'[City])
    VAR __ToCity = SELECTEDVALUE('To City'[City])
    VAR __FromLat = RADIANS(LOOKUPVALUE('Table'[Latitude],'Table'[City],__FromCity))
    VAR __ToLat = RADIANS(LOOKUPVALUE('Table'[Latitude],'Table'[City],__ToCity))
    VAR __FromLong = RADIANS(LOOKUPVALUE('Table'[Longitude],'Table'[City],__FromCity))
    VAR __ToLong = RADIANS(LOOKUPVALUE('Table'[Longitude],'Table'[City],__ToCity))
    VAR __distanceLong = (__ToLong - __FromLong)
    VAR __y = COS(__ToLat) * SIN(__distanceLong)
    VAR __x = COS(__FromLat) * SIN(__ToLat) - SIN(__FromLat) * COS(__ToLat) * COS(__distanceLong)
    VAR __atan2 = 
        SWITCH(
            TRUE(),
            __x > 0, ATAN(__y/__x),
            __x < 0 && __y >= 0, ATAN(__y/__x) + PI(),
            __x < 0 && __y < 0, ATAN(__y/__x) - PI(),
            __x = 0 && __y > 0, PI()/2,
            __x = 0 && __y < 0, PI()/2 * (0-1),
            BLANK()
        )
RETURN
    DEGREES(__atan2)

 

Bearing 2 = MOD([Bearing (Degrees)]+360,360)

 

 


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

12 REPLIES 12
amitchandak
Super User
Super User

Thank you, indeed this is for distance, and it works. Bearing seems another matter, surprisingly little mentioned about it in DAX.

@Paulus  - How is this? PBIX is attached. Please confirm you get the right answers for your data.

 

 

b = 
    VAR __FromCity = SELECTEDVALUE('From City'[City])
    VAR __ToCity = SELECTEDVALUE('To City'[City])
    VAR __FromLat = LOOKUPVALUE('Table'[Latitude],'Table'[City],__FromCity)
    VAR __ToLat = LOOKUPVALUE('Table'[Latitude],'Table'[City],__ToCity)
    VAR __FromLong = LOOKUPVALUE('Table'[Longitude],'Table'[City],__FromCity)
    VAR __ToLong = LOOKUPVALUE('Table'[Longitude],'Table'[City],__ToCity)
    VAR __distanceLong = RADIANS(ABS(__ToLong - __FromLong))
    VAR __x = COS(RADIANS(__ToLat)) * SIN(__distanceLong)
    VAR __y = COS(RADIANS(__FromLat)) * SIN(RADIANS(__ToLat)) - SIN(RADIANS(__FromLat)) * COS(RADIANS(__ToLat)) * COS(__distanceLong)
    VAR __atan2 = 
        SWITCH(
            TRUE(),
            __x > 0, ATAN(__y/__x),
            __x < 0 && __y >= 0, ATAN(__y/__x) + PI(),
            __x < 0 && __y < 0, ATAN(__y/__x) - PI(),
            __x = 0 && __y > 0, PI()/2,
            __x = 0 && __y < 0, PI()/2 * (0-1),
            BLANK()
        )
RETURN
    DEGREES(__atan2)

 

 


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Hi Greg,

Thanks.

Not quite yet, the result between Kansas and St.Louis returns -6,5126.....  it should be 97 degrees.

Paul

I believe this is it. Attached PBIX matches with my Excel file, will post both. 

 

Bearing (Degrees) = 
    VAR __FromCity = SELECTEDVALUE('From City'[City])
    VAR __ToCity = SELECTEDVALUE('To City'[City])
    VAR __FromLat = RADIANS(LOOKUPVALUE('Table'[Latitude],'Table'[City],__FromCity))
    VAR __ToLat = RADIANS(LOOKUPVALUE('Table'[Latitude],'Table'[City],__ToCity))
    VAR __FromLong = RADIANS(LOOKUPVALUE('Table'[Longitude],'Table'[City],__FromCity))
    VAR __ToLong = RADIANS(LOOKUPVALUE('Table'[Longitude],'Table'[City],__ToCity))
    VAR __distanceLong = (__ToLong - __FromLong)
    VAR __y = COS(__ToLat) * SIN(__distanceLong)
    VAR __x = COS(__FromLat) * SIN(__ToLat) - SIN(__FromLat) * COS(__ToLat) * COS(__distanceLong)
    VAR __atan2 = 
        SWITCH(
            TRUE(),
            __x > 0, ATAN(__y/__x),
            __x < 0 && __y >= 0, ATAN(__y/__x) + PI(),
            __x < 0 && __y < 0, ATAN(__y/__x) - PI(),
            __x = 0 && __y > 0, PI()/2,
            __x = 0 && __y < 0, PI()/2 * (0-1),
            BLANK()
        )
RETURN
    DEGREES(__atan2)

 

Bearing 2 = MOD([Bearing (Degrees)]+360,360)

 

 


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Great!, Thanks. I have been struggling with this for quite a while, digging as hole for myself, not being in my comfort zone.

Briefly why I need it; deliveries to droppoints, many of them available, service engineer on his way to work, in his allocated working area, needs to collect spare parts from a droppoint. The nearest droppoint may be say 25 miles but in southern direction, whereas his allocated working area is in the North. I convert degrees into something like 0-22,5 = N 22,6-66,5= NE etc. 

Paul

Cool @Paulus, glad we got it, I'll post the solution to the Quick Measures gallery for future reference for everyone.

Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...
az38
Community Champion
Community Champion

Hi @Paulus 

Im not sure what do you mean exactly but try a solution by @Greg_Deckler  https://community.powerbi.com/t5/Quick-Measures-Gallery/Going-the-Distance/td-p/963267


do not hesitate to give a kudo to useful posts and mark solutions as solution
LinkedIn
Greg_Deckler
Super User
Super User

Sure, what is the formula for that? Should be doable. I have one for distance. https://community.powerbi.com/t5/Quick-Measures-Gallery/Going-the-Distance/td-p/963267

 


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Hi Greg,
In Excel
=ATAN2(COS(G5)*SIN(I5)-SIN(G5)*COS(I5)*COS(J5-H5); SIN(J5-H5)*COS(I5)), where
G5=latitude 1, H5=longitude 1, I5=latitude 2, J5=longitude 2, all in radians from latitude longitude as decimal number.
The result using DEGREES RETURNS a value, then use MOD(this value+360;360) for final bearing. 
 
This is what I have got in DAX, it is not working properly, it should basically avoid ATAN2.
Bearing =
VAR lat1rad = RADIANS(PI()/180)*SELECTEDVALUE(Distance_Bearing[Latitude_1])
VAR lon1rad = RADIANS(PI()/180)*SELECTEDVALUE(Distance_Bearing[Longitude_1])
VAR lat2rad = RADIANS(PI()/180)*SELECTEDVALUE('To Locations'[Latitude_2])
VAR lon2rad = RADIANS(PI()/180)*SELECTEDVALUE('To Locations'[Longitude_2])
VAR lat2lat1 = RADIANS(PI()/180)*(lat2rad-lat1rad)
VAR lon2lon1 = RADIANS(PI()/180)*(lon2rad-lon1rad)
VAR var_a = SIN(lat2lat1/2)*SIN(lat2lat1/2)+
COS(lat1rad)*COS(lat2rad)*
SIN(lon2lon1/2)*SIN(lon2lon1/2)
VAR var_c = 2 * ASIN(MIN(1;SQRT(var_a)))
VAR bearing =var_c*(180/PI())
RETURN
bearing * 6371
 
**
Paul
 

OK, I modified the formula like this (below). Since you have this in Power Query, any chance you can share lat/long points and the bearings you are expecting? ATAN2 is included in the formula below, no reason to avoid it. https://community.powerbi.com/t5/Quick-Measures-Gallery/ATAN2/td-p/963263

Bearing (Degrees) = 
    VAR __FromCity = SELECTEDVALUE('From City'[City])
    VAR __ToCity = SELECTEDVALUE('To City'[City])
    VAR __FromLat = LOOKUPVALUE('Table'[Latitude],'Table'[City],__FromCity)
    VAR __ToLat = LOOKUPVALUE('Table'[Latitude],'Table'[City],__ToCity)
    VAR __FromLong = LOOKUPVALUE('Table'[Longitude],'Table'[City],__FromCity)
    VAR __ToLong = LOOKUPVALUE('Table'[Longitude],'Table'[City],__ToCity)
    VAR __distanceLong = RADIANS(__ToLong - __FromLong)
    VAR __x = COS(RADIANS(__ToLat)) * SIN(__distanceLong)
    VAR __y = COS(RADIANS(__FromLat)) * SIN(RADIANS(__ToLat)) - SIN(RADIANS(__FromLat)) * COS(RADIANS(__ToLat)) * COS(__distanceLong)
    VAR __atan2 = 
        SWITCH(
            TRUE(),
            __x > 0, ATAN(__y/__x),
            __x < 0 && __y >= 0, ATAN(__y/__x) + PI(),
            __x < 0 && __y < 0, ATAN(__y/__x) - PI(),
            __x = 0 && __y > 0, PI()/2,
            __x = 0 && __y < 0, PI()/2 * (0-1),
            BLANK()
        )
RETURN
    MOD(
        DEGREES(__atan2) + 360,
        360
    )

 


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Are you using this?

Bearing from point A to B, can be calculated as,

β = atan2(X,Y),

where, X and Y are two quantities and can be calculated as:

X = cos θb * sin ∆L

Y = cos θa * sin θb – sin θa * cos θb * cos ∆L


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel1

Power BI Monthly Update - May 2024

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