How-to recipes · Counting and summing
How to count unique values in Excel
Count how many different values a list contains with UNIQUE and COUNTA, or with SUMPRODUCT and COUNTIF in Excel 2016 and 2019.
You have a column of customer names taken from an order list, and the same customer appears many times because they ordered more than once. Your manager asks a simple question: how many different customers did we sell to? Counting the rows gives the number of orders, not customers. What you need is the number of distinct values, where every name counts once no matter how often it repeats. This recipe shows the quickest way in Microsoft 365, a formula that works in Excel 2016 and 2019, and the traps that make the count wrong.
The example data
The orders are in A1:A9 with a header in A1. Alpha Ltd ordered three times, Brightway twice, and the other three customers once each, so the answer we expect is 5.
Microsoft 365 and Excel 2021: UNIQUE + COUNTA
UNIQUE returns the list without repeats, and COUNTA counts how many items that list holds. Put them together in any empty cell:
| A | B | C | |
|---|---|---|---|
| 1 | Customer | ||
| 2 | Alpha Ltd | 5 | |
| 3 | Brightway | ||
| 4 | Alpha Ltd | ||
| 5 | Cedar Co | ||
| 6 | Brightway | ||
| 7 | Delta Inc | ||
| 8 | Alpha Ltd | ||
| 9 | Evergreen |
UNIQUE produces Alpha Ltd, Brightway, Cedar Co, Delta Inc and Evergreen in memory; COUNTA counts those five items.
Result: 5
You can also write =ROWS(UNIQUE(A2:A9)). ROWS counts the rows of the array instead of the non-empty items, which gives the same answer here. If you want to see the list as well as the count, type =UNIQUE(A2:A9) in one cell so it spills down, and =COUNTA(C2#) next to it, where C2# means the whole spilled range.
Distinct versus unique
People use the word unique for two different questions. Distinct means every different value once (the answer above, 5). Unique in the strict sense means values that appear exactly once. UNIQUE can answer the second question too through its third argument, exactly_once:
| A | B | C | |
|---|---|---|---|
| 1 | Customer | ||
| 2 | Alpha Ltd | 3 | |
| 3 | Brightway | ||
| 4 | Alpha Ltd | ||
| 5 | Cedar Co | ||
| 6 | Brightway | ||
| 7 | Delta Inc | ||
| 8 | Alpha Ltd | ||
| 9 | Evergreen |
Only Cedar Co, Delta Inc and Evergreen occur a single time, so the result is 3.
Result: 3
Excel 2016 and 2019: SUMPRODUCT + COUNTIF
Older versions have no UNIQUE function, but a classic formula does the same job without any helper columns:
| A | B | C | |
|---|---|---|---|
| 1 | Customer | ||
| 2 | Alpha Ltd | 5 | |
| 3 | Brightway | ||
| 4 | Alpha Ltd | ||
| 5 | Cedar Co | ||
| 6 | Brightway | ||
| 7 | Delta Inc | ||
| 8 | Alpha Ltd | ||
| 9 | Evergreen |
Works in every version of Excel since 2007.
Result: 5
How it works, step by step:
COUNTIF(A2:A9,A2:A9)asks, for every row, how many times that row's value appears in the whole list. The result is the array {3;2;3;1;2;1;3;1}.1/...turns each count into a fraction: each Alpha Ltd row becomes 1/3, each Brightway row 1/2, and the single customers 1.- The fractions of one customer always add up to exactly 1 (three thirds, two halves), so SUMPRODUCT adding everything returns the number of different customers: 5.
SUMPRODUCT is used instead of SUM because it handles arrays without Ctrl+Shift+Enter in older Excel. For the strict appears once count in Excel 2016, use =SUMPRODUCT(--(COUNTIF(A2:A9,A2:A9)=1)), which counts the rows whose value has a count of exactly 1.
Blank cells break both formulas
1/COUNTIF(...) divides by zero for the blanks and the whole SUMPRODUCT returns #DIV/0!. UNIQUE, on the other hand, keeps one blank as a value of its own (shown as 0), so COUNTA counts it and your answer is one too high.Use these versions when the column may contain gaps:
=COUNTA(UNIQUE(FILTER(A2:A100,A2:A100<>"")))=SUMPRODUCT((A2:A100<>"")/COUNTIF(A2:A100,A2:A100&""))&"" stops the division by zero.Other ways to get the count
- PivotTable distinct count. Select the data, Insert > PivotTable, tick Add this data to the Data Model, drag Customer to Values, then Value Field Settings > Distinct Count. Useful when you need the count per region or month. See PivotTables: summarise thousands of rows in seconds.
- Remove Duplicates on a copy. Copy the column, run Data > Remove Duplicates and look at the row count in the status bar. Fine for a one-off, but it does not update. See How to remove duplicates in Excel.
- Distinct count with a condition, for example customers in one region only, is covered in How to count distinct values with criteria in Excel.
Pitfalls
- Both UNIQUE and COUNTIF ignore case, so
alpha ltdandAlpha Ltdcount as one customer. - A trailing space makes
Alpha Ltda different value. Clean the column with TRIM first if the data comes from another system. - Numbers stored as text (
"105") and real numbers (105) are different values for UNIQUE, but COUNTIF treats them as the same. Convert everything to one type before counting. - COUNTIF-based formulas get slow on tens of thousands of rows because every row is compared with every other row. UNIQUE is much faster on big lists.
=COUNTA(UNIQUE(Orders[Customer])), and the count updates automatically when new orders are added. Tables are explained in Excel tables and structured references.Related reading: Dynamic arrays: spill, FILTER, SORT, UNIQUE, SEQUENCE and # explains spilled formulas like UNIQUE, and Counting and summing with criteria: COUNTIFS, SUMIFS, SUMPRODUCT covers COUNTIF and its relatives in depth.
⬇Practice workbookExamples with live formulas + practice sheets · .xlsx · 16 KB