Sub topic 1
Why Conditional Aggregation: Totals, Counts, and Averages for a Subset
Assume you can enter formulas in Google Sheets and understand cell references such as A2:A100. You should also know the basic SUM, COUNT, and AVERAGE functions. This chapter uses a sales table with one row per transaction.
Prerequisites
You need to understand:
- How to enter and edit formulas in Google Sheets.
- How to write cell and range references.
- The difference between text, numbers, and dates.
- That a header row is usually excluded from calculation ranges.
The problem with ordinary aggregation
An ordinary aggregation calculates across every value in a range:
=SUM(E2:E100)This formula adds all revenue values. It cannot distinguish between regions, products, or dates.
Conditional aggregation calculates across only the rows that meet a rule. For example, you might ask:
- How many transactions came from the West region?
- What was the total revenue for the West?
- What was the average revenue for West transactions?
The three functions match these questions:
| Question | Function | Result |
|---|---|---|
| How many rows qualify? | `COUNTIFS` | A count |
| What is the total of qualifying values? | `SUMIFS` | A sum |
| What is the average of qualifying values? | `AVERAGEIFS` | An average |
The functions inspect a criteria range, test each row, and include only matching rows in the result.
A first example
Suppose your columns are:
A: RegionB: ProductC: DateD: UnitsE: Revenue
To count West transactions, use:
=COUNTIFS(A2:A100, "West")To add revenue only for those transactions, use:
=SUMIFS(E2:E100, A2:A100, "West")To average that revenue, use:
=AVERAGEIFS(E2:E100, A2:A100, "West")The range containing the condition is paired with the condition "West". SUMIFS and AVERAGEIFS also receive the range whose values they calculate.
Why this is useful
Conditional aggregation keeps a summary connected to the source data. When a row changes or a new matching row is added within the selected range, the result updates automatically. This is usually more useful than manually filtering rows and calculating a separate total.
The main errors at this stage are using the wrong function, placing the calculated range in the wrong position, or including a header in a numeric range. Later chapters add multiple conditions, date boundaries, wildcards, and error handling.
Exercise
Which function should you use to find the average revenue for transactions in the West region?
Show answer
Use AVERAGEIFS, with the revenue range first and the region range paired with the criterion "West": =AVERAGEIFS(E2:E100, A2:A100, "West").