How to return closest value based on two criteria?

bhorsley89

New Member
Joined
Oct 24, 2024
Messages
3
Office Version
  1. 365
Platform
  1. Windows
I'm wanting to return a value that corresponds to another value that is closest to a target value that I determine. For example, in cell H4, I input the number 6 which returns 113 in cell J4. This currently works as 6 is closest to 5 in column B (mins) and the value 113 in column C (var1) is associated with this row index.

1729750852323.png


The formula I'm using to achieve this is:
=INDEX(C4:C9,MATCH(MIN(ABS(B4:B9-H4)),ABS(B4:B9-H4),0))

However! I'd like to apply the same thought process and formula to find the closest value to a target (cell H4) but based on the value in cell I4 (test type). So, in essence, how can I edit my formula to return a value in column C (var1) that is based on finding the closest value in mins (column B) to that in cell H4, but only for tests (column A) that match cell I4?

Note, I'd like to achieve these two things within the one formula and cell (J4). In addition, like the current formula, if a number I input in cell H4 doesn't exist, I'd still like the next closest to be returned.
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Welcome to the MrExcel board!

What should happen if there was two equal closest values? For example in H4 if you had 4 with Test 1 in I4.
3 and 5 are both equally close to 4, so what to do?
Would it be 117 or 113 or something else?
 
Upvote 0
Welcome to the MrExcel board!

What should happen if there was two equal closest values? For example in H4 if you had 4 with Test 1 in I4.
3 and 5 are both equally close to 4, so what to do?
Would it be 117 or 113 or something else?
That's a good question. In this situation, I think my preference would be to return 117 (3 mins). So, if a number falls within two directly either side of it, return the smallest (where possible). Having said that, in the scenario where I have 1 set as the target value in H4 and it does not exist based on I4, I'd like the next number returned.

Know I'm asking a lot here, but this is a unique scenario for a project I'm working on.
 
Upvote 0
Having said that, in the scenario where I have 1 set as the target value in H4 and it does not exist based on I4, I'd like the next number returned.
Not sure that I followed that, but see if this does what you want. If not, please provide more varied sample data, expected results and further clarification.

If providing sample data I suggest that you investigate XL2BB to make it easier for helpers by not having to manually type out sample data to test with.
(If you have trouble with XL2BB, review the "XL2BB Icons greyed out" link in the 'Known XL2BB issues' section near the top of the XL2BB Instructions page linked above.)

24 10 24.xlsm
ABCDEFGHIJ
3testminsvar1minsTest 1Output
4Test 131176Test 1113
5Test 151134Test 1117
6Test 181146Test 292
7Test 221034Test 292
8Test 24921Test 2103
9Test 371002000Test 3100
Closest
Cell Formulas
RangeFormula
J4:J9J4=LET(f,SORT(FILTER(B$4:C$9,A$4:A$9=I4),1),INDEX(SORTBY(f,ABS(H4-TAKE(f,,1))),1,2))
 
Upvote 0
Solution
Excellent! This does the job. Thanks!

Not sure I fully understand the LET() function, but it returns what I want. Appreciate the fast response.
 
Upvote 0
You can try the below :

Book2
ABCDEFGHIJ
1
2Example Data
3testmnsvar1minsTestOutput
4Test 13117Enter desired mins6Test 1113
5Test 15113
6Test 18114
7Test 22103
8Test 2492
9Test 37100
Sheet1
Cell Formulas
RangeFormula
J4J4=VLOOKUP(H4,B4:C9,2)
 
Upvote 0
Excellent! This does the job. Thanks!
You're welcome. Thanks for the follow-up. :)


Not sure I fully understand the LET() function,
LET allows us to introduce a name/variable (f in my case) and give it a value to be used (possibly a number of times) later in the formula.
=LET(f,SORT(FILTER(B$4:C$9,A$4:A$9=I4),1),INDEX(SORTBY(f,ABS(H4-TAKE(f,,1))),1,2))
In my example, f is given the values returned by the blue text. Those values are then used twice where the red f's appear.
LET can make the formula shorter and more efficient because it does not have to calculate the same information twice.
If we did not use LET my formula would be written
=INDEX(SORTBY(SORT(FILTER(B$4:C$9,A$4:A$9=I4),1),ABS(H4-TAKE(SORT(FILTER(B$4:C$9,A$4:A$9=I4),1),,1))),1,2)
which is both longer and the purple part needs to have exactly the same thing calculated twice.


You can try the below :
I'm thinking that you may have missed this request..
find the closest value to a target (cell H4) but based on the value in cell I4 (test type). So, in essence, how can I edit my formula to return a value in column C (var1) that is based on finding the closest value in mins (column B) to that in cell H4, but only for tests (column A) that match cell I4?
 
Upvote 0

Forum statistics

Threads
1,223,101
Messages
6,170,116
Members
452,302
Latest member
TaMere

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