Forum Discussion

SCHAPELM's avatar
SCHAPELM
New Member
3 years ago
Solved

Calculation distance between latitudes & Longitudes

I currently want to calculate the distance between two sites based on the lattitude & longitude. The lattitude & longitude are currently contained within the table listed against each site.

 

How i want it to work

 

Step 1: Select Site 1

Step 2:Select Site 2

= Result Distance between site 1 & 2

 

 

I need help in creating the two measures required for step 1 & step 2 then writing the equasion for the result.

 

I know the calculation to to work out the distance is:
 =acos(sin(lat1)*sin(lat2)+cos(lat1)*cos(lat2)*cos(lon2-lon1))*6371


Im new to power BI but fluent in tableau so i know how it should work in that format but new to DAX

 

Thanks 
Matt

  • tamerj1's avatar
    tamerj1
    3 years ago

    SCHAPELM 
    Thank you.

    Please refer to attached sample file with the proposed solution

    Distance = 
    VAR FromSite = SELECTEDVALUE ( 'Site From'[From Site] )
    VAR FromTable = FILTER ( 'Table', 'Table'[SiteDescription] = FromSite )
    VAR FromLatitude = MAXX ( FromTable, 'Table'[Latitude] )
    VAR FromLongitude = MAXX ( FromTable, 'Table'[Longitude] )
    VAR ToSite = SELECTEDVALUE ( 'Site To'[To Site] )
    VAR ToTable = FILTER ( 'Table', 'Table'[SiteDescription] = ToSite )
    VAR ToLatitude = RADIANS ( MAXX ( ToTable, 'Table'[Latitude] ) )
    VAR ToLongitude = RADIANS ( MAXX ( ToTable, 'Table'[Longitude] ) )
    RETURN 
        ACOS ( 
            SIN ( FromLatitude ) * SIN ( ToLatitude ) 
                + COS ( FromLatitude ) * COS ( ToLatitude ) * COS ( ToLongitude - FromLongitude ) 
        ) * 6371

6 Replies

  • tamerj1's avatar
    tamerj1
    Icon for Community Champion rankCommunity Champion

    Hi SCHAPELM 
    How do you have the locations in your data?
    Do you have the latitude and longitude in separate columns or together in one column? please present a sample

  • Hi tamerj1 
    the latitude and longitude are in seperate columns within the table against each site

    below is an example of how it is presented

     

    SiteDescriptionLatitudeLongitude
    Site 1-49.22121.86
    Site 2-38.04140.67
    Site 3-38.00140.71
    Site 4-37.94140.73
    Site 5-37.9140.56
    • tamerj1's avatar
      tamerj1
      Icon for Community Champion rankCommunity Champion

      SCHAPELM 
      Thank you.

      Please refer to attached sample file with the proposed solution

      Distance = 
      VAR FromSite = SELECTEDVALUE ( 'Site From'[From Site] )
      VAR FromTable = FILTER ( 'Table', 'Table'[SiteDescription] = FromSite )
      VAR FromLatitude = MAXX ( FromTable, 'Table'[Latitude] )
      VAR FromLongitude = MAXX ( FromTable, 'Table'[Longitude] )
      VAR ToSite = SELECTEDVALUE ( 'Site To'[To Site] )
      VAR ToTable = FILTER ( 'Table', 'Table'[SiteDescription] = ToSite )
      VAR ToLatitude = RADIANS ( MAXX ( ToTable, 'Table'[Latitude] ) )
      VAR ToLongitude = RADIANS ( MAXX ( ToTable, 'Table'[Longitude] ) )
      RETURN 
          ACOS ( 
              SIN ( FromLatitude ) * SIN ( ToLatitude ) 
                  + COS ( FromLatitude ) * COS ( ToLatitude ) * COS ( ToLongitude - FromLongitude ) 
          ) * 6371
  • Hi tamerj1 
    Thankyou for this,
    just curious as I have plotted all my sites on a map (over 500) is it possible to use the map to autoselect  'Site 1' & 'Site 2' fields in the equasion
    In essence you select site 1 from the map, then select site 2 and a card displayed on the report would show the distance. Understand if this is too complex

    cheers

    • tamerj1's avatar
      tamerj1
      Icon for Community Champion rankCommunity Champion

      SCHAPELM 
      Please see attached modified sample file

      Distance = 
      IF ( 
          COUNTROWS ( VALUES ( 'Table'[SiteDescription] ) ) = 2,
          VAR FromTable = TOPN ( 1, 'Table', 'Table'[SiteDescription], ASC )
          VAR Lat1 = RADIANS ( MAXX ( FromTable, 'Table'[Latitude] ) )
          VAR Lon1 = RADIANS ( MAXX ( FromTable, 'Table'[Longitude] ) )
          VAR ToTable = TOPN ( 1, 'Table', 'Table'[SiteDescription] )
          VAR Lat2 = RADIANS ( MAXX ( ToTable, 'Table'[Latitude] ) )
          VAR Lon2 = RADIANS ( MAXX ( ToTable, 'Table'[Longitude] ) )
          RETURN 
              ACOS ( 
                  SIN ( Lat1 ) * SIN ( Lat2 ) 
                      + COS ( Lat1 ) * COS ( Lat2 ) * COS ( Lon2 - Lon1 ) 
              ) * 6371,
          "Select two locations"
      )
  • Anonymous's avatar
    Anonymous
    Not applicable

    Dear all,

     

    i replied your code on my project in order to obtain the distance in kilometers. Unfortunately, the result is not accurate. Is it a data latitud/longitud problem? I'm affraid not bcs I tested it on a website and it recognized the kilometer distance correctly.

     

    Brief:

    - on my original database i had repeated coordinates several times since each row is a zipcode;

    - I named my site1 as "origem" and remove duplicated coordinates;

    - I named my site2 as "destination" and remove duplicated coordinates;
    - "admin name3" is location name;
    - i already testing removing duplicates in original database but nothing changed;


    my measure is:

    Distance =
    VAR FromSite = SELECTEDVALUE ( Origem[admin name3] )
    VAR FromTable = FILTER ( 'Origem', 'Origem'[admin name3] = FromSite )
    VAR FromLatitude = MAXX ( FromTable, 'Origem'[Latitude] )
    VAR FromLongitude = MAXX ( FromTable, 'Origem'[Longitude] )
    VAR ToSite = SELECTEDVALUE ( Destino[admin name3] )
    VAR ToTable = FILTER ( 'Destino', 'Destino'[admin name3] = ToSite )
    VAR ToLatitude = RADIANS ( MAXX ( ToTable, 'Destino'[Latitude] ) )
    VAR ToLongitude = RADIANS ( MAXX ( ToTable, 'Destino'[Longitude] ) )
    RETURN
        ACOS (
            SIN ( FromLatitude ) * SIN ( ToLatitude )
                + COS ( FromLatitude ) * COS ( ToLatitude ) * COS ( ToLongitude - FromLongitude )
        ) * 6371


    Could someone help me? I'm not getting why kilometeres are incorrect. Is due tables relationship? i kept it without any connection as you showed on your summary.

     

    Thank you in advance