Sub topic 11
Driving a filtered view from input cells
This chapter assumes you can enter formulas in Google Sheets, use open-ended ranges such as A2:C, and write comparisons with =, <>, > and >=. It also assumes you have used FILTER with one or more conditions and have nested FILTER, SORT and UNIQUE together.
Until now, every condition in this tutorial has been typed into the formula, such as "West" or 1000. Changing the report means editing the formula. A better design puts the criteria in cells. The formula reads those cells, and anyone can change the view by changing an input.
Why move criteria into cells
Hard-coded criteria cause three problems:
- Each change needs a formula edit, and a typo can break the whole report.
- Users who should only pick a region can also overwrite the logic.
- Copying the report for another region means copying and editing the formula.
With input cells, the formula stays fixed. Only the inputs change. This is the same idea as a parameter in a function.
A worked layout
Suppose the data sits in A1:C, with a header row and these columns:
| Column | Header | Example values |
|---|---|---|
| A | Rep | Ana, Ben, Chloe |
| B | Region | West, East, North |
| C | Sales | 1200, 450, 8300 |
Put the controls on the same sheet, to the right of the data:
| Cell | Content |
|---|---|
| E1 | Region |
| F1 | Dropdown with the region to show (or empty) |
| E2 | Minimum sales |
| F2 | A number (or empty) |
The report formula goes in H1, and its output spills down and across from there.
Step 1: Replace the constant with a cell reference
The simplest version swaps the literal "West" for F1:
=FILTER(A2:C, B2:B=F1)Changing F1 from West to East changes the output immediately. The formula does not need to be touched.
Step 2: Add a dropdown
Typing a region by hand invites misspellings, and a misspelled region matches nothing. Use data validation to restrict F1 to valid values.
- Select
F1. - Choose Data > Data validation and add a rule.
- Set the criteria to Dropdown (from a range) and point it at a list of regions.
- Under advanced options, decide whether invalid entries are rejected or only flagged.
You can build the list of choices with UNIQUE. Put this formula in K1, away from your data:
=SORT(UNIQUE(B2:B))Point the dropdown at K1:K. When a new region appears in the data, the list grows without any manual step. One catch: UNIQUE(B2:B) returns a blank at the end if the range contains empty rows, and a blank can appear as a choice. Filter it out:
=SORT(UNIQUE(FILTER(B2:B, B2:B<>"")))Step 3: Treat an empty input as "show everything"
With B2:B=F1, an empty F1 compares each region to an empty value. No row matches, so the result is an error. Often you want the opposite: no selection means no restriction. Use the pattern from the Topic Spine:
=FILTER(A2:C, (F1="")+(B2:B=F1))This works because the two terms are added, which gives OR logic:
- When
F1is empty,F1=""is a singleTRUE. Google Sheets expands it to every row, so every row passes. - When
F1holds a region,F1=""isFALSE, and only rows whereB2:B=F1isTRUEpass.
The + operator turns the booleans into numbers, and FILTER treats any nonzero number as TRUE.
Several inputs at once
Each input is one more term. For the region and a minimum sales figure, combine the two optional conditions with AND logic by passing them as separate arguments:
=FILTER(A2:C, (F1="")+(B2:B=F1), (F2="")+(C2:C>=F2))Read it as two independent switches:
- The first condition passes a row if no region is chosen or the row's region matches.
- The second condition passes a row if no minimum is set or the row's sales are at least the minimum.
- Because they are separate arguments, a row must pass both.
Each parenthesized pair is OR logic internally. The comma between them is AND logic. This structure scales: add a third input by adding a third argument.
Handling a blank row inside the data
The formulas above use A2:C, which includes empty rows below the data. When F1 and F2 are both empty, every condition is TRUE for those blank rows too, so the output ends with empty rows. Add a guard that requires a non-blank key column:
=FILTER(A2:C, A2:A<>"", (F1="")+(B2:B=F1), (F2="")+(C2:C>=F2))The extra condition A2:A<>"" is an AND with everything else. It removes the blank trailing rows.
Completing the formula
A report usually needs sorting and a friendly message when nothing matches. Wrap the filter in SORT and IFERROR:
=IFERROR(
SORT(
FILTER(A2:C, A2:A<>"", (F1="")+(B2:B=F1), (F2="")+(C2:C>=F2)),
3, FALSE),
"No rows match these filters")Google Sheets accepts line breaks and indentation inside a formula, which keeps long formulas readable. In the formula bar, press Ctrl+Enter (Cmd+Enter on Mac) to insert a line break.
The order matters. FILTER runs first, because sorting rows you will discard is wasted work. SORT runs on the filtered rows. IFERROR is outermost so that it catches the #N/A from an empty FILTER.
A caution about IFERROR
IFERROR catches every error, not only "no matches." A mistyped range or a mismatch in array sizes would also show your friendly message and hide the real problem. While building, leave IFERROR off. Add it last, once the formula works on test inputs.
Inputs as a data flow
The diagram shows how the pieces connect. Inputs feed conditions, conditions feed FILTER, and the output spills into the report area.
Inputs that hold text, numbers and dates
Input cells bring type problems that typed constants hide.
- Numbers stored as text. If
F2was pasted from another source, it may be text such as"1000". ComparingC2:C>=F2then gives wrong results or none. Check by setting the cell to a number format, or wrap the reference:C2:C>=VALUE(F2).VALUEfails on an empty cell reference in some cases, so keep it inside the(F2="")+pattern only if you test the empty case. - Dates. A date typed into a cell is stored as a serial number, so
D2:D>=F3compares correctly if both sides are real dates. If the data column holds text that merely looks like dates, the comparison fails. UseISDATEorISNUMBERon a sample cell to check. - Case. The
=operator in Google Sheets compares text without regard to case, so"west"matches"West". Trailing spaces do matter. A value"West "does not equal"West". Clean the source withTRIMif you see unexplained misses.
For a date range driven by two input cells (a start date in F3 and an end date in F4), use two conditions:
=FILTER(A2:D, A2:A<>"", (F3="")+(D2:D>=F3), (F4="")+(D2:D<=F4))This assumes column D holds real dates. Leaving either input empty removes that bound.
Matching several selections
A dropdown allows one value. If a user needs several regions, put the choices in a list, for example M1:M3, and use COUNTIF or MATCH to test membership:
=FILTER(A2:C, A2:A<>"", COUNTIF(M1:M3, B2:B))COUNTIF with an array as its criteria returns a count for each row, which is 0 for a non-member and 1 or more for a member. FILTER treats nonzero as TRUE. If the list is empty, nothing matches, so wrap this in the same empty-input pattern if you want "no selection means all."
Common mistakes with input-driven views
- Putting inputs inside the spill range. If
F1sits where the report needs to spill, the result is a#REF!spill error. Keep controls above or beside the report with space to grow. - Comparing to the wrong anchor. If you later copy the formula,
F1can shift. Use$F$1when the formula will be copied, and plainF1when it will not. - Forgetting the blank-row guard. The result then has extra empty rows when all inputs are empty.
- Leaving
IFERRORon while debugging. It hides real errors. - Treating "0" as empty.
F2=""isFALSEwhenF2contains0, which is correct: zero is a real minimum. If you want zero to mean "no limit," use(F2=0)+(C2:C>=F2)or the combined check(F2="")+(F2=0)+(C2:C>=F2).
Exercises
Exercise
A sheet has data in A2:D with Product in column A, Category in column B, Price in column C, and Stock in column D. Cell G1 holds a category and cell G2 holds a maximum price. Write a formula that returns all rows for the chosen category priced at or below the maximum. An empty G1 means all categories, an empty G2 means no price limit, blank rows must not appear, and a no-match result shows "Nothing found".
Show answer
=IFERROR(FILTER(A2:D, A2:A<>"", (G1="")+(B2:B=G1), (G2="")+(C2:C<=G2)), "Nothing found") The first condition removes blank rows. The next two each use OR logic internally so an empty input switches that restriction off, and the separate arguments combine them with AND logic.
Exercise
A user clears the region dropdown in F1 and the report formula =FILTER(A2:C, B2:B=F1) returns #N/A instead of all rows. Why, and how do you fix it?
Show answer
An empty F1 is compared with each region, and no region equals an empty value, so no row passes and FILTER returns #N/A. Add an OR term for the empty case: =FILTER(A2:C, (F1="")+(B2:B=F1)). When F1 is empty, the first term is TRUE for every row.