Hi,
I need a DAX formula to calculate the bearing in degrees between 2 sets of latitude/longitude points, my distance DAX is working fine, but for bearing I need the ATAN2 formula which is available in PowerQuery but not in DAX.
Would appreciate your help.
Paul
See if this works for you:
https://community.powerbi.com/t5/Quick-Measures-Gallery/ATAN2/td-p/963263
- Brian
1 Like
Thank you @Brian, I have tried this one, but did not get the expected results. To be quite honest, this level of trigonometry is beyond my comfortzone
Paul
Yeah, beyond mine too.
The guy who developed this formula, Greg Deckler, is a DAX superstar on the Microsoft PBI forum, and is really responsive to questions. Iād recommend posting the unexpected results you got there and asking if he can help you resolve the issue.
- Brian
In the meantime this has been solved, thanks to all for thinking with me.
Solution:
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)