Sub topic 13
Next step: SUMIF, COUNTIF, and AVERAGEIF
Assume you can enter formulas, use cell references such as A2, and select ranges in Google Sheets. This chapter uses those skills to calculate results for only the rows that match a condition.
Why conditional functions matter
SUM, COUNT, and AVERAGE process every relevant cell. Their conditional versions first test each row, then include only matching rows.
SUMIFadds values that meet a condition.COUNTIFcounts cells that meet a condition.AVERAGEIFcalculates the mean of values that meet a condition.
Each function uses this general pattern:
=FUNCTION(criteria_range, criterion, result_range)The criteria_range contains the values to test. The criterion describes the test. The result_range contains the numbers to add or average. COUNTIF needs only the first two arguments because it counts matching cells in the criteria range.
A worked example
Suppose a sheet contains this order data:
| A | B | C |
|---|---|---|
| Product | Region | Sales |
| Apples | East | 120 |
| Oranges | West | 90 |
| Apples | West | 150 |
| Bananas | East | 80 |
| Apples | East | 110 |
To add sales for Apples only, use:
=SUMIF(A2:A6, "Apples", C2:C6)The function checks A2:A6. It adds the corresponding values from C2:C6 when the product is Apples. The result is 380.
To count orders from the East region, use:
=COUNTIF(B2:B6, "East")This returns 3. The function counts matching cells in B2:B6.
To find the average sales for Apples, use:
=AVERAGEIF(A2:A6, "Apples", C2:C6)This averages 120, 150, and 110, returning 126.67.
Criteria and common errors
Text criteria usually go inside quotation marks:
=SUMIF(B2:B6, "East", C2:C6)Comparison criteria also need quotation marks:
=COUNTIF(C2:C6, ">100")For a criterion stored in another cell, join the operator to that cell with &. If E1 contains 100, use:
=COUNTIF(C2:C6, ">"&E1)The criteria range and result range should cover the same rows. For example, A2:A6 should pair with C2:C6. If the ranges have different sizes, the result can be confusing because rows no longer line up as intended.
AVERAGEIF returns #DIV/0! when no rows match or when matching result cells contain no numbers. You can show a useful message instead:
=IFERROR(AVERAGEIF(A2:A6, "Pears", C2:C6), "No matching sales")A cell containing zero is a number and participates in an average. A blank result cell is ignored by AVERAGEIF.
Choosing the right function
| Task | Function |
|---|---|
| Add sales for one region | `SUMIF` |
| Count orders above 100 | `COUNTIF` |
| Average sales for one product | `AVERAGEIF` |
These functions handle one condition. When a task requires several conditions, such as Apples sold in the East, use the related SUMIFS, COUNTIFS, or AVERAGEIFS functions.
Exercise
Write a formula that counts sales values in C2:C6 greater than 100.
Show answer
=COUNTIF(C2:C6, ">100")