Finding the Nth Smallest Position

ModelMad

New Member
Joined
Jan 23, 2024
Messages
9
Office Version
  1. 365
Platform
  1. Windows
Hello,

Thanks for reading. For a model i am working on i need to move activity from one building to the nth closest match which matches a specified criteria. I think i need to use the SMALL function to return the position of the smallest distance which matches my criteria, i can then offset this to the Unique ID of that site which is positioned above. Note i can't use the MATCH function as sometimes the postcodes are the same but the buildings are different (and do not match my criteria) and Match will return the first match only.

My formula below is consistently returning criteria matches but not the nth closest:

=OFFSET('Postcode Distance Matrix'!$F$7,-3,SMALL(IF(('Postcode Distance Matrix'!$G$3:$BR$3='Space Required Pivot '!$G10)*('Postcode Distance Matrix'!$G12:$BR12), COLUMN('Postcode Distance Matrix'!$G$3:$BR$3)-COLUMN('Postcode Distance Matrix'!$G12)+1), K$3))

Postcode Distance Matrix G3:BR3 - matching criteria
Space Required Pivot G - Matching criteria
Postcode Distance Matrix G12:BR12 - contains distance between buildings

Any idea how i can amend my formula to consistently return the nth smallest match??
 
How about
Excel Formula:
=XLOOKUP(SMALL(IF($B$2:$F$2=$J4,B4:F4), K2),B4:F4,B3:F3)
 
Upvote 0

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
How about
Excel Formula:
=XLOOKUP(SMALL(IF($B$2:$F$2=$J4,B4:F4), K2),B4:F4,B3:F3)
Thanks for this suggestion, it works on the isolated example but in my full dataset it still has issues with returning the wrong site code where two buildings are the same distance from the origin. I think it is still finding the first match which matches the specifications.
 
Upvote 0
Would you always want the last match?
 
Upvote 0
Would you always want the last match?
No, not necessarily. The brief is I want to identify the closest building which matches my criteria, there are some buildings which sit within the same postcode and are therefore the same distance from my origin property but have different criteria. if i use MATCH+SMALL or LOOKUP+SMALL the formula will return the first property it finds which is the same distance from the origin property as the one which matches the criteria, this isn't always the same property.
 
Upvote 0
If you have two buildings which are the same distance, how is a formula going to know which you want?
 
Upvote 0
how about this?: formula in L4

Book5
ABCDEFGHIJKLMNOPQR
1
2CriteriaCACBCDCACF1
3Site CodeAAABACADAFSite CodeCriteriaFull FormulaNew formulaExpected resultPosition Formula Position Expected ResultSmallest matching critera resultSmallest Expected
4BA0.10.30.20.050.9BACAAAADAD140.050.05
5BA0.400.61.35.4BACD
6BB0.81.202.23.3BB
7BC0.94.50.100.4BC
8BD1.12.23.55.50BD
9
Sheet1
Cell Formulas
RangeFormula
K4K4=OFFSET(A3,0,SMALL(IF(($B$2:$F$2=$J4)*(B4:F4), COLUMN($B4:$F4)-COLUMN($B4)+1), K2))
L4L4=INDEX( $B$3:$F$3,1,MAX(ROW($B$4:$F$8)*($B$4:$F$8*($A$4:$A$8=I4)*($B$2:$F$2=J4)=SMALL(FILTER(FILTER($B$4:$F$8, $A$4:$A$8=I4), $B$2:$F$2=J4), K2))))
N4N4=SMALL(IF(($B$2:$F$2=$J4)*(B4:F4), COLUMN($B4:$F4)-COLUMN($B4)+1), K2)
P4P4=SMALL(IF($B$2:$F$2=J4,B4:F4),K2)
A4:A8A4=I4
 
Upvote 0
Ok, how about
Excel Formula:
=INDEX(SORT(FILTER(VSTACK($B$3:$F$3,B4:F4),$B$2:$F$2=J4),2,,1),1,K$2)
 
Upvote 1
Solution
Ok now it is working properly... I think...
Let me know

SiteCodes.xlsx
ABCDEFGHIJK
1
2CriteriaCACBCDCACF1
3Site CodeAAABACADAFSite CodeCriteriaFull Formula
4BA0.10.30.20.050.9BACAAD
5BA0.400.61.35.4BACDAC
6BB0.81.202.23.3BBCAAA
7BC0.94.50.100.4BCCFAF
8BD1.12.23.55.50BDCBAB
Sheet1
Cell Formulas
RangeFormula
A4:A8A4=I4
K4:K8K4=INDEX($B$3:$F$3,,MAX(IFERROR((COLUMN($B$2:$F$2)-COLUMN($A$2))*($B$4:$F$8*IF($A$4:$A$8<>I4,"",1)*IF($B$2:$F$2<>J4,"",1)=SMALL(FILTER(FILTER($B$4:$F$8,$A$4:$A$8=I4),$B$2:$F$2=J4),$K$2)),0)))
 
Upvote 0

Forum statistics

Threads
1,215,076
Messages
6,122,984
Members
449,092
Latest member
Mr Hughes

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top