Count unique values in Excel
In Excel 365, =COUNTA(UNIQUE(A2:A21)) counts how many different values are in A2:A21. UNIQUE makes a list with each value once, and COUNTA counts that list. In Excel 2019 and older, use =SUMPRODUCT(1/COUNTIF(A2:A21,A2:A21)). UNIQUE works in Excel 365, Excel 2021 and later and Excel for the web, on Windows and Mac; the SUMPRODUCT formula works in every version.
Counting unique values at a glance
| You want to count | Excel 365 and 2021 | Excel 2019 and older |
|---|---|---|
| Different values | =COUNTA(UNIQUE(A2:A9)) | =SUMPRODUCT(1/COUNTIF(A2:A9,A2:A9)) |
| Different values, with blanks in the list | =COUNTA(UNIQUE(FILTER(A2:A9,A2:A9<>""))) | =SUMPRODUCT((A2:A9<>"")/COUNTIF(A2:A9,A2:A9&"")) |
| Values that appear only once | =COUNTA(UNIQUE(A2:A9,,TRUE)) | =SUMPRODUCT(--(COUNTIF(A2:A9,A2:A9)=1)) |
| Different values in one group | =COUNTA(UNIQUE(FILTER(A2:A9,B2:B9="North"))) | =SUMPRODUCT((B2:B9="North")/COUNTIFS(A2:A9,A2:A9,B2:B9,B2:B9)) |
| Different pairs across two columns | =ROWS(UNIQUE(A2:B9)) | Join the columns in a helper column first |
Each one is worked through below.
A worked example
A list of 8 orders has some customers more than once. How many different customers are there?
By hand, you'd write each customer down the first time you see them: Harbour, Ashby, Kestrel, Northgate. Ashby, Harbour and Kestrel come up again, but they're already on your list. That's 4 different customers.
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Customer | Each one once | How many | |
| 2 | Harbour | Harbour | 4 | |
| 3 | Ashby | Ashby | ||
| 4 | Kestrel | Kestrel | ||
| 5 | Ashby | Northgate | ||
| 6 | Northgate | |||
| 7 | Harbour | |||
| 8 | Kestrel | |||
| 9 | Harbour |
=COUNTA(UNIQUE(A2:A9))
You don't need the list in column C to get the count: the formula in D2 does both steps at once. There's more on UNIQUE in UNIQUE and FILTER.
In older Excel: SUMPRODUCT and COUNTIF
Excel 2019 and older don't have UNIQUE. This formula gives the same 4:
=SUMPRODUCT(1/COUNTIF(A2:A9,A2:A9))
Here's how it works. COUNTIF counts how many times each row's customer appears: Harbour 3 times, so each Harbour row gets 1/3. Three thirds add up to 1. Ashby appears twice, so each Ashby row gets 1/2, and two halves make 1. Every customer adds up to exactly 1, however many times they appear, so the total is the number of different customers. Here are the steps laid out in columns:
| A | B | C | D | E | |
|---|---|---|---|---|---|
| 1 | Customer | Times listed | Share | ||
| 2 | Harbour | 3 | 0.333 | Total | 4 |
| 3 | Ashby | 2 | 0.5 | One formula | 4 |
| 4 | Kestrel | 2 | 0.5 | ||
| 5 | Ashby | 2 | 0.5 | ||
| 6 | Northgate | 1 | 1 | ||
| 7 | Harbour | 3 | 0.333 | ||
| 8 | Kestrel | 2 | 0.5 | ||
| 9 | Harbour | 3 | 0.333 |
SUMPRODUCT is there so that older Excel works out COUNTIF for every row at once. It works without pressing any special keys. On long lists of thousands of rows it can be slow, because COUNTIF runs once for every row.
Leaving out the blanks
If the list has empty cells, UNIQUE counts "empty" as one more value, so the answer is 1 too high. Here the list has 4 different customers and 2 gaps, so =COUNTA(UNIQUE(A2:A9)) gives 5 (D2 on the sheet below). Filter the blanks out first:
=COUNTA(UNIQUE(FILTER(A2:A9,A2:A9<>"")))
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Customer | Counting | Answer | |
| 2 | Harbour | Blanks left in | 5 | |
| 3 | Ashby | Blanks filtered out | 4 | |
| 4 | Older Excel | 4 | ||
| 5 | Kestrel | |||
| 6 | Northgate | |||
| 7 | Harbour | |||
| 8 | ||||
| 9 | Ashby |
The plain SUMPRODUCT formula gives #DIV/0! when there's a blank, because COUNTIF finds no empty cells to match. In the older Excel version, &"" fixes that, and (A2:A9<>"") gives each blank row a share of 0.
Different values, or values that appear once?
"Unique" can mean two things. "How many different customers?" is 4 in the worked example. "How many customers ordered only once?" is 1, because only Northgate appears once. Some people call the first one "distinct". For the second, add TRUE as UNIQUE's third part:
=COUNTA(UNIQUE(A2:A9,,TRUE))
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Customer | Counting | Answer | |
| 2 | Harbour | Different customers | 4 | |
| 3 | Ashby | Ordered only once | 1 | |
| 4 | Kestrel | Ordered more than once | 3 | |
| 5 | Ashby | |||
| 6 | Northgate | |||
| 7 | Harbour | |||
| 8 | Kestrel | |||
| 9 | Harbour |
The two commas leave UNIQUE's middle part empty: that part is for going across columns, which you don't need here. Taking one count from the other gives the customers who came back, 3. To see which ones are repeated, see finding duplicates.
Different values in one group
To count different values that match a condition, such as different customers in the North, filter first. The North orders are from Harbour (three times), Kestrel and Northgate, so 3 different customers.
| A | B | C | D | E | |
|---|---|---|---|---|---|
| 1 | Customer | Region | Region | Customers | |
| 2 | Harbour | North | North | 3 | |
| 3 | Ashby | South | South | 2 | |
| 4 | Kestrel | North | |||
| 5 | Ashby | South | |||
| 6 | Northgate | North | |||
| 7 | Harbour | North | |||
| 8 | Kestrel | South | |||
| 9 | Harbour | North |
Kestrel ordered in both regions, so it counts once in each. In older Excel, =SUMPRODUCT((B2:B9="North")/COUNTIFS(A2:A9,A2:A9,B2:B9,B2:B9)) gives the same 3. If no row matches, FILTER gives #CALC!. Wrapping the count as =IFERROR(ROWS(UNIQUE(FILTER(A2:A9,B2:B9="West"))),0) shows 0 instead. Giving FILTER a third part, "", doesn't help here, because COUNTA counts that empty text as 1.
Common mistakes
The count is one too high
There's a blank in the range, or the heading is included. Start at row 2 and filter out the blanks, as above.
The same name counted twice
"Ashby" and "Ashby " (with a space on the end) are different to Excel. Tidy the list with TRIM first. Capitals don't matter: UNIQUE treats ASHBY and Ashby as the same.
#NAME? in older Excel
UNIQUE isn't in Excel 2019 or older, so the formula shows #NAME?. Use the SUMPRODUCT formula.
COUNTA on its own
=COUNTA(A2:A9) counts every order, 8, so a customer who ordered three times counts 3 times. Wrap the range in UNIQUE first. For more on COUNTA and the other counting functions, see counting cells with text.
Try it now
Some customers have ordered more than once. In H1, work out how many different customer IDs there are in A2:A21.
| A | B | C | D | E | F | G | H | |
|---|---|---|---|---|---|---|---|---|
| 1 | Customer ID | Amount (£) | Different customers | |||||
| 2 | 1038 | 160 | ||||||
| 3 | 1037 | 33 | ||||||
| 4 | 1042 | 118 | ||||||
| 5 | 1047 | 144 | ||||||
| 6 | 1039 | 148 | ||||||
| 7 | 1032 | 53 | ||||||
| 8 | 1032 | 60 | ||||||
| 9 | 1043 | 151 | ||||||
| 10 | 1031 | 259 | ||||||
| 11 | 1037 | 126 | ||||||
| 12 | 1037 | 86 | ||||||
| 13 | 1046 | 172 | ||||||
| 14 | 1044 | 95 | ||||||
| 15 | 1042 | 210 | ||||||
| 16 | 1038 | 47 | ||||||
| 17 | 1031 | 118 | ||||||
| 18 | 1047 | 66 | ||||||
| 19 | 1050 | 240 | ||||||
| 20 | 1039 | 81 | ||||||
| 21 | 1032 | 129 |
Open this in Try a formula: the same sheet with the answer filled in, to change or share by link.
Part of Excel data cleaning practice: every lesson on the topic, with a project in Excel.
Where it works
UNIQUE and FILTER work in Excel 365, Excel 2021 and later, and Excel for the web, on Windows and Mac. SUMPRODUCT and COUNTIF work in every version of Excel, and COUNTIFS in Excel 2007 and later.
Questions
How do I count unique values with a condition?
Filter the list first, then count the different values in what is left: =COUNTA(UNIQUE(FILTER(A2:A50,B2:B50="North"))) counts the different customers in the North. If nothing might match, use =IFERROR(ROWS(UNIQUE(FILTER(A2:A50,B2:B50="North"))),0), which shows 0 in place of the #CALC! error. In Excel 2019 and older, use =SUMPRODUCT((B2:B50="North")/COUNTIFS(A2:A50,A2:A50,B2:B50,B2:B50)), which gives the same count without UNIQUE or FILTER.
How do I count unique values in Excel without the UNIQUE function?
Use =SUMPRODUCT(1/COUNTIF(A2:A50,A2:A50)). COUNTIF counts how many times each row's value appears, so a value listed three times gives each of its rows a third. The thirds add up to 1, so every different value adds exactly 1 to the total. It works in every version of Excel. If the list has blanks, use the version with &"" shown above.
What's the difference between distinct and unique values?
Distinct values are every different value, each counted once: in Harbour, Ashby, Harbour there are 2. Unique values sometimes means the ones that appear exactly once: there, only Ashby, so 1. Excel's UNIQUE gives the distinct values unless you add TRUE as its third part. =COUNTA(UNIQUE(A2:A50)) counts distinct values and =COUNTA(UNIQUE(A2:A50,,TRUE)) counts the ones that appear once.
How do I count unique values and ignore blanks?
Filter out the empty cells before UNIQUE: =COUNTA(UNIQUE(FILTER(A2:A50,A2:A50<>""))). Without the filter, UNIQUE keeps one empty value, so the count is 1 too high. In older Excel, use =SUMPRODUCT((A2:A50<>"")/COUNTIF(A2:A50,A2:A50&"")). A cell holding only a space isn't blank to Excel, so tidy the list with TRIM first if there may be stray spaces.