Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
AGVEGA
Frequent Visitor

Distance between two coordinates

Calculating the top 3 closest reps to a client. Rank 1 calculate correctly, but when I expand to rank 2 or 3, it doesn't. Any tips? I saw the other post: https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Latitude-Longitude-amp-Top-3-Closest...
I tried that calculation and had the same problem. Only because I tested on Excel and it calculated correctly, did I do the following. 

Dax:

Closest Clinician 1 =

Var COS1=
'client_database'[COS Radian Lat1]

Var SIN1=
'client_database'[Sin Radian Lat1]

VAR Lat1=
'client_database'[latitude]

Var Lon1=
'client_database'[longitude]

VAR Clinicianlist=
ADDCOLUMNS(
    VALUES('cmr_database - 1 All'[Clinician & Discipline]),
    "@Rank",
    RANKX(VALUES('cmr_database - 1 All'[Clinician & Discipline]),
    VAR Lat2=CALCULATE(SELECTEDVALUE('cmr_database - 1 All'[Updated Latitude]))
    VAR Lon2= CALCULATE(SELECTEDVALUE('cmr_database - 1 All'[Updated Longitude]))
    VAR COS2=CALCULATE(SELECTEDVALUE('cmr_database - 1 All'[COS Lat2]))
    VAR SIN2=CALCULATE(SELECTEDVALUE('cmr_database - 1 All'[Sin Lat2]))

    VAR Difference=COS(RADIANS(Lat1-Lat2))

    VAR ACOSCalc=COS1*COS2+SIN1*SIN2*Difference

    Return
    ACOS(ACOSCalc)*3959,
    ,
    ASC
    )
)
return
MAXX(FILTER(Clinicianlist,[@Rank]=1),'cmr_database - 1 All'[Clinician & Discipline])




1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @AGVEGA ,

 

According to your statement, I think you want to calculate the top3 client data. I think this might be caused by your filter in return field. You use [@Rank]=1 in filter, I suggest you to try [@Rank]<=3.

Closest Clinician 1 =
VAR COS1 = 'client_database'[COS Radian Lat1]
VAR SIN1 = 'client_database'[Sin Radian Lat1]
VAR Lat1 = 'client_database'[latitude]
VAR Lon1 = 'client_database'[longitude]
VAR Clinicianlist =
    ADDCOLUMNS (
        VALUES ( 'cmr_database - 1 All'[Clinician & Discipline] ),
        "@Rank",
            RANKX (
                VALUES ( 'cmr_database - 1 All'[Clinician & Discipline] ),
                VAR Lat2 =
                    CALCULATE ( SELECTEDVALUE ( 'cmr_database - 1 All'[Updated Latitude] ) )
                VAR Lon2 =
                    CALCULATE ( SELECTEDVALUE ( 'cmr_database - 1 All'[Updated Longitude] ) )
                VAR COS2 =
                    CALCULATE ( SELECTEDVALUE ( 'cmr_database - 1 All'[COS Lat2] ) )
                VAR SIN2 =
                    CALCULATE ( SELECTEDVALUE ( 'cmr_database - 1 All'[Sin Lat2] ) )
                VAR Difference =
                    COS ( RADIANS ( Lat1 - Lat2 ) )
                VAR ACOSCalc = COS1 * COS2 + SIN1 * SIN2 * Difference
                RETURN
                    ACOS ( ACOSCalc ) * 3959,
                ,
                ASC
            )
    )
RETURN
    MAXX (
        FILTER ( Clinicianlist, [@Rank] <= 3 ),
        'cmr_database - 1 All'[Clinician & Discipline]
    )

 

Best Regards,

Rico Zhou

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

2 REPLIES 2
Anonymous
Not applicable

Hi @AGVEGA ,

 

According to your statement, I think you want to calculate the top3 client data. I think this might be caused by your filter in return field. You use [@Rank]=1 in filter, I suggest you to try [@Rank]<=3.

Closest Clinician 1 =
VAR COS1 = 'client_database'[COS Radian Lat1]
VAR SIN1 = 'client_database'[Sin Radian Lat1]
VAR Lat1 = 'client_database'[latitude]
VAR Lon1 = 'client_database'[longitude]
VAR Clinicianlist =
    ADDCOLUMNS (
        VALUES ( 'cmr_database - 1 All'[Clinician & Discipline] ),
        "@Rank",
            RANKX (
                VALUES ( 'cmr_database - 1 All'[Clinician & Discipline] ),
                VAR Lat2 =
                    CALCULATE ( SELECTEDVALUE ( 'cmr_database - 1 All'[Updated Latitude] ) )
                VAR Lon2 =
                    CALCULATE ( SELECTEDVALUE ( 'cmr_database - 1 All'[Updated Longitude] ) )
                VAR COS2 =
                    CALCULATE ( SELECTEDVALUE ( 'cmr_database - 1 All'[COS Lat2] ) )
                VAR SIN2 =
                    CALCULATE ( SELECTEDVALUE ( 'cmr_database - 1 All'[Sin Lat2] ) )
                VAR Difference =
                    COS ( RADIANS ( Lat1 - Lat2 ) )
                VAR ACOSCalc = COS1 * COS2 + SIN1 * SIN2 * Difference
                RETURN
                    ACOS ( ACOSCalc ) * 3959,
                ,
                ASC
            )
    )
RETURN
    MAXX (
        FILTER ( Clinicianlist, [@Rank] <= 3 ),
        'cmr_database - 1 All'[Clinician & Discipline]
    )

 

Best Regards,

Rico Zhou

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Thanks Rico! That worked

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

Check out the August 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

Find out what's new and trending in the Fabric community.