May 28 2024 04:02 AM
Hi All - I have the below ranges in different cells in Excel:
4-24, 25-72, 73-96, 96+
What I need to write a formula for, is to show in another column when searching all the above, which one is the smallest range.
Hope that makes sense.
Thanks
May 28 2024 05:31 AM
Do you mean the smallest range or do you required the range with the fewest contents?
The first would be given by
= LET(
lower, TEXTBEFORE(range, "-"),
upper, TEXTAFTER(range, "-"),
size, IFERROR(1 + upper - lower, "∞"),
minimum, MIN(size),
XLOOKUP(minimum, size, range)
)
May 28 2024 05:53 AM
The range/bin least populated by a data sample would be given by
= LET(
lower, VALUE(TEXTBEFORE(range, {"-","+"})),
count, DROP(FREQUENCY(sample, lower),1),
XLOOKUP(MIN(count), count, range)
)
In each case it is the way ranges have been specified that makes for difficulty.
May 28 2024 07:49 AM
@PeterBartholomew1 Thanks for your response. So the way I see to write this is:
= LET(lower, TEXTBEFORE(range, "4-24"), upper, TEXTAFTER(range, "96+"), size, IFERROR(1 + upper - lower, "∞"), minimum, MIN(size), XLOOKUP(minimum, size, range))
What I am trying to achieve:
If column A = 4-24, Column B = 72-96 and Column C = 25-72, then column D would bring back 4-24
May 28 2024 09:05 AM
May 29 2024 01:41 AM
May 29 2024 01:30 PM
That is done with a defined name from Name Manager
In this instance 'ranges' is used in the formula. I stopped using direct cell references of the type B3:E3 in 2016 when I also switched to array formulae for everything. It can make it hard for others to recognise what is going on.
May 30 2024 01:40 AM
May 30 2024 02:22 AM
That is good. The #N/A (not available) is a correct result for the upper bound from 96+. The IFERROR converts that to text "∞" so that the MIN function does not error. Were you to replace 96+ by 96-1000 (say) the error trapping would no longer be needed. 96-100 would also be error-free but that would be returned as the smallest range.
May 30 2024 08:00 AM
May 30 2024 02:24 PM
Sorry, I hadn't realised that the ranges do not for a unique set. You could go into Name Manager and remove the '$'s before the row numbers to make them relative references, Sheet1!$B3:$E3, that copy down as you are used to.
Alternatively, you could do as I always do and include the multiple rows within a 2D range "Sheet1!$B$3:$E$6". That means changing the formula to show that you want it to be applied row by row.
= BYROW(ranges,
LAMBDA(rng,
LET(
lower, TEXTBEFORE(rng, "-"),
upper, TEXTAFTER(rng, "-"),
size, IFERROR(1 + upper - lower, "∞"),
minimum, MIN(size),
XLOOKUP(minimum, size, rng)
)
)
)
That means you may need to look up the way in which: LET is used to define and evaluate local variables; LAMBDA is used to define functions with the parameters you specify; LAMBDA helper functions feed a sequence of parameters (rows in this instance) for evaluation by a LAMBDA function. The catch is that all this is very different from traditional spreadsheet working.
I will never go back to the old ways but, equally, many others will never make the move.