Sub topic 9
Combining array functions in one formula
This chapter assumes you can enter formulas, use absolute references, and already know what SEQUENCE, FILTER, SORT, SORTBY and UNIQUE return and how a spill range works. Here you will nest those functions so that one formula does the work of several helper columns.
How nesting works
Every array function returns an array. That array can be the input to another function. Excel calculates from the inside out, and only the outermost function spills its result to the grid. The inner results exist only in memory, so they take up no cells.
For example, =SORT(UNIQUE(A2:A20)) first builds the list of unique values, then sorts that list. The spill range shows only the sorted result.
A worked example
Suppose A2:C20 holds a product, a region and a sales figure. You want the three largest sales above 100. Build the formula in stages.
- Filter the rows:
=FILTER(A2:C20,C2:C20>100,"No matches"). - Wrap it in SORTBY. SORTBY needs the sort column to match the filtered rows, so you must repeat the same filter for the sort key.
- Wrap it in TAKE to keep three rows.
The result looks like this:
=TAKE(SORTBY(FILTER(A2:C20,C2:C20>100,"No matches"),FILTER(C2:C20,C2:C20>100),-1),3)The filter appears twice, which is the main weakness of nesting. In the next step you will remove the repetition.
Because SORT takes a column number rather than a second array, a shorter version is possible when the sort column sits inside the filtered output:
=TAKE(SORT(FILTER(A2:C20,C2:C20>100,"No matches"),3,-1),3)Here SORT sorts by the third column of the filtered result, so the filter is written once.
Pick the right sorting function
| Situation | Use |
|---|---|
| Sort key is a column of the data you pass in | SORT with a column number |
| Sort key is a separate or calculated array | SORTBY |
| Sort by several keys | SORTBY with more key and order pairs |
Combining logic inside FILTER
FILTER takes one Boolean array as its condition. Build compound conditions with arithmetic, because the AND and OR functions collapse an array to a single TRUE or FALSE.
- AND: multiply the tests, as in
(B2:B20="East")*(C2:C20>100). - OR: add the tests, as in
(B2:B20="East")+(B2:B20="West").
=SORT(UNIQUE(FILTER(A2:A20,(B2:B20="East")*(C2:C20>100),"None")))This returns the unique products sold in the East region with sales above 100, sorted alphabetically.
Common errors
- Mismatched sizes. FILTER's include array must have the same number of rows as the data. A range such as
C2:C19againstA2:C20returns #VALUE!. - Using AND or OR on arrays. The formula returns one value, so FILTER keeps all rows or none.
- Hiding errors with IFERROR around everything. Use FILTER's third argument for empty results. An outer IFERROR also hides real mistakes.
- A blocked spill. A nested formula still spills at the outermost level, so any non-empty cell in the way gives #SPILL!.
Reducing repetition
The repeated FILTER above is a sign that a name would help. The chapter on LET later in this tutorial shows how to calculate the filtered array once and reuse it. Until then, prefer SORT with a column number when the key is part of the data.
Exercise
A2:B20 holds names in column A and scores in column B. Write one formula that returns the names and scores of the five highest scores, sorted from highest to lowest.
Show answer
=TAKE(SORT(A2:B20,2,-1),5). SORT orders the two columns by column 2 in descending order, and TAKE keeps the first five rows of that sorted array.
Exercise
Why does =FILTER(A2:C20,OR(B2:B20="East",B2:B20="West")) not return only East and West rows?
Show answer
OR collapses all the tests into a single TRUE or FALSE. FILTER then receives one value instead of one per row, so it returns every row or none. Use (B2:B20="East")+(B2:B20="West") instead.