Sub topic 9
Combining array functions in one formula
This chapter assumes you can enter formulas in Excel for Microsoft 365 or Excel 2021 or later. It also assumes you know SEQUENCE, FILTER, SORT, SORTBY and UNIQUE individually, and that you understand a spill range and its anchor cell. The chapter covers how functions pass arrays to each other, which order works best, how to avoid common mistakes, and how to keep long formulas readable.
Why combine functions
Each array function does one job. FILTER selects rows, SORT orders them, UNIQUE removes duplicates, and TAKE keeps a few. Most real questions need two or three of these steps, such as "the five largest orders over 100" or "a sorted list of customers without repeats".
In older Excel you would put each step in its own helper column. With dynamic arrays you nest the functions in one formula. The output of the inner function becomes the input of the outer one. Only the final result spills onto the sheet.
How nesting works
Excel evaluates a nested formula from the inside out, the same way it does any nested formula. Consider this one:
=SORT(UNIQUE(A2:A20))A2:A20is read as an array of 19 values.UNIQUEreceives that array and returns a shorter array with duplicates removed.SORTreceives the shorter array and returns it ordered.- The anchor cell receives the final array and spills it.
The intermediate arrays after steps 2 and 3 are never written to cells. They exist only in memory while the formula calculates. This is why one formula in one cell can do work that once needed several columns.
Choosing the order of functions
The order of nesting changes both the result and the cost.
- Filter first, then sort. Sorting fewer rows is cheaper, and the sorted output contains only rows you want.
- Sort first, then take. TAKE keeps the first rows of whatever it receives. To get a top N, the data must already be sorted when TAKE sees it.
- UNIQUE before or after SORT. Both
SORT(UNIQUE(x))andUNIQUE(SORT(x))give the same list for a single column. The first sorts the shorter list, so it is usually the better choice.
A top-N list shows why the order matters:
=TAKE(SORT(A2:B20,2,-1),3)SORT orders the two columns by column 2 from largest to smallest. TAKE then keeps the first three rows. If you swapped them, TAKE would cut the first three rows of the unsorted data, and the sort would reorder those three rows only.
Filtering with several conditions
FILTER's second argument is an array of TRUE and FALSE values. You can build that array from comparisons. When arithmetic combines TRUE and FALSE values, they become 1 and 0. So multiplication works as AND and addition works as OR:
=FILTER(A2:C20,(B2:B20="West")*(C2:C20>100),"No matches")This returns rows where the region is West and the amount is above 100.
=FILTER(A2:C20,(B2:B20="West")+(B2:B20="East"),"No matches")This returns rows for West or East. Wrap each comparison in its own parentheses. Without them, operator precedence changes the meaning.
Sorting a filtered result
Put FILTER inside SORT or SORTBY:
=SORT(FILTER(A2:C20,C2:C20>100,"No matches"),3,-1)SORT's second argument is a column number within the array it receives. After FILTER, the array has the same three columns, so column 3 is still the amount.
SORTBY works differently. It takes a separate array to sort by, and that array must have the same number of rows as the data. This is useful when the sort key is not part of the data:
=SORTBY(A2:C20,C2:C20,-1)When you filter and then use SORTBY, the sort key must be filtered the same way. Otherwise the row counts differ and the formula returns #VALUE!. SORT avoids this problem because it uses a column number inside its own input.
=LET(rows,FILTER(A2:C20,C2:C20>100),SORTBY(rows,INDEX(rows,0,3),-1))Here INDEX(rows,0,3) returns the whole third column of the filtered rows, so the key matches the data.
Using LET to name intermediate arrays
Deep nesting is hard to read, and repeating the same expression makes a formula slow and error-prone. LET lets you name an array once and use the name afterward. This is the same formula as above, written step by step:
=LET(
data, A2:C20,
big, FILTER(data, INDEX(data,0,3)>100),
TAKE(SORT(big,3,-1),5)
)Each name-value pair is calculated once. The last argument is the result that spills. You can add line breaks inside the formula bar by pressing Alt+Enter, or Ctrl+Option+Enter on a Mac. LET also helps when a value is used twice, because Excel calculates it only once.
=LET(n,SEQUENCE(5),n^2)This is the smallest case. It names the array once and spills the squares 1, 4, 9, 16 and 25.
Combining arrays of different shapes
Arithmetic between a column and a row produces a grid. Excel pairs every row value with every column value:
=SEQUENCE(3)*SEQUENCE(1,4)This spills a multiplication table of 3 rows and 4 columns. The first array is 3 by 1 and the second is 1 by 4, so each is stretched to 3 by 4.
If two arrays have different sizes and neither is a single row or column that can stretch, the smaller one is padded with #N/A. For example, adding a 5-row column to a 3-row column gives #N/A in the last two rows. Check sizes with ROWS and COLUMNS when a result looks wrong.
You can also build a table from several results. HSTACK places arrays side by side and VSTACK places them one above the other:
=LET(k,SORT(UNIQUE(A2:A20)),HSTACK(k,COUNTIF(A2:A20,k)))The name k holds the sorted unique list. COUNTIF receives the whole list as its criteria and returns one count per item. HSTACK joins the list and the counts into a two-column table that spills.
Combining with the spill reference operator
A formula can use the spill range of another formula through #. Suppose E2 holds =SORT(UNIQUE(A2:A20)). In another cell you can write:
=SUM(E2#)Or you can build a column that always matches the list's length:
=SEQUENCE(ROWS(E2#))Splitting a task across two anchor cells makes each formula shorter, and the second adjusts when the first spills more or fewer cells. The trade-off is that the sheet now has a dependency. If someone puts a value in the first spill range's path, the first formula returns #SPILL! and the second stops working correctly. A single LET formula avoids that dependency.
Handling an empty result in a chain
FILTER returns #CALC! when no row matches and you give no third argument. Every function that receives that error passes it along. A chain therefore fails when its first step finds nothing.
There are two ways to deal with this:
- Give FILTER its own message. The third argument works, but the message is a text string, and later functions treat it as data. For example,
SORT("No matches",3,-1)returns#VALUE!because the string has no third column. - Wrap the whole chain. Use IFERROR on the outside:
=IFERROR(TAKE(SORT(FILTER(A2:C20,C2:C20>100),3,-1),3),"No matches")The second approach is safer for chains. Its disadvantage is that IFERROR also hides unrelated errors, such as a mistyped range. While you build a formula, leave IFERROR off. Add it when the formula is correct.
Common mistakes
| Symptom | Likely cause | Fix |
|---|---|---|
| `#VALUE!` from SORTBY after FILTER | The sort key has more rows than the filtered data | Filter the key too, or use SORT with a column number |
| Top N shows the wrong rows | TAKE ran before SORT | Put SORT inside TAKE |
| Wrong rows from FILTER with two conditions | Missing parentheses around comparisons | Wrap each comparison before multiplying or adding |
| `#N/A` at the end of a result | Arrays of different lengths were combined | Compare sizes with ROWS and COLUMNS |
| `#SPILL!` in a chain | Something blocks the final spill range | Clear the cells below and beside the anchor |
| A count includes a 0 | UNIQUE returned a 0 for blank cells in the input | Filter out blanks before UNIQUE |
For the blank-cell problem, filter first:
=SORT(UNIQUE(FILTER(A2:A20,A2:A20<>"")))Practical guidance
- Build a chain one function at a time. Enter the innermost function first, check its spill, then wrap the next function around it.
- Use LET when a formula has more than two levels, or when the same range appears more than once.
- Keep the final result in one anchor cell with room to grow. Leave the cells below and to the right empty.
- Prefer one LET formula over several linked spill ranges when others will edit the workbook.
Exercise
A table in A2:C20 has product names in column A, regions in column B and sales in column C. Write one formula that returns the three highest-selling rows for the region "West", sorted from largest to smallest, and shows "No matches" if there is no West row.
Show answer
=IFERROR(TAKE(SORT(FILTER(A2:C20,B2:B20="West"),3,-1),3),"No matches"). FILTER keeps the West rows. SORT orders them by column 3 in descending order. TAKE keeps the first three. If FILTER finds nothing it returns #CALC!, which passes through SORT and TAKE, and IFERROR replaces it with the text.
Exercise
Why does =SORTBY(FILTER(A2:C20,C2:C20>100),C2:C20,-1) fail, and how would you fix it?
Show answer
The sort key C2:C20 has 19 rows, but the filtered data has only the rows where C is above 100. The row counts differ, so SORTBY returns #VALUE!. Either use SORT(FILTER(A2:C20,C2:C20>100),3,-1), or name the filtered rows with LET and sort by their third column: =LET(r,FILTER(A2:C20,C2:C20>100),SORTBY(r,INDEX(r,0,3),-1)).