Sub topic 13
Keeping formulas readable with LET and helper functions
Long array formulas get hard to read when the same sub-expression appears several times. This chapter shows how LET names intermediate arrays, and when to use helper functions that process arrays in other ways. It assumes you can already write FILTER, SORT and UNIQUE formulas and know what a spill range is. You need Excel for Microsoft 365 or Excel 2021 or later.
Naming values with LET
LET assigns a name to a value or array, then uses the name in a final calculation. The pattern is name, value, name, value, and so on, with the result expression last.
=LET(n,SEQUENCE(5),n^2)Here n holds the array 1 to 5. The final expression n^2 spills the squares 1, 4, 9, 16 and 25. Without LET you would write SEQUENCE(5)^2, which works for this small case but gets awkward when the array is used more than once.
Why it helps
- Readability. A name such as
big_salessays more than a repeatedFILTER(...)call. - Speed. Excel evaluates each named value once, even if the name is used many times.
- Fewer edits. If a range changes, you edit it in one place.
A worked example
Suppose A2:C20 holds a table with a label in column A, a region in column B and an amount in column C. You want the three largest rows among those with an amount above 100. Written as one nested formula, the FILTER call has to be repeated if you also want to count the matches. With LET you name it once:
=LET(
rows, FILTER(A2:C20, C2:C20>100, "No matches"),
TAKE(SORTBY(rows, INDEX(rows,,3), -1), 3)
)Reading from the top:
rowsstores the filtered table.INDEX(rows,,3)returns the third column ofrows, which is the amount.SORTBYsortsrowsby that column in descending order.TAKE(...,3)keeps the first three rows.
The result spills as a 3-row, 3-column block. Note that the sort key is taken from rows, not from C2:C20. After filtering, the original range has a different size from rows, and SORTBY would return #VALUE! if the two did not match.
Helper functions for arrays
LET pairs well with the lambda helper functions. They take an array and a LAMBDA, which is a small unnamed function you write inline, and apply it across the array.
| Function | What it does |
|---|---|
| `MAP` | Applies a lambda to each element and returns an array of the same shape |
| `BYROW` | Applies a lambda to each row and returns one value per row |
| `BYCOL` | Applies a lambda to each column and returns one value per column |
| `REDUCE` | Combines elements into a single accumulated value |
| `SCAN` | Like `REDUCE`, but returns every intermediate value |
For example, this returns the largest value in each row of A2:C20:
=BYROW(A2:C20, LAMBDA(r, MAX(r)))Here r is the parameter name for one row. The result spills as one column with a value per row.
Choosing between them
Use plain array arithmetic first, such as =B2:B6*C2:C6. Reach for BYROW or MAP only when a function does not accept an array in the way you need, for example when you want a per-row MAX rather than a single MAX over everything. Lambda helpers are slower than built-in array operations on large ranges.
Common errors
- Missing result expression.
LETmust end with a calculation. If the last argument is a name and value pair, Excel reports a formula error. - Wrong number of arguments.
LETneeds an odd count: pairs plus one result. - Sort or filter size mismatch. Arrays passed together must have the same number of rows or columns.
- Lambda not called. A
LAMBDAtyped alone in a cell returns#CALC!because nothing supplies its arguments. Pass it to a helper such asBYROW.
Exercise
Rewrite =SORT(UNIQUE(A2:A20)) using LET so that the unique list is stored in a name called u before sorting.
Show answer
=LET(u, UNIQUE(A2:A20), SORT(u))
Exercise
A formula =BYROW(A2:C20, LAMBDA(r, MAX(r))) is entered in E2. What shape does the spill range have, and which reference would you use to sum all its values?
Show answer
It spills one column with one value per row of A2:C20, so 19 rows by 1 column. Use =SUM(E2#) to sum the whole spill range.