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

Get certified in Microsoft Fabric—for free! For a limited time, the Microsoft Fabric Community team will be offering free DP-600 exam vouchers. Prepare now

Reply
Anonymous
Not applicable

Ranking and displaying cities of shortest distance

Hi,

I have a data set that contains columns Country, State, City, Location X, Location Y; (X,Y) is the location on the coordinate. So anybody can help me on how to caculate the shortest distance starting from every city to any other instate cities and display those cities(with the first and second shortest distance) in columns?

 

Here's the original data:

 

Original.PNG 

 

and here's what I would like to show: 

 

Result.PNG

 

 

Pre-Appreciated!

 

1 ACCEPTED SOLUTION

Hi @Anonymous,

 

Try these two measures, please.

shortestInStateCity =
MINX (
    TOPN (
        1,
        FILTER (
            ADDCOLUMNS (
                CROSSJOIN (
                    ALL ( 'Table1' ),
                    SELECTCOLUMNS (
                        'Table1',
                        "City2", [City],
                        "Country2", [Country],
                        "State2", [State],
                        "X2", [X],
                        "Y2", [Y]
                    )
                ),
                "Distance", SQRT ( POWER ( [X] - [X2], 2 ) + POWER ( [Y] - [Y2], 2 ) )
            ),
            [Distance] <> 0
                && [State] = [State2]
        ),
        [Distance], ASC
    ),
    [City]
)
2ndShortestInStateCity =
MINX (
    TOPN (
        1,
        TOPN (
            2,
            FILTER (
                ADDCOLUMNS (
                    CROSSJOIN (
                        ALL ( 'Table1' ),
                        SELECTCOLUMNS (
                            'Table1',
                            "City2", [City],
                            "Country2", [Country],
                            "State2", [State],
                            "X2", [X],
                            "Y2", [Y]
                        )
                    ),
                    "Distance", SQRT ( POWER ( [X] - [X2], 2 ) + POWER ( [Y] - [Y2], 2 ) )
                ),
                [Distance] <> 0
                    && [State] = [State2]
            ),
            [Distance], ASC
        ),
        [Distance], DESC
    ),
    [city]
)

Ranking-and-displaying-cities-of-shortest-distance

 

Best Regards,

Community Support Team _ Dale
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

6 REPLIES 6
v-jiascu-msft
Microsoft Employee
Microsoft Employee

Hi @Anonymous,

 

What's the distance? How can we calculate it?

 

 

Best Regards,

Community Support Team _ Dale
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Anonymous
Not applicable

every city is described as a point(x,y) on the coordinate, and the distance between 2 cities is calculated as D = sqrt((x1-x2)*(x1-x2) +(y1-y2)*(y1-y2)).

 

Thanks!

 

@v-jiascu-msft

Hi @Anonymous,

 

Try these two measures, please.

shortestInStateCity =
MINX (
    TOPN (
        1,
        FILTER (
            ADDCOLUMNS (
                CROSSJOIN (
                    ALL ( 'Table1' ),
                    SELECTCOLUMNS (
                        'Table1',
                        "City2", [City],
                        "Country2", [Country],
                        "State2", [State],
                        "X2", [X],
                        "Y2", [Y]
                    )
                ),
                "Distance", SQRT ( POWER ( [X] - [X2], 2 ) + POWER ( [Y] - [Y2], 2 ) )
            ),
            [Distance] <> 0
                && [State] = [State2]
        ),
        [Distance], ASC
    ),
    [City]
)
2ndShortestInStateCity =
MINX (
    TOPN (
        1,
        TOPN (
            2,
            FILTER (
                ADDCOLUMNS (
                    CROSSJOIN (
                        ALL ( 'Table1' ),
                        SELECTCOLUMNS (
                            'Table1',
                            "City2", [City],
                            "Country2", [Country],
                            "State2", [State],
                            "X2", [X],
                            "Y2", [Y]
                        )
                    ),
                    "Distance", SQRT ( POWER ( [X] - [X2], 2 ) + POWER ( [Y] - [Y2], 2 ) )
                ),
                [Distance] <> 0
                    && [State] = [State2]
            ),
            [Distance], ASC
        ),
        [Distance], DESC
    ),
    [city]
)

Ranking-and-displaying-cities-of-shortest-distance

 

Best Regards,

Community Support Team _ Dale
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Anonymous
Not applicable

@LivioLanzo@v-jiascu-msft

 

It works pretty well, thank you so much!

 

one question about the function Minx(): it is said on the MS DAX doc: 

Return value

A decimal number.

 

https://docs.microsoft.com/en-us/dax/minx-function-dax

 

but it returns the 'City' in my case. 

 

Thanks!

 

 

 

 

 

 

LivioLanzo
Solution Sage
Solution Sage

Hello @Anonymous

 

could you show your real dataset?

 

thx

 


 


Did I answer your question correctly? Mark my answer as a solution!


Proud to be a Datanaut!  

Anonymous
Not applicable

sorry, I can't.  The data I posted are similar to my real data.

 

every city is described as a point(x,y) on the coordinate, and the distance between 2 cities is calculated as D = sqrt((x1-x2)*(x1-x2) +(y1-y2)*(y1-y2)).

 

@LivioLanzo

 

 

Helpful resources

Announcements
OCT PBI Update Carousel

Power BI Monthly Update - October 2024

Check out the October 2024 Power BI update to learn about new features.

September Hackathon Carousel

Microsoft Fabric & AI Learning Hackathon

Learn from experts, get hands-on experience, and win awesome prizes.

October NL Carousel

Fabric Community Update - October 2024

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