Reply
avatar user
Anonymous
Not applicable
Partially syndicated - Outbound

Finding the second smallest distance between points with dynamic coordinates defined by measures

Hi,

For each point I'm trying to find the 5 closest points. The x, y, and z coordinates for each point are defined by measures whose values change depending on user selection.

I've succeeded in determining the closest point. However, I'm struggling with finding the second closest point.
This is the measure I've used for finding the closest point:

 

 

 

Dist = 
var x0 = CALCULATE([x],FILTER('Dim Butik',SELECTEDVALUE('Dim Butik'[ButikID])))
var y0 = CALCULATE([y],FILTER('Dim Butik',SELECTEDVALUE('Dim Butik'[ButikID])))
var z0 = CALCULATE([z],FILTER('Dim Butik',SELECTEDVALUE('Dim Butik'[ButikID])))

var _Table =
    ADDCOLUMNS(
        FILTER(ALL('Dim Butik'),'Dim Butik'[ButikID]<>SELECTEDVALUE('Dim Butik'[ButikID])),
        "distance", SQRT(POWER([x]-x0,2)+POWER([y]-y0,2)+POWER([z]-z0,2)))

var _min1 = CALCULATE(MINX(_Table,[distance]))
var min1 = CALCULATE(MIN('Dim Butik'[ButikID]),FILTER(_Table,[distance]=_min1))

return min1

 

 

 

I've tried finding the next point min2 using a filter expression with [ButikID] <> min1 but I couldn't get it to work. I've also tried using RANKX but I can't get that to work either. 

 

Any help would be much appreciated. 

Edit: 

https://drive.google.com/file/d/1BzCfkNdP7LdWMyPhI2O533g_l5d-HBRQ/view?usp=sharing 
File uploaded with sample data and expected results^

 

1 ACCEPTED SOLUTION
avatar user
Anonymous
Not applicable

Syndicated - Outbound

Solved it with the addition:

var _min2 = MINX(FILTER(_Table,[distance]>_min1),[distance])
var min2 = MINX(FILTER(_Table,[distance]=_min2),'Dim Butik'[ButikID])

  

View solution in original post

5 REPLIES 5
avatar user
Anonymous
Not applicable

Syndicated - Outbound

Solved it with the addition:

var _min2 = MINX(FILTER(_Table,[distance]>_min1),[distance])
var min2 = MINX(FILTER(_Table,[distance]=_min2),'Dim Butik'[ButikID])

  

Fowmy
Super User
Super User

Syndicated - Outbound

@Anonymous 

Your 2nd smallest min could be this:

var min1 = MINX ( FILTER ( _Table, [distance] > _min1 ), 'Dim Butik'[ButikID] )
Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

avatar user
Anonymous
Not applicable

Syndicated - Outbound

Thank you for the reply.
Unfortunately, it doesn't quite seem to get the job done. I added 

 

 

var min2 = MINX(FILTER(_Table,[distance]>_min1),'Dim Butik'[ButikID])

 

 

and returned both min1 and min2. This is some of the results:

MayaB_0-1623927619641.png

That can't be right. 

Syndicated - Outbound

@Anonymous 

If you could share a PBIX file with sample data and the expected result, it would be easier.

Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

avatar user
Anonymous
Not applicable

Syndicated - Outbound

https://drive.google.com/file/d/1BzCfkNdP7LdWMyPhI2O533g_l5d-HBRQ/view?usp=sharing 
File uploaded with sample data and expected results^ 

avatar user

Helpful resources

Announcements
FebPBI_Carousel

Power BI Monthly Update - February 2025

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

March2025 Carousel

Fabric Community Update - March 2025

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

Top Solution Authors (Last Month)
Top Kudoed Authors (Last Month)