skip to main content
Power BI
    • What is Power BI
    • Why Power BI
    • Customer stories
    • Data visuals
    • Security
    • Power BI Desktop
    • Power BI Pro
    • Power BI Premium
    • Power BI Mobile
    • Power BI Embedded
    • Power BI Report Server
  • Pricing
    • Azure + Power BI
    • Microsoft 365 + Power BI
    • Dynamics 365 + Power BI
      • Energy
      • Healthcare
      • Manufacturing
      • Media
      • Retail
    • For analysts
    • For IT
      • Overview
      • Embedded analytics
      • Power BI visuals
      • Automation
      • Documentation
      • Community
    • Overview
    • Find consulting services
    • Partner showcase
    • Find a partner
    • Become a partner
    • Instructor-led training
    • Getting started
      • Overview
      • Online workshops
      • Self-guided learning
      • Webinars
      • Documentation
      • Roadmap
      • Overview
      • Issues
      • Give feedback
    • Blog
    • Business intelligence topics
    • Overview
    • Forums
    • Galleries
    • Submit ideas
    • Events
    • User groups
    • Community blog
    • Register
    • ·
    • Sign in
    • ·
    • Help
    Go To
    • Galleries
    • Community Connections & How-To Videos
    • COVID-19 Data Stories Gallery
    • Themes Gallery
    • Data Stories Gallery
    • R Script Showcase
    • Webinars and Video Gallery
    • Quick Measures Gallery
    • 2021 MSBizAppsSummit Gallery
    • 2020 MSBizAppsSummit Gallery
    • 2019 MSBizAppsSummit Gallery
    • Events
    cancel
    Turn on suggestions
    Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.
    Showing results for 
    Search instead for 
    Did you mean: 
    • Microsoft Power BI Community
    • Galleries
    • Quick Measures Gallery
    • Going the Distance

    Going the Distance

    03-07-2020 11:43 AM - last edited 05-13-2020 00:06 AM

    Super User Greg_Deckler
    Super User
    6052 Views
    LinkedIn LinkedIn Facebook Facebook Twitter Twitter
    Greg_Deckler
    Super User Greg_Deckler
    Super User
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    Going the Distance

    ‎03-07-2020 11:43 AM

    Uses the Haversine formula to compute the distance between the latitudes and longitudes of two points.

     

     

    c = 
        VAR __FromCity = SELECTEDVALUE('From City'[City])
        VAR __ToCity = SELECTEDVALUE('To City'[City])
        VAR __FromLat = LOOKUPVALUE('Table'[Latitude],'Table'[City],__FromCity)
        VAR __ToLat = LOOKUPVALUE('Table'[Latitude],'Table'[City],__ToCity)
        VAR __FromLong = LOOKUPVALUE('Table'[Longitude],'Table'[City],__FromCity)
        VAR __ToLong = LOOKUPVALUE('Table'[Longitude],'Table'[City],__ToCity)
        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

     

     

    c is the main measure and then you need to multiple this by the radius of the earth in either km, miles, etc.

     

    Note, this does not account for the elliptical shape of the Earth so don't use it for anything besides estimation as you could be off a few miles over long distances.

     

    eyJrIjoiNWYwYTBjZjEtZGIxNi00NWExLWI5MzMtNjJlZDg5MTA1ZDU2IiwidCI6IjRhMDQyNzQzLTM3M2EtNDNkMi04MjdiLTAwM2Y0YzdiYTFlNSIsImMiOjN9


    @ me in replies or I'll lose your thread!!!
    Instead of a Kudo, please vote for this idea
    Become an expert!: Enterprise DNA
    External Tools: MSHGQM
    YouTube Channel!: Microsoft Hates Greg
    Latest book!:
    Mastering Power BI 2nd Edition

    DAX is easy, CALCULATE makes DAX hard...
    GoingTheDistance.pbix
    173 KB
    Labels:
    • Labels:
    • Mathematical
    • Other
    Message 1 of 7
    6,052 Views
    4
    Reply
    • All forum topics
    • Previous Topic
    • Next Topic
    fran_parrett
    fran_parrett
    Frequent Visitor
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    ‎01-13-2023 03:40 AM

    Thank you @Greg_Deckler this is brilliant and works well. I have an issue where I am using "From" and "To" postcodes but in some cases I do not know the "To" postcode (so that field is blank). This is then giving me a ridiculously huge number. Do you have a suggestion for how I can get it to either give me a figure of 0 or say "unknown" if one of the postcodes is unknown? Any help would be much appreciated. I'm still new to DAX and can't figure out the best way to do this.

    Message 5 of 7
    640 Views
    0
    Reply
    Greg_Deckler
    Super User Greg_Deckler
    Super User
    In response to fran_parrett
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    ‎01-13-2023 05:59 AM

    @fran_parrett Couple thoughts, let's say that one of your postcodes is blank and that is a substitute for City in the example. You could do this:

    c = 
        VAR __FromCity = SELECTEDVALUE('From City'[City])
        VAR __ToCity = SELECTEDVALUE('To City'[City])
        VAR __FromLat = LOOKUPVALUE('Table'[Latitude],'Table'[City],__FromCity)
        VAR __ToLat = LOOKUPVALUE('Table'[Latitude],'Table'[City],__ToCity)
        VAR __FromLong = LOOKUPVALUE('Table'[Longitude],'Table'[City],__FromCity)
        VAR __ToLong = LOOKUPVALUE('Table'[Longitude],'Table'[City],__ToCity)
        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
        VAR __Result = IF( __FromCity = BLANK() || __ToCity = BLANK(), 0, __c)
    RETURN
        __Result

    @ me in replies or I'll lose your thread!!!
    Instead of a Kudo, please vote for this idea
    Become an expert!: Enterprise DNA
    External Tools: MSHGQM
    YouTube Channel!: Microsoft Hates Greg
    Latest book!:
    Mastering Power BI 2nd Edition

    DAX is easy, CALCULATE makes DAX hard...
    Message 6 of 7
    631 Views
    1
    Reply
    fran_parrett
    fran_parrett
    Frequent Visitor
    In response to Greg_Deckler
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    ‎01-13-2023 06:22 AM

    @Greg_Deckler thank you so much. Yes the city was substituted for postcodes as I needed it on a much more local scale. This worked perfectly though! I really appreciate your help.

    Message 7 of 7
    628 Views
    2
    Reply
    alissa
    alissa
    Frequent Visitor
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    ‎09-08-2021 09:51 AM

    Thank you, @Greg_Deckler !! This worked perfectly for me. Much appriciated! 

    Message 4 of 7
    4,144 Views
    2
    Reply
    MYDATASTORY
    MYDATASTORY Resolver I
    Resolver I
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    ‎03-07-2020 12:07 PM

    @Greg_Deckler  This is great, looking forward to seeing the final distance calculation.

    Message 2 of 7
    6,048 Views
    0
    Reply
    Greg_Deckler
    Super User Greg_Deckler
    Super User
    In response to MYDATASTORY
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    ‎03-07-2020 12:40 PM

    @MYDATASTORY 

     

    OK, I downloaded the UK Postal Code latitudes and longitudes from here:

    https://www.freemaptools.com/download-uk-postcode-lat-lng.htm

     

    You guys sure have a lot of postal codes, apparently about 1.7M of them and since they are unique as well as the latitudes and longitudes, well, it makes for a fairly sizeable file. Anyway, I went ahead and implemented a few columns and such to get the distances.

     

    However, they only allow a maximum upload file size of 50MB and the file is nearly twice that. So I am uploading to my personal OneDrive and will share out a file from there. Hang tight.

     

     


    @ me in replies or I'll lose your thread!!!
    Instead of a Kudo, please vote for this idea
    Become an expert!: Enterprise DNA
    External Tools: MSHGQM
    YouTube Channel!: Microsoft Hates Greg
    Latest book!:
    Mastering Power BI 2nd Edition

    DAX is easy, CALCULATE makes DAX hard...
    Message 3 of 7
    6,043 Views
    0
    Reply

    Power Platform

    • Overview
    • Power BI
    • Power Apps
    • Power Pages
    • Power Automate
    • Power Virtual Agents

    • Sign in
    • Sign up

    Browse

    • Solutions
    • Partners
    • Consulting Services

    Downloads

    • Power BI Desktop
    • Power BI Mobile
    • Power BI Report Server
    • See all downloads

    Learn

    • Guided learning
    • Documentation
    • Support
    • Community
    • Give feedback
    • Webinars
    • Developers
    • Blog
    • Newsletter

    © 2023 Microsoft

    Follow Power BI

    • Privacy & cookies
    • Manage cookies
    • Terms of use
    • Trademarks
    California Consumer Privacy Act (CCPA) Opt-Out Icon Your California Privacy Choices