Count unique values with a condition
Updated · Checked on a live sheet · Excel and Google Sheets
The formula
=COUNTA(UNIQUE(FILTER(D2:D13, E2:E13=I1))) counts the different agents who handled a ticket with the priority in I1. FILTER keeps the matching rows, UNIQUE removes repeats, COUNTA counts what's left. It needs Excel 2021 or later, Microsoft 365 or Google Sheets.
In general: =COUNTA(UNIQUE(FILTER(values, condition)))
Try it on a live sheet
| A | B | C | D | E | F | G | H | I | |
|---|---|---|---|---|---|---|---|---|---|
| 1 | Ticket | Opened | Closed | Agent | Priority | Rating | Priority | High | |
| 2 | T-101 | 1 Apr 2026 | 2 Apr 2026 | Mara | High | 5 | Agents | 3 | |
| 3 | T-102 | 1 Apr 2026 | 6 Apr 2026 | Jonas | Low | 3 | |||
| 4 | T-103 | 2 Apr 2026 | 2 Apr 2026 | Priya | High | 4 | |||
| 5 | T-104 | 3 Apr 2026 | 9 Apr 2026 | Mara | Medium | 2 | |||
| 6 | T-105 | 3 Apr 2026 | Jonas | High | |||||
| 7 | T-106 | 6 Apr 2026 | 7 Apr 2026 | Priya | Low | 5 | |||
| 8 | T-107 | 7 Apr 2026 | 10 Apr 2026 | Mara | Medium | 4 | |||
| 9 | T-108 | 8 Apr 2026 | 8 Apr 2026 | Jonas | High | 5 | |||
| 10 | T-109 | 9 Apr 2026 | 15 Apr 2026 | Priya | Medium | 3 | |||
| 11 | T-110 | 10 Apr 2026 | Mara | Low | |||||
| 12 | T-111 | 13 Apr 2026 | 14 Apr 2026 | Jonas | Medium | 4 | |||
| 13 | T-112 | 14 Apr 2026 | 16 Apr 2026 | Priya | High | 5 |
I2 shows 3: Mara, Priya and Jonas each handled at least one High priority ticket, however many each one handled.
Why it works
Counts the cells in each different value of the rows of D2:D13 where E2:E13 is equal to I1 that aren't empty.
- 1. FILTER: Keeps the rows of
D2:D13whereE2:E13is equal toI1, and spills them into the cells below. If nothing matches, Excel shows#CALC!. - 2. UNIQUE: Lists each different value in the result of step 1 once, and spills the list.
- 3. COUNTA: Counts the cells in the result of step 2 that aren't empty.
In any Excel version: SUMPRODUCT and COUNTIFS
Give each matching row 1 divided by how often its agent appears among matching rows, so each agent adds up to exactly 1: SUMPRODUCT((E2:E13=I1)/COUNTIFS(E2:E13, E2:E13, D2:D13, D2:D13)). It works without dynamic arrays and gives the same 3.
=SUMPRODUCT((E2:E13=I1)/COUNTIFS(E2:E13,E2:E13,D2:D13,D2:D13))Two conditions
Multiply conditions inside FILTER: COUNTA(UNIQUE(FILTER(D2:D13, (E2:E13="High")*(F2:F13=5)))) counts agents with a five-star High priority ticket.
=COUNTA(UNIQUE(FILTER(D2:D13,(E2:E13="High")*(F2:F13=5))))When you need it
“How many different customers bought this product?”, “how many agents worked on high-priority tickets?”, “how many stores sold something this week?”: distinct counts with a condition answer questions that a plain COUNTIFS can't, because COUNTIFS counts rows, not different values. They're common in dashboards and in job tests. Clean the values first (TRIM, consistent capitals), because “Mara” and “Mara ” count as two, and decide what an empty result should show.
In Google Sheets
Google Sheets has FILTER, UNIQUE and COUNTA, so the main formula works there. Sheets' FILTER returns #N/A when nothing matches, and COUNTA counts that error as 1; wrap it in IFNA if no match is possible.
Common mistakes
No match at all: in Excel, FILTER returns #CALC! and COUNTA counts the error as one value. Give FILTER a third argument and subtract, or check with COUNTIF first.
Spaces or different capitals making the same name look like two values: clean with TRIM first.
Using COUNTIF instead of COUNTIFS in the SUMPRODUCT version, which counts repeats across all rows, not just matching ones.
Sources
Official documentation the facts on this page were checked against.