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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
akarasick1
Frequent Visitor

Calculating minimum distance between XY coordinates

I'm trying to determine the closest point to any given point on an XY scatterplot. For simplicity, data is plotted on a 10x10 XY axis. How can I calculate the minimum distance from each point to any other point on the scatterplot? I'm trying to use something as follows, but to return the minimum sqrt:

 

Distance =
var x1 = SELECTEDVALUE(Locations[X])
var x2 = values(Locations[X])
var y1 = SELECTEDVALUE(Locations[Y])
var y2 = values(Locations[Y])
var result =
sqrt(power((x2-x1),2)+power((y2-y1),2))
return
result

2 ACCEPTED SOLUTIONS
Greg_Deckler
Super User
Super User

Probably something like:

 

Distance =
  VAR x1 = SELECTEDVALUE(Locations[X])
  VAR y1 = SELECTEDVALUE(Locations[Y])
  VAR __Table = 
    ADDCOLUMNS(
      FILTER(ALL(Locations),[X] <> x1 && [Y] <> y1)
      "distance",SQRT(POWER(([X] - x1),2) + POWER(([Y] - y1),2))
RETURN
  MINX(__Table,[distance])

@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

Well, that's what I was trying to do with that filter. You would need some kind of ID or Index. So if you had that identifier, just grab the identifier with a SELECTEDVALUE and then filter on that identifier <> that.

@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

4 REPLIES 4
Greg_Deckler
Super User
Super User

Probably something like:

 

Distance =
  VAR x1 = SELECTEDVALUE(Locations[X])
  VAR y1 = SELECTEDVALUE(Locations[Y])
  VAR __Table = 
    ADDCOLUMNS(
      FILTER(ALL(Locations),[X] <> x1 && [Y] <> y1)
      "distance",SQRT(POWER(([X] - x1),2) + POWER(([Y] - y1),2))
RETURN
  MINX(__Table,[distance])

@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Thank you, Greg. This definitely works!

 

The only tweak I'd need is that I can't exclude points that match the selected X or Y. Any way to just remove the given point and not all related x/y values?

Well, that's what I was trying to do with that filter. You would need some kind of ID or Index. So if you had that identifier, just grab the identifier with a SELECTEDVALUE and then filter on that identifier <> that.

@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Perfect. Thanks!

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors