COUNTIF and SUMIF

Your call with the sales team starts in ten minutes. They expect two numbers from you: how many headphone orders there were and what they add up to. All you have is an export from the CRM, where orders for different products are mixed together:

fx
A
B
C
D
E
F
G
H
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
Product
Amount, $
Headphones, orders
Total, $
Headphones
7200
Watch
3400
Headphones
2600
Speaker
8100
Headphones
5400
Watch
4300
Speaker
1900
Headphones
4800
Watch
1100
Watch
1000
Case
8800
Case
11800
Watch
6800
Case
4300
Headphones
12600
Headphones
5900
Speaker
7700
Watch
12000
Case
2500
Watch
2800
Headphones
2600
Headphones
10300
Cable
12000
Case
3300
Watch
5200
Case
10100
Headphones
900
Headphones
10900
Watch
9700
Watch
8300
Headphones
4300
Cable
4000
Headphones
6700
Headphones
9800
Headphones
2700
Headphones
10200
Case
5500
Headphones
10900
Headphones
9300
Case
5500
Sheet1
Scroll through the export: 40 orders, and headphones show up here and there. Counting these rows by eye takes a long time, and it's easy to lose count.

COUNT and SUM won't help here: they take every row. You need functions that check a condition first and take only the rows that match.

It's easier to see how they work on the first eight orders of the export: rows two through nine.

How many rows match: COUNTIF

COUNTIF has two arguments: the range to check and the condition. This formula counts the headphone orders:

=COUNTIF(A2:A9,"Headphones")

The condition goes in quotes because, for the function, it's text. The function applies it to each cell of the range in turn: is A2 "Headphones"? What about A3? That's why you don't put a cell address in the condition.

The function compares the whole cell: "Wireless headphones" doesn't match the condition "Headphones". It doesn't care about uppercase and lowercase letters.

A condition can also be a comparison. The sign and the number go together, also in quotes: ">5000" means "greater than 5,000". You can use the same signs as in ordinary comparisons, for example "<>Headphones" means "any product except headphones". A condition about the amount is checked against column B, and a condition about the product against column A:

fx
A
B
C
D
E
F
1
2
3
4
5
6
7
8
9
10
11
Product
Amount, $
What we count
Orders
Headphones
7200
Headphones
Watch
3400
Over $5,000
Headphones
2600
Not headphones
Speaker
8100
Headphones
5400
Watch
4300
Speaker
1900
Headphones
4800
Sheet1

What they add up to: SUMIF

SUMIF takes the same two arguments and adds a third: what to add up.

=SUMIF(A2:A9,"Headphones",B2:B9)

The function goes row by row. If column A says "Headphones", it takes the amount from the same row of column B. That's why both ranges must start and end on the same rows.

In the diagram, both functions go through the orders row by row. Switch the condition to "Over 5,000": the same column of amounts is both checked and added up.

A: ProductB: Amount
2Headphones7,200
3Watch3,400
4Headphones2,600
5Speaker8,100
6Headphones5,400
7Watch4,300
8Speaker1,900
9Headphones4,800
checking the condition matched
How many rows matched
=COUNTIF(A2:A9,"Headphones")
0
What is the total
=SUMIF(A2:A9,"Headphones",B2:B9)
$0

AVERAGEIF calculates the average of the matching rows. Its arguments are the same as for SUMIF. For example, this is how you get the average watch order, which comes to $3,850:

=AVERAGEIF(A2:A9,"Watch",B2:B9)

The largest and the smallest order by a condition are found by MAXIFS and MINIFS, but in these functions the column of amounts comes first, and the range with the condition comes after it. For example, =MAXIFS(B2:B9,A2:A9,"Watch") returns the largest watch order, $4,300.

The report for the call

A coworker has already started a report below the orders, but both cells show 0. Fix the formulas:

Fix B11: the formula should count the headphone orders
fx
A
B
C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Product
Amount, $
Headphones
7200
Watch
3400
Headphones
2600
Speaker
8100
Headphones
5400
Watch
4300
Speaker
1900
Headphones
4800
Headphone orders
Headphone revenue, $
Sheet1
B11 shows 0. Click the cell and look at the formula: it looks for "Headphones" in B2:B9, but that range holds order amounts, not product names.

The formulas work on eight rows. Now for the reason you started all this: the whole export. The formulas are the same, only the last row of the ranges changes:

fx
A
B
C
D
E
F
G
H
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
Product
Amount, $
Headphones, orders
Total, $
Headphones
7200
Watch
3400
Headphones
2600
Speaker
8100
Headphones
5400
Watch
4300
Speaker
1900
Headphones
4800
Watch
1100
Watch
1000
Case
8800
Case
11800
Watch
6800
Case
4300
Headphones
12600
Headphones
5900
Speaker
7700
Watch
12000
Case
2500
Watch
2800
Headphones
2600
Headphones
10300
Cable
12000
Case
3300
Watch
5200
Case
10100
Headphones
900
Headphones
10900
Watch
9700
Watch
8300
Headphones
4300
Cable
4000
Headphones
6700
Headphones
9800
Headphones
2700
Headphones
10200
Case
5500
Headphones
10900
Headphones
9300
Case
5500
Sheet1
Type a COUNTIF formula for the whole export into E1, the products are in A2:A41. Then type SUMIF into G1, the amounts are in B2:B41.

A summary for every product

On the call, they may also ask about the other products. You don't need to write a separate formula with a name in quotes for each one: the condition can be a cell where that name is already written. You write the formula once and fill it down:

=SUMIF($A$2:$A$9,D2,$B$2:$B$9)

The ranges are locked with dollar signs so they don't shift when you fill the formula down. D2 has no dollar signs: one row down, the condition becomes D3, the next product.

fx
A
B
C
D
E
F
1
2
3
4
5
6
7
8
9
10
11
Product
Amount, $
Product
Amount, $
Headphones
7200
Headphones
Watch
3400
Watch
Headphones
2600
Speaker
Speaker
8100
Headphones
5400
Watch
4300
Speaker
1900
Headphones
4800
Sheet1

Before the call, it's worth checking the summary: the SUM of column E must match the SUM of all the orders. If the summary is smaller, the export has a product that's missing from it.