Sub topic 13
Next step: SUMIF, COUNTIF, and AVERAGEIF
This chapter assumes you can write SUM, AVERAGE, and COUNT over a range, and that you know how those functions treat text and blank cells. It also assumes you are comfortable with cell addresses such as B2 and with ranges such as B2:B6. The conditional functions are the same aggregates with a filter added: they work on only the cells that meet a rule you set.
Why add a condition
SUM, AVERAGE, and COUNT work on every number in the range you give them. Real data usually mixes categories. A sales sheet has several products, a gradebook has several classes, and an expense log has several months. You often want one slice, such as total sales for apples or how many orders exceeded 100.
You could sort the data and select a smaller range by hand. That breaks when rows are added or reordered. The conditional functions let the formula decide which rows count.
The three functions in this chapter are:
- SUMIF adds the values in matching rows.
- COUNTIF counts the matching cells.
- AVERAGEIF averages the values in matching rows.
The sample data
The examples use this small table. Product names are in A2:A6 and sales amounts are in B2:B6.
| Row | A (Product) | B (Sales) |
|---|---|---|
| 2 | Apples | 120 |
| 3 | Pears | 80 |
| 4 | Apples | 150 |
| 5 | Bananas | 60 |
| 6 | Pears | 95 |
The plain total, =SUM(B2:B6), is 505. Every result below is a part of that total or a count of its rows.
SUMIF
The syntax is:
=SUMIF(range, criterion, [sum_range])rangeis the group of cells that the criterion is tested against.criterionis the rule a cell must meet.sum_rangeis the group of cells to add. It is optional. If you leave it out, SUMIF adds the cells inrangeitself.
To add the sales for apples, test column A and add column B:
=SUMIF(A2:A6, "Apples", B2:B6)The result is 270, which is 120 plus 150. The ranges are different columns, but they line up row by row. Row 2 is tested in column A and added from column B.
When the test and the sum use the same column, omit the third argument:
=SUMIF(B2:B6, ">90")This adds only the values greater than 90, which are 120, 150, and 95. The result is 365.
COUNTIF
The syntax has no sum range, because it counts cells and does not add values:
=COUNTIF(range, criterion)To count orders above 100:
=COUNTIF(B2:B6, ">100")The result is 2. To count how many rows are for apples:
=COUNTIF(A2:A6, "Apples")The result is also 2. Unlike COUNT, COUNTIF is not limited to numbers. It counts text cells that match the criterion, so it works on a column of names, statuses, or categories.
AVERAGEIF
The syntax mirrors SUMIF:
=AVERAGEIF(criteria_range, criterion, [average_range])To find the average sale for pears:
=AVERAGEIF(A2:A6, "Pears", B2:B6)The result is 87.5, which is 80 plus 95, divided by 2. As with SUMIF, omitting the last argument averages the cells that were tested.
Writing criteria
The criterion is the part that most often goes wrong. It is a single value, and Sheets reads it as a rule.
| Criterion | Meaning |
|---|---|
| `"Apples"` | Cells equal to Apples. Matching ignores uppercase and lowercase. |
| `">100"` | Numbers greater than 100. |
| `"<=50"` | Numbers less than or equal to 50. |
| `"<>Apples"` | Cells not equal to Apples. |
| `"P*"` | Text that starts with P. `*` stands for any run of characters. |
| `"?ears"` | Text such as Pears or Bears. `?` stands for exactly one character. |
| `"<>"` | Cells that are not empty. |
| `""` | Cells that are empty. |
Three rules apply to all of these.
- A criterion that contains an operator must be inside quotation marks. Write
">100", not>100. - A bare number or bare text works as an "equals" test.
100and"=100"are equivalent. - To match a literal
*or?, put a tilde in front of it, as in"~*".
Here are two more examples using the sample data:
=SUMIF(A2:A6, "<>Apples", B2:B6)
=SUMIF(A2:A6, "P*", B2:B6)The first adds everything except apples and returns 235. The second adds every product beginning with P and returns 175, which is the two pear rows.
Using a cell as the criterion
Hard-coded criteria force you to edit the formula whenever the question changes. Put the criterion in a cell instead. If D1 contains the word Apples:
=SUMIF(A2:A6, D1, B2:B6)For an operator, join it to the cell with &. If D2 contains 100:
=COUNTIF(B2:B6, ">"&D2)The & builds the text ">100" and passes it to COUNTIF. Now you can type a different threshold in D2 and the count updates. Writing ">D2" instead would compare against the literal text "D2", which is a common mistake.
How the choice works
The diagram shows how to pick among the functions, depending on what you need from the matching rows.
More than one rule
SUMIF, COUNTIF, and AVERAGEIF take one rule. For several rules that must all be true, Sheets has SUMIFS, COUNTIFS, and AVERAGEIFS. Their argument order differs from the single-rule versions. In SUMIFS and AVERAGEIFS, the range to add or average comes first:
=SUMIFS(B2:B6, A2:A6, "Apples", B2:B6, ">100")
=COUNTIFS(B2:B6, ">=80", B2:B6, "<=100")The first adds apple sales above 100, which is 120 plus 150, or 270. The second counts values from 80 through 100, which are 80 and 95, so it returns 2. Each rule is a pair of a range and a criterion, and a row counts only when every pair is satisfied. This chapter does not cover them further, but the second example is the usual way to count values between two limits.
Errors and gotchas
No matching rows in AVERAGEIF
AVERAGEIF divides by the number of matches. If nothing matches, that is a division by zero, and you get #DIV/0!. SUMIF and COUNTIF return 0 in the same situation, so they do not fail. Handle the AVERAGEIF case with IFERROR, as you did for AVERAGE:
=IFERROR(AVERAGEIF(A2:A6, "Kiwis", B2:B6), "No sales")IFERROR hides every error, not only #DIV/0!. A typo in a range reference would also show "No sales", so use it once the formula is known to work.
Ranges of different sizes
In SUMIF, range and sum_range should have the same height and width. Sheets lines up the sum range using its top-left cell and the shape of the criteria range, so a mismatched sum range can silently add the wrong rows. Always select the same number of rows for both, and keep their start rows aligned.
Trailing spaces and spelling
An "Apples" criterion does not match "Apples " with a trailing space, or "Apple". Matching ignores case but not spacing. If a COUNTIF returns fewer rows than you can see, look for stray spaces in the data.
Filtered and hidden rows
SUMIF, COUNTIF, and AVERAGEIF look at every row in the range, including rows hidden by a filter. They test your criterion and nothing else. To aggregate only the rows a filter leaves visible, use SUBTOTAL, which is covered in the next chapter.
A worked example
Suppose D1 holds a product name and D2 holds a minimum sale. One formula can report the average of that product's sales, and another can count how many sales reached the minimum:
=IFERROR(AVERAGEIF(A2:A6, D1, B2:B6), "No sales")
=COUNTIF(B2:B6, ">="&D2)With D1 set to Pears and D2 set to 90, the first formula returns 87.5. The second counts 120, 150, and 95, so it returns 3. Changing D1 or D2 updates both results without touching the formulas.
Summary
- SUMIF, COUNTIF, and AVERAGEIF apply SUM, COUNT, and AVERAGE to only the rows that meet one rule.
- The optional last argument of SUMIF and AVERAGEIF is the range to add or average. Without it, the tested range is used.
- Criteria are text rules. They support operators,
*and?wildcards, and cell references joined with&. - AVERAGEIF returns
#DIV/0!when nothing matches, while SUMIF and COUNTIF return 0. - Use the S versions for several rules, and SUBTOTAL for filtered data.
Exercise
Using the sample table (A2:A6 holds Apples, Pears, Apples, Bananas, Pears and B2:B6 holds 120, 80, 150, 60, 95), write one formula for each task. (1) The total sales of everything except Pears. (2) The number of sales below 100. (3) The average sale for Bananas, showing "None" if there is no match. Give each result.
Show answer
(1) =SUMIF(A2:A6, "<>Pears", B2:B6) returns 330, which is 120 + 150 + 60. (2) =COUNTIF(B2:B6, "<100") returns 3, for 80, 60, and 95. (3) =IFERROR(AVERAGEIF(A2:A6, "Bananas", B2:B6), "None") returns 60, because Bananas appears once with a value of 60.
Exercise
Cell D2 holds the number 100. A colleague writes =COUNTIF(B2:B6, ">D2") and gets 0. Why, and what is the fix?
Show answer
The criterion ">D2" is a fixed piece of text, so Sheets compares each value with the literal text "D2" and nothing matches. Join the operator to the cell value with an ampersand: =COUNTIF(B2:B6, ">"&D2). With D2 equal to 100, this counts 120 and 150 and returns 2.