Sub topic 16
When to use something else: regex, FILTER, QUERY, pivot tables
SUMIFS, COUNTIFS, and AVERAGEIFS cover most day-to-day summaries. They also have hard limits. This chapter shows where those limits are and which tool to switch to. It assumes you can write the three functions with several criteria pairs, use operators and wildcards, and read #DIV/0! and #N/A errors.
What the *IFS functions cannot do
The three functions share the same design, so they share the same gaps.
- They return one number. They cannot return the matching rows, a list of unique regions, or a grouped table.
- Conditions combine only with AND. OR needs the workaround from the earlier chapter, which adds several results together.
- Criteria compare a column to a value. They cannot compare one column to another, such as "Revenue greater than Cost".
- Text matching is limited. You get
*,?, and~, not patterns such as "starts with a letter, then three digits". - Every summary needs its own formula. A table with 20 regions and 12 months needs 240 formulas, or one carefully anchored formula filled across and down.
When you hit one of these walls, you do not have to throw the *IFS functions away. You can often add one function to them, or replace them with a better fit.
Regex: pattern matching beyond wildcards
Google Sheets supports regular expressions through REGEXMATCH, REGEXEXTRACT, and REGEXREPLACE. The *IFS functions do not accept regex in their criteria. Instead, pair REGEXMATCH with SUMPRODUCT, which makes REGEXMATCH test every cell in a range.
=SUMPRODUCT(REGEXMATCH(B2:B100, "^(Widget|Gadget)") * E2:E100)REGEXMATCH returns TRUE or FALSE for each product name. Multiplying by the revenue range turns TRUE into 1 and FALSE into 0, so SUMPRODUCT adds only the matching revenues. Counting is the same without the multiplication:
=SUMPRODUCT(--REGEXMATCH(B2:B100, "(?i)^wid"))The -- converts TRUE and FALSE to 1 and 0. The (?i) prefix makes the pattern case-insensitive. This matters because, unlike *IFS text criteria, regex is case-sensitive by default.
Use this approach when a wildcard would be ambiguous, for example SKUs that look like AB-1234 but not AB-12X4. Avoid it for simple "starts with" tests, where "Wid*" is shorter and easier to read.
FILTER: when you need the rows, or richer conditions
FILTER returns the rows of a range that satisfy conditions. Passing several conditions to FILTER combines them with AND, like the *IFS functions.
=FILTER(A2:E100, A2:A100="West", D2:D100>=10)The result spills into as many rows and columns as needed. Wrap it in SUM, COUNT, or AVERAGE and you have the same result as an *IFS formula:
=SUM(FILTER(E2:E100, A2:A100="West", D2:D100>=10))FILTER earns its place where the *IFS functions fail. Conditions are real logical tests, so you can compare columns:
=SUM(FILTER(E2:E100, E2:E100 > F2:F100))Here F holds a target. No criteria string could express "this column is above that column". OR logic becomes natural too, because adding two logical arrays gives TRUE when either side is TRUE:
=SUM(FILTER(E2:E100, (A2:A100="West") + (A2:A100="East")))You can also negate with NOT() or multiply conditions for an explicit AND.
Two behaviors differ from the *IFS functions:
- FILTER returns
#N/Awhen no row matches, where AVERAGEIFS returns#DIV/0!and SUMIFS and COUNTIFS return 0. Wrap it with IFERROR, but pick the fallback deliberately.=IFERROR(SUM(FILTER(...)), 0)is fine for totals. - All ranges must have the same number of rows, as with *IFS. A mismatch gives an error.
FILTER is slightly heavier to read than SUMIFS for plain cases, so keep the *IFS version when a simple condition is enough.
QUERY: a small database language inside one cell
QUERY runs a statement written in the Google Visualization query language against a range. It can select, filter, group, sort, and relabel in a single formula.
=QUERY(A1:E100, "select A, sum(E), count(E), avg(E) where D >= 10 group by A label sum(E) 'Revenue', count(E) 'Orders', avg(E) 'Average'", 1)The third argument, 1, says the range has one header row. The query uses column letters from the range (A, B, C...), not header names. One formula produces a full grouped summary for every region, which would take dozens of *IFS formulas.
Clauses must appear in a fixed order: select, where, group by, pivot, order by, limit, label. Writing them out of order is a common cause of query errors.
To drive a query from a cell, join the value into the string. Text values need single quotes inside the query:
=QUERY(A1:E100, "select sum(E) where A = '" & G2 & "' label sum(E) ''", 1)Dates must be written as date 'yyyy-mm-dd' in the query string:
=QUERY(A1:E100, "select A, sum(E) where C >= date '2026-01-01' and C < date '2026-02-01' group by A", 1)QUERY supports or, not, contains, starts with, like, and matches. The matches operator uses regex, and it must match the whole cell value.
Weaknesses to know:
- Mixed-type columns. QUERY infers one data type per column from the majority of values. A column with numbers and a few text entries can silently drop the minority values.
- String building. Quotes inside quotes get hard to read, and a stray apostrophe in a cell value can break the query.
- Column letters. If you insert a column in the source, the query's letters do not update.
Pivot tables: summaries you explore by hand
A pivot table is a built-in tool, not a formula. Select the data and choose Insert > Pivot table. Then drag fields into Rows, Columns, and Values, and choose SUM, COUNTA, or AVERAGE for each value.
Pivot tables fit when you do not yet know which cuts you want. You can regroup by region, then by product, then by month in seconds, add a filter, and group dates by month or year. They refresh when the source data changes, and a range such as A:E picks up new rows.
They are a poor fit when other formulas must refer to a result at a fixed position. A pivot table's size changes with its data, and its layout is controlled through the editor rather than by formulas.
Choosing a tool
| Need | Best fit | Reason |
|---|---|---|
| One total, count, or average with a few AND conditions | SUMIFS, COUNTIFS, AVERAGEIFS | Shortest, easiest to audit |
| Pattern like `AB-` plus four digits | SUMPRODUCT + REGEXMATCH | *IFS has no regex |
| Compare two columns | FILTER | Conditions are free-form tests |
| OR across many values | FILTER or QUERY | No added results to maintain |
| Grouped report as a formula | QUERY | Group, sort, and label in one cell |
| Explore data, then decide the cuts | Pivot table | Interactive, no formulas needed |
| The matching rows themselves | FILTER or QUERY | *IFS cannot return rows |
Practical guidance
Start with the IFS functions. They are the easiest to read and the easiest for a coworker to debug. Move to FILTER when the condition needs real logic, to QUERY when you need grouped output, and to a pivot table when you are still exploring. If you find yourself writing the same IFS formula over a grid of 100 or more cells, that is usually a sign a QUERY or pivot table would be simpler.
Exercise
Column A holds regions, column D holds units, and column E holds revenue (rows 2 to 100). You need total revenue for rows where region is "West" OR units are at least 10. Why does SUMIFS not do this directly, and which formula does?
Show answer
SUMIFS combines all criteria pairs with AND, so it returns only rows that satisfy both. Adding two SUMIFS results would double-count West rows that also have 10 or more units. Use FILTER with an added logical test instead: =SUM(FILTER(E2:E100, (A2:A100="West") + (D2:D100>=10) > 0)). The "> 0" turns the sum 0, 1, or 2 into TRUE or FALSE, so each row counts once. Wrap the formula in IFERROR(..., 0) in case no rows match.
Exercise
You want one cell that lists each region with its total revenue and order count, sorted by revenue from largest to smallest, for rows with units of at least 10. Which function fits, and what is the formula?
Show answer
QUERY fits, because it groups, sorts, and labels in one formula: =QUERY(A1:E100, "select A, sum(E), count(E) where D >= 10 group by A order by sum(E) desc label sum(E) 'Revenue', count(E) 'Orders'", 1). The clauses appear in the required order: select, where, group by, order by, label.