Forum Discussion
Anonymous
5 years agoNot applicable
[HELP NEEDED] : Distance calculation with (latlong) in same table/column.
Bonsoir BI community ! Anyone ever tried the calculating distance using long lat in same columns before? I am working on an academic project and want to calculate the distance btween two stores per ...
- 5 years ago
Anonymous Just use Going the Distance: Going the Distance - Microsoft Power BI Community
Like this:
Column = VAR __NextLocation = MINX(FILTER('Table',[Country]=EARLIER([Country]) && [Location] > EARLIER([Location])),[Location]) VAR __FromLat = [lat] VAR __ToLat = MINX(FILTER('Table15',[Country]=EARLIER([Country]) && [Location] = __NextLocation),[lat]) VAR __FromLong = [lng] VAR __ToLong = MINX(FILTER('Table15',[Country]=EARLIER([Country]) && [Location] = __NextLocation),[lng]) VAR __distanceLong = RADIANS(__ToLong - __FromLong) VAR __distanceLat = RADIANS(__ToLat - __FromLat) VAR __a = (SIN(__distanceLat/2))^2 + COS(RADIANS(__FromLat)) * COS(RADIANS(__ToLat)) * SIN((__distanceLong/2))^2 VAR __y = SQRT(__a) VAR __x = SQRT(1 - __a) 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() ) VAR __c = 2 * __atan2 RETURN __c - 5 years ago
Anonymous Try:
Column = VAR __NextLocation = MAXX(FILTER('Table',[Country]=EARLIER([Country]) && [Location] < EARLIER([Location])),[Location]) VAR __FromLat = [lat] VAR __ToLat = MINX(FILTER('Table15',[Country]=EARLIER([Country]) && [Location] = __NextLocation),[lat]) VAR __FromLong = [lng] VAR __ToLong = MINX(FILTER('Table15',[Country]=EARLIER([Country]) && [Location] = __NextLocation),[lng]) VAR __distanceLong = RADIANS(__ToLong - __FromLong) VAR __distanceLat = RADIANS(__ToLat - __FromLat) VAR __a = (SIN(__distanceLat/2))^2 + COS(RADIANS(__FromLat)) * COS(RADIANS(__ToLat)) * SIN((__distanceLong/2))^2 VAR __y = SQRT(__a) VAR __x = SQRT(1 - __a) 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() ) VAR __c = 2 * __atan2 RETURN __c
Greg_Deckler
Community Champion
5 years agoAnonymous Just use Going the Distance: Going the Distance - Microsoft Power BI Community
Like this:
Column =
VAR __NextLocation = MINX(FILTER('Table',[Country]=EARLIER([Country]) && [Location] > EARLIER([Location])),[Location])
VAR __FromLat = [lat]
VAR __ToLat = MINX(FILTER('Table15',[Country]=EARLIER([Country]) && [Location] = __NextLocation),[lat])
VAR __FromLong = [lng]
VAR __ToLong = MINX(FILTER('Table15',[Country]=EARLIER([Country]) && [Location] = __NextLocation),[lng])
VAR __distanceLong = RADIANS(__ToLong - __FromLong)
VAR __distanceLat = RADIANS(__ToLat - __FromLat)
VAR __a = (SIN(__distanceLat/2))^2 + COS(RADIANS(__FromLat)) * COS(RADIANS(__ToLat)) * SIN((__distanceLong/2))^2
VAR __y = SQRT(__a)
VAR __x = SQRT(1 - __a)
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()
)
VAR __c = 2 * __atan2
RETURN
__cAnonymous
5 years agoNot applicable
Greg_Deckler Merci à vous ! Works perfectly!
I made slight modifications:
- I multiplied by 6371 (average earth radium in Km)
- I also applied IF(ISBLANK) to make sure the first row per country is zero, otherwise, it gives large value numbers.
Thank you for your assistance!
VAR __c * (6371)
RETURN
IF(BLANK(__NextLocation),0,(__c))