Forum Discussion
Distance (in kilometers) between adresses
- 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
you will have more luck using a routing service API like Google Maps or similar - but you would have to precalculate that for each combination of store and delivery location. Most of them are not free, but you wouldn't have to calculate that often as the distance only changes when there is contruction, closures or detours.
Isn't radius (straight line) a viable option? Do I need to have latitude and longitude to do it?
- lbendlin1 year ago
Super User
yes, you would need these. You can get them from geocoding API services which are - you guessed it - mostly not free.
And it may not be meaningful - just imagine there is a river between store and delivery and the next bridge is 50 km out...