Power BI is turning 10, and we’re marking the occasion with a special community challenge. Use your creativity to tell a story, uncover trends, or highlight something unexpected.
Get startedJoin us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.
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!
Solved! Go to 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] )
Best Regards,
Hi @Anonymous,
What's the distance? How can we calculate it?
Best Regards,
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!
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] )
Best Regards,
It works pretty well, thank you so much!
one question about the function Minx(): it is said on the MS DAX doc:
A decimal number.
https://docs.microsoft.com/en-us/dax/minx-function-dax
but it returns the 'City' in my case.
Thanks!
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!
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)).
This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.
Check out the June 2025 Power BI update to learn about new features.
User | Count |
---|---|
80 | |
79 | |
58 | |
36 | |
35 |
User | Count |
---|---|
99 | |
56 | |
56 | |
46 | |
40 |