How-to recipes · Counting and summing

How to count distinct values with criteria in Excel

Count how many different customers, products or names meet a condition, with FILTER and UNIQUE or a SUMPRODUCT formula for Excel 2016.

Intermediate UNIQUE FILTER ROWS IFERROR SUMPRODUCT COUNTIFS

Counting the different values in a whole column is easy (see How to count unique values in Excel). The harder and more common question adds a condition: how many different customers bought in the North region? or how many different products did each sales rep sell this month? A plain COUNTIFS counts rows, so a customer with four orders counts four times. You need to filter the rows first and then count each remaining value once.

The example data

A small sales list with region, customer and amount in A1:C8. The region we want is typed in F2, so the formulas can be reused for any region. In the North, Alpha Ltd appears twice and Cedar Co twice, so there are 5 North rows but only 3 different customers.

Microsoft 365: FILTER, UNIQUE and ROWS

Read the formula from the inside out: FILTER keeps only the customers whose region is North, UNIQUE removes the repeats, and ROWS counts what is left.

F3fx=IFERROR(ROWS(UNIQUE(FILTER(B2:B8,A2:A8=F2))),0)
ABCDEF
1RegionCustomerAmountRegion
2NorthAlpha Ltd1200North
3SouthBrightway4503
4NorthCedar Co300
5NorthAlpha Ltd800
6SouthAlpha Ltd950
7NorthDelta Inc620
8NorthCedar Co150

FILTER returns Alpha Ltd, Cedar Co, Alpha Ltd, Delta Inc, Cedar Co; UNIQUE reduces that to three names.

Result: 3

Why IFERROR? When no row matches, FILTER returns the #CALC! error (an empty result). Without IFERROR you would see the error instead of 0.

Watch out. Do not use COUNTA around FILTER here. If nothing matches, COUNTA(UNIQUE(FILTER(...))) returns 1, because COUNTA counts the error value itself as one non-empty item. The same happens if you give FILTER an if_empty text such as "". ROWS inside IFERROR does not have that problem.

Two or more conditions

Multiply the conditions inside FILTER. Each comparison gives TRUE/FALSE, and multiplying them works like AND. To count the North customers with at least one order of 500 or more:

Formula
=IFERROR(ROWS(UNIQUE(FILTER(B2:B8,(A2:A8=F2)*(C2:C8>=500)))),0)

Only Alpha Ltd (1200 and 800) and Delta Inc (620) qualify, so the result is 2. For OR logic add the conditions instead: (A2:A8="North")+(A2:A8="South").

Excel 2016 and 2019: SUMPRODUCT and COUNTIFS

Without FILTER and UNIQUE, use the fraction trick. Every matching row contributes 1 divided by the number of matching rows for the same customer, so each customer adds up to exactly 1:

F3fx=SUMPRODUCT((A2:A8=F2)/COUNTIFS(A2:A8,A2:A8,B2:B8,B2:B8))
ABCDEF
1RegionCustomerAmountRegion
2NorthAlpha Ltd1200North
3SouthBrightway4503
4NorthCedar Co300
5NorthAlpha Ltd800
6SouthAlpha Ltd950
7NorthDelta Inc620
8NorthCedar Co150

Alpha Ltd in the North contributes 1/2 + 1/2, Cedar Co 1/2 + 1/2, Delta Inc 1.

Result: 3

  • (A2:A8=F2) is 1 for North rows and 0 for the others, so other regions add nothing.
  • COUNTIFS(A2:A8,A2:A8,B2:B8,B2:B8) counts, for every row, how many rows have the same region and the same customer. Alpha Ltd in the South has its own count of 1 and does not disturb the North count.
  • SUMPRODUCT adds the fractions without Ctrl+Shift+Enter.

Adding a number condition is trickier, because rows that fail the condition can get a count of 0 and cause #DIV/0!. Add the failing test to the denominator so it never drops to zero:

F3fx=SUMPRODUCT((A2:A8=F2)*(C2:C8>=500)/(COUNTIFS(A2:A8,A2:A8,B2:B8,B2:B8,C2:C8,">=500")+(C2:C8<500)))
ABCDEF
1RegionCustomerAmountRegion
2NorthAlpha Ltd1200North
3SouthBrightway4502
4NorthCedar Co300
5NorthAlpha Ltd800
6SouthAlpha Ltd950
7NorthDelta Inc620
8NorthCedar Co150

Rows under 500 get a numerator of 0 and a denominator of at least 1, so they add nothing and cause no error.

Result: 2

A PivotTable alternative

If you need the distinct count for every region at once, a PivotTable is often simpler than formulas. Select the data, choose Insert > PivotTable, tick Add this data to the Data Model, put Region in Rows and Customer in Values, then open Value Field Settings and choose Distinct Count. The Data Model option is required: a normal PivotTable only offers Count, which counts rows. More in PivotTables: summarise thousands of rows in seconds and Power Pivot and the Data Model: relationships and DAX measures.

In Microsoft 365 you can also build the per-region table with a formula: put =UNIQUE(A2:A8) in a column and next to it =IFERROR(ROWS(UNIQUE(FILTER($B$2:$B$8,$A$2:$A$8=H2))),0) copied down, where H2 holds the first region.

Pitfalls

  • Blank customer cells are counted as a value by UNIQUE. Add *(B2:B8<>"") to the FILTER condition to exclude them.
  • Text comparisons are not case-sensitive: north matches North.
  • Spaces at the end of a customer name create a second "different" customer. Clean the data with TRIM first, as shown in Text cleanup: TRIM, CLEAN, TEXTSPLIT, Flash Fill and Text to Columns.
  • The SUMPRODUCT version compares every row with every other row. On 50,000 rows it can take seconds to recalculate; the FILTER version is far faster.
Tip. Keep the condition in a cell (F2 here) rather than typing "North" in the formula. Combine it with a drop-down list (How to create a drop-down list in Excel) and you have a small interactive report.

To learn the conditional counting functions in depth, read Counting and summing with criteria: COUNTIFS, SUMIFS, SUMPRODUCT.

⬇Practice workbookExamples with live formulas + practice sheets · .xlsx · 17 KB

More how-to recipes