#PowerPlatformTip 44: 'Calculate the Distance'
TL;DR: Calculate great-circle distance for free using the Haversine formula: copy the ready-made snippet and plug in your two lat/long pairs.
Here’s a cool tip to calculate the distance between two places without relying on premium services like Google or Bing maps.
💡 Challenge
How do you provide accurate, real-time location-based distance calculations inside PowerApps or Power Automate, without extra costs?
✅ Solution
You can use the Haversine formula, a proven geospatial calculation mechanism.
- There’s a ready-to-use code snippet available: CalculateDistance.json @ GitHub, Marcel Lehmann
- Just copy the JSON logic and paste into your Power Platform workflow (typically as an inline calculation or custom connector).
- Replace the latitude and longitude values for locations A and B with your own values.
🔧 How It’s Done
- Obtain coordinates:
- Manually input latitude and longitude for each location, or use the PowerApps location function to fetch the user/device position (
Location.Latitude,Location.Longitude).
- Manually input latitude and longitude for each location, or use the PowerApps location function to fetch the user/device position (
-
Drop the values into the shared script or formula:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
// Example for PowerApps With( { r: 6371000, p: (Pi() / 180), latA: YourStartLatitude, lonA: YourStartLongitude, latB: YourEndLatitude, lonB: YourEndLongitude }, Round( (2 * r) * Asin( Sqrt( 0.5 - Cos((latA - latB) * p) / 2 + Cos(latB * p) * Cos(latA * p) * (1 - Cos((lonA - lonB) * p)) / 2 ) ), 2 ) ) - For JSON users, import the code from the referenced snippet and wire up your variables according to context.
- The result is your distance in meters, output instantly in your app or flow.
🎉 Result
Enjoy accurate, fast, and cost-effective distance calculations for any business scenario, including logistics, travel, or field service.
🌟 Key Advantages
- No dependency on paid mapping services
- Works both in manual and automated scenarios
- Can leverage device location for dynamic, user-centered calculations
🛠️ FAQ
1. What is the Haversine formula?
A mathematical function for calculating the shortest distance between two points on the Earth, given their latitude and longitude.
2. How do I get my coordinates in PowerApps?
Use built-in location functions or user input fields to collect latitude and longitude.
3. Can I use this in Power Automate as well?
Yes, simply adapt the code for use in expressions, custom connectors, or flow steps.
🔗 Related Tips
🔸 #PowerPlatformTip 43: ‘With Function’ - the function keeping the Haversine formula above readable instead of a wall of nested math.
🔸 #PowerPlatformTip 72: ‘PowerApps with Google Maps API’ - a mapping alternative for when you need more than a raw distance number.
🎥 Video Tutorial
This tip is by Marcel Lehmann, Microsoft MVP (Business Applications) and founder of the Power Platform Academy, which runs free live workshops (in German). See the next free workshop dates (in German). For companies that want this implemented for them, see KMUpower: Power Platform consulting & automation for SMEs (DACH).