Sub topic 5
AVERAGEIFS: averaging matching values
This chapter assumes you can enter and copy formulas, understand relative and absolute references, and have used SUMIFS and COUNTIFS. It uses the sample sales data from earlier chapters: headers in row 1, data in rows 2 to 100, with Date in A, Region in B, Product in C, Amount in D, and Notes in E.
The working model
AVERAGEIFS returns the mean of the values in one range, but only for rows where every condition is true. It is built like SUMIFS. The range to average comes first, followed by one or more pairs of a criteria range and a criteria.
=AVERAGEIFS(D2:D100, B2:B100, "West")For the sample rows, the West amounts are 1200 and 310, so the result is 755. Excel works out the same number as SUMIFS / COUNTIFS for the same conditions.
Adding more conditions
Add more pairs to narrow the rows. All pairs must be true for a row to count, which is AND logic.
=AVERAGEIFS(D2:D100, B2:B100, "West", C2:C100, "Laptop")This averages Amount for West laptop rows only. The criteria ranges must have the same number of rows and columns as the average range. If they differ, Excel returns #VALUE!.
Operators work as they do in the other functions. This formula averages only the amounts of 500 or more:
=AVERAGEIFS(D2:D100, D2:D100, ">=500")What AVERAGEIFS ignores
Blank cells and text in the average range are ignored. A blank is not treated as zero, so it does not pull the mean down. If you want blanks counted as zero, enter 0 in them.
The #DIV/0! error
If no row meets all the conditions, there is nothing to divide. AVERAGEIFS then returns #DIV/0!. The sample data has no South rows, so this formula fails:
=AVERAGEIFS(D2:D100, B2:B100, "South")Wrap the formula in IFERROR to show readable text instead:
=IFERROR(AVERAGEIFS(D2:D100, B2:B100, "South"), "No sales")Common mistakes
| Symptom | Likely cause |
|---|---|
| `#DIV/0!` | No rows matched, or the matching cells in the average range are all blank or text |
| `#VALUE!` | Ranges have different sizes |
| Average is too high | Blank cells are skipped, not counted as zero |
| Unexpected result for a text match | Trailing spaces in the data, such as "West " |
Exercise
Write a formula that returns the average Amount for East rows where the Product is "Monitor". Show "None" if there are no matching rows.
Show answer
=IFERROR(AVERAGEIFS(D2:D100, B2:B100, "East", C2:C100, "Monitor"), "None")
Exercise
A colleague's formula =AVERAGEIFS(D2:D100, B2:B99, "West") returns #VALUE!. What is wrong, and how do you fix it?
Show answer
The criteria range B2:B99 has 98 rows, but the average range D2:D100 has 99 rows. All ranges must be the same size. Change B2:B99 to B2:B100.