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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

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
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.