cancel
Showing results for
Did you mean:

The ultimate Microsoft Fabric, Power BI, Azure AI & SQL learning event! Join us in Las Vegas from March 26-28, 2024. Use code MSCUST for a \$100 discount. Register Now

Helper II

## 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:

and here's what I would like to show:

Pre-Appreciated!

1 ACCEPTED SOLUTION
Employee

Hi @Erin001,

```shortestInStateCity =
MINX (
TOPN (
1,
FILTER (
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 (
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]
)
```

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.
6 REPLIES 6
Employee

Hi @Erin001,

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.
Helper II

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

Employee

Hi @Erin001,

```shortestInStateCity =
MINX (
TOPN (
1,
FILTER (
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 (
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]
)
```

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.
Helper II

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!

Solution Sage

Hello @Erin001

could you show your real dataset?

thx

Proud to be a Datanaut!

Helper II

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

Announcements

#### Microsoft Fabric Community Conference

Join us at our first-ever Microsoft Fabric Community Conference, March 26-28, 2024 in Las Vegas with 100+ sessions by community experts and Microsoft engineering.

#### Power BI Monthly Update - February 2024

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

#### Microsoft Fabric Career Hub

Explore career paths and learn resources in Fabric.

#### Microsoft Fabric Partner Community

Engage with the Fabric engineering team, hear of product updates, business opportunities, and resources in the Fabric Partner Community.

Top Solution Authors
Top Kudoed Authors