Sub topic 13
Keeping Formulas Readable with LET and Helper Functions
This chapter assumes you can already write formulas with FILTER, SORT, SEQUENCE and UNIQUE. It also assumes you know how a spill range behaves, including the # operator and the #SPILL! and #CALC! errors. You need Excel for Microsoft 365, or Excel 2021 for the LET parts. The helper functions later in the chapter need a version that includes LAMBDA.
Once you combine array functions, formulas get long. The same sub-expression often appears several times, and nobody can tell what each part means. LET fixes this by naming values. The LAMBDA helper functions fix a different problem: they apply a calculation row by row or element by element when a plain function would collapse the whole array.
The problem with repeated sub-expressions
Suppose you want the average of the values in C2:C20 that are above 100. Written without names, the formula repeats the FILTER:
=IF(COUNT(FILTER(C2:C20,C2:C20>100,""))=0,"None",AVERAGE(FILTER(C2:C20,C2:C20>100,"")))This has three problems:
- The filter condition appears twice, so you must edit it in two places.
- Excel evaluates the FILTER twice.
- A reader has to compare the two copies to confirm they are identical.
LET: naming values inside a formula
The syntax of LET is a list of name and value pairs, followed by one final calculation:
LET(name1, value1, [name2, value2, ...], calculation)The same formula with LET:
=LET(big,FILTER(C2:C20,C2:C20>100,""),IF(COUNT(big)=0,"None",AVERAGE(big)))Excel calculates big once and reuses it. The last argument is the result of the whole formula. If that result is an array, the formula spills like any other:
=LET(n,SEQUENCE(5),n^2)This spills 1, 4, 9, 16 and 25.
Rules for LET
- Each value can use any name defined before it. A name cannot refer to a name defined after it.
- A name works only inside the LET that defines it. It does not appear in the Name Manager and cannot be used in other cells.
- A name can hold a single value, a range or a whole array.
- Names must start with a letter, and they cannot look like a cell reference such as
A1orR1C1. Excel rejects the formula if you break this rule. - LET needs an odd number of arguments. That is pairs plus the final calculation. If you forget the calculation, Excel will not accept the formula.
- A formula can hold up to 126 name and value pairs.
Building a formula in steps
Each name can be one stage of a pipeline. This formula returns the three highest rows of a two-column table:
=LET(data,A2:B20,
ranked,SORT(data,2,-1),
TAKE(ranked,3))Press Alt+Enter inside the formula bar to put each pair on its own line. Excel ignores the line breaks when it calculates.
You can use the same approach with a spill reference. If E2 holds a spilling formula:
=LET(vals,E2#,SUM(vals)/ROWS(vals))Here vals always covers the current spill range, even when it grows or shrinks.
Debugging a LET formula
To see what a name holds, temporarily replace the final calculation with that name. In the earlier example, replacing TAKE(ranked,3) with ranked shows the full sorted table. Put the original calculation back when you are done.
Mermaid diagrams must use the diagram fence, so here is the pipeline as intended:
LAMBDA helper functions
Many functions reduce an array to one answer. SUM(B2:D6) returns the total of all 15 cells, not a total for each row. To get one result per row, you need a function that calls your calculation once per row. These are the LAMBDA helper functions.
A LAMBDA is a small unnamed function. Its parameters come first, and the calculation comes last:
LAMBDA(parameter1, [parameter2, ...], calculation)You rarely type LAMBDA on its own. You pass it to one of these helpers:
| Function | Calls the LAMBDA for | Returns |
|---|---|---|
| BYROW | each row of an array | one value per row |
| BYCOL | each column of an array | one value per column |
| MAP | each element of one or more same-sized arrays | an array the same size as the inputs |
| SCAN | each element, carrying an accumulator | the running value after every step |
| REDUCE | each element, carrying an accumulator | only the final accumulated value |
| MAKEARRAY | each position in a grid you size | an array with the rows and columns you request |
Examples
Row totals for B2:D6 spill as five values:
=BYROW(B2:D6,LAMBDA(rowvals,SUM(rowvals)))MAP works element by element across arrays of the same size:
=MAP(B2:B6,C2:C6,LAMBDA(qty,price,qty*price))For this case, =B2:B6*C2:C6 gives the same answer. MAP earns its place when the calculation uses a function that does not work element by element on its own.
A running total uses SCAN. The first argument is the starting value of the accumulator:
=SCAN(0,B2:B6,LAMBDA(total,value,total+value))REDUCE uses the same pattern but returns only the last accumulator value:
=REDUCE(0,B2:B6,LAMBDA(total,value,total+value))MAKEARRAY builds a multiplication table:
=MAKEARRAY(3,4,LAMBDA(rw,cl,rw*cl))This spills the same grid as =SEQUENCE(3,4) only when the calculation is (rw-1)*4+cl. As written, it gives the products 1 to 12 in a 3 by 4 layout.
The one-value rule
Excel cannot place an array inside a single cell of another array. For BYROW, BYCOL and MAP, each call to your LAMBDA must therefore return one value. If it returns several, the whole formula returns #CALC!:
=BYROW(B2:D6,LAMBDA(rowvals,rowvals*2))This fails because each row would produce three values. Use a calculation that produces a single value per row, such as SUM, MAX or AVERAGE.
Combining LET and helpers
LET and the helpers work well together. LET names each stage, and the helper handles the per-row step. This formula takes names in A2:A20 and three score columns in B2:D20. It returns the three people with the highest total score:
=LET(people,A2:A20,
scores,B2:D20,
totals,BYROW(scores,LAMBDA(rowvals,SUM(rowvals))),
table,HSTACK(people,totals),
TAKE(SORTBY(table,totals,-1),3))Read it from top to bottom:
peopleandscoresgive the input ranges meaningful names.totalsholds one total per person.tablejoins names and totals side by side.- The last line sorts by
totals, highest first, and keeps three rows.
The result spills as three rows and two columns.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `#NAME?` | A name is misspelled, or you used a LET name outside its formula | Check spelling, and keep the use inside the LET |
| `#CALC!` from BYROW, BYCOL or MAP | The LAMBDA returned an array, not one value | Return a single value per call |
| `#CALC!` from FILTER inside LET | The filter found no rows and has no third argument | Add an if-empty value, or test for it |
| `#VALUE!` from a helper | The LAMBDA has a different number of parameters than the helper supplies | Match the parameter count to the helper |
| `#SPILL!` | The result spills into cells that are not empty | Clear the blocking cells |
| Excel will not accept the formula | Invalid name, or no final calculation | Rename the name, or add the result as the last argument |
Choosing the right tool
Use LET when a value appears more than once, or when naming a step makes the formula easier to read. Use a helper function when the calculation must run once per row, per column or per element, and no built-in function does that. If a plain operation such as =B2:B6*C2:C6 already works, keep it. It is shorter and every Excel version since the dynamic array engine understands it.
Check version support before sharing a workbook. LET is in Excel 2021 and later. LAMBDA and its helpers are in Microsoft 365, and Excel 2021 does not include them. Functions such as TAKE and HSTACK arrived after LET. In a version that lacks a function, the cell shows #NAME?.
Exercise
Write one formula using LET that averages the values in C2:C20 that are greater than 100. It must return the text "None" when no value is above 100, and it must write the FILTER condition only once.
Show answer
=LET(big,FILTER(C2:C20,C2:C20>100,""),IF(COUNT(big)=0,"None",AVERAGE(big))). The name big holds the filtered values. FILTER returns "" when nothing matches, COUNT of that is 0, and the IF returns "None".
Exercise
Why does =BYROW(B2:D6,LAMBDA(rowvals,rowvals*2)) return #CALC!, and how would you change it to return the largest value in each row?
Show answer
Each call to the LAMBDA returns three values (one row doubled), and BYROW can only accept one value per row. Use =BYROW(B2:D6,LAMBDA(rowvals,MAX(rowvals))), which returns one value per row and spills five results.