Forum Discussion

Henrique_Quint's avatar
1 year ago
Solved

Distance (in kilometers) between adresses

Hello everyone,   I'm currently in a pickle, I have two tables. One that has all my stores info and their adresses The other one has delivery adresses for every sale.   What I want to do is c...
  • danextian's avatar
    1 year ago

    You can use the Haversine formula to calculate the distance between two geo coordinates (must be a pair of latitude and longitude).

    Distance in KM =
    // Haversine Formula
    VAR EarthRadiusKm = 6371.0
    VAR Lat1 =
        RADIANS ( SELECTEDVALUE ( 'FromLocation'[Latitude] ) )
    VAR Lat2 =
        RADIANS ( SELECTEDVALUE ( ToLocation[Latitude] ) )
    VAR Lon1 =
        RADIANS ( SELECTEDVALUE ( 'FromLocation'[Longitude] ) )
    VAR Lon2 =
        RADIANS ( SELECTEDVALUE ( ToLocation[Longitude] ) )
    VAR DeltaLat = Lat2 - Lat1
    VAR DeltaLon = Lon2 - Lon1
    VAR a =
        POWER ( SIN ( DIVIDE ( DeltaLat, 2 ) ), 2 )
            + COS ( Lat1 ) * COS ( Lat2 )
                * POWER ( SIN ( DIVIDE ( DeltaLon, 2 ) ), 2 )
    VAR c =
        2 * ASIN ( SQRT ( a ) )
    RETURN
        EarthRadiusKm * c