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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
Anonymous
Not applicable

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
Anonymous
Not applicable

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
Anonymous
Not applicable

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

@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

Anonymous
Not applicable

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. 

@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

Anonymous
Not applicable

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

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors