# Count unique distinct values based on a condition

This article demonstrates how to construct a formula that counts unique distinct values based on a condition. The image above demonstrates a formula in cell E12 that counts unique distinct items in column C based on a condition applied to column B.

Unique distinct values are all values except duplicates that are merged into one distinct value.

### Table of Contents

## 1. Count unique distinct values based on a condition

The image above shows a table in columns B and C. Column B contains names and column C contains products. How many unique distinct products did Salesperson Jennifer sell?

The blue arrows show unique distinct products based on salesperson "Jennifer", the total number matches the number in cell E4. The remaining highlighted records are only duplicate values.

Array formula in cell E2:

### 1.1 How to create an array formula

- Copy (Ctrl + c) and paste (Ctrl + v) array formula into formula bar.
- Press and hold Ctrl + Shift.
- Press Enter once.
- Release all keys.

### 1.2 Explaining array formula in cell E2

You can follow along as I explain the formula, select cell E2. Go to tab "Formulas" on the ribbon, press with left mouse button on "Evaluate formula" button.

Press with mouse on "Evaluate" button shown in above image to move to next step.

#### Step 1 - Calculate unique distinct products that Jennifer sold

The COUNTIFS function calculates the number of cells across multiple ranges that equals all given conditions.

COUNTIFS(*criteria_range1*, *criteria1*, [*criteria_range2*, *criteria2*]…)

1/(COUNTIFS($B$5:$B$26, "Jennifer", $C$5:$C$26, $C$5:$C$26))

returns {0,25; 0,5; 0,333333333333333; ... ; 0,333333333333333}

#### Step 2 - Filter Jennifers products

The IF function returns one value if the logical test is TRUE and another value if the logical test is FALSE.

IF(*logical_test*, [*value_if_true*], [*value_if_false*])

IF("Jennifer"=$B$5:$B$26, 1/(COUNTIFS($B$5:$B$26, "Jennifer", $C$5:$C$26, $C$5:$C$26)), 0)

returns {0,25; 0,5; 0,333333333333333; ... ; 0}

#### Step 3 - Sum array

The SUM function allows you to add numbers, the function returns the sum in the cell it is entered in. The SUM function is cleverly designed to ignore text and boolean values, adding only numbers.

SUM(*number1*, [*number2]*, ...)

SUM(IF("Jennifer"=$B$5:$B$26, 1/(COUNTIFS($B$5:$B$26, "Jennifer", $C$5:$C$26, $C$5:$C$26)), 0))

returns 4 in cell E2.

## 2. Count unique distinct values based on a condition - Excel 365

This example demonstrates a smaller formula that works only in Excel 365, two out of three functions are Excel 365 functions.

Dynamic array formula in cell F28:

This formula is entered as a regular formula.

### 2.1 Explaining formula in cell F28

#### Step 1 - Check which values meet the condition specified in cell C26

The equal sign lets you compare value to value, in this case, value to values. The output is an array of boolean values True or False.

B3:B24=C26

returns {TRUE; TRUE; TRUE; ... ; FALSE}

#### Step 2 - Filter values

The FILTER function lets you extract values/rows based on a condition or criteria. It is in the Lookup and reference category and is only available to Excel 365 subscribers.

FILTER(C3:C24,B3:B24=C26)

returns {"Product CC"; "Product AA"; "Product BB"; ... ; "Product CC"}.

#### Step 3 - Extract unique distinct values

The UNIQUE function lets you extract both unique and unique distinct values and also comparing columns to columns or rows to rows.

UNIQUE(FILTER(C3:C24,B3:B24=C26))

returns {"Product CC"; "Product AA"; "Product BB"; "Product DD"}.

#### Step 4 - Return number of rows in array

The ROWS function returns the number of rows based on a cell range or array.

ROWS(UNIQUE(FILTER(C3:C24,B3:B24=C26)))

returns 4.

## 4. How to count unique distinct values based on a date

The array formula in cell D3 calculates the number of unique distinct items based on the given date in column B. Unique distinct values are all values but duplicates are merged into one value.

Example, there are five items on date 1/5/2010 in the table above. 1150, 1126, 1131, 1131 and 1126, however there are only three unique distinct items 1150, 1126 and 1131 and that number is what the formula returned in cell D3.

Excel 365 dynamic array formula in cell D3:

Array formula in D3:

To enter an array formula, type the formula in a cell then press and hold CTRL + SHIFT simultaneously, now press Enter once. Release all keys.

The formula bar now shows the formula with a beginning and ending curly bracket telling you that you entered the formula successfully. Don't enter the curly brackets yourself.

Copy cell D3 and paste it down as far as needed.

### Explaining formula in cell D3

#### Step 1 - Calculate alphabetical rank

The COUNTIF function counts values based on a condition or criteria, in this case I use the ampersand to concatenate a less than sign. This will return a number representing the rank in an alphabetically sorted list.

COUNTIF($C$3:$C$11, "<"&$C$3:$C$11)

returns {6;0;2;2;0;6;6;2;2}

#### Step 2 - Extract numbers based on date

The IF function has three arguments, the first one must be a logical expression. If the expression evaluates to TRUE then one thing happens (argument 2) and if FALSE another thing happens (argument 3).

IF(B3=$B$3:$B$11, COUNTIF($C$3:$C$11, "<"&$C$3:$C$11), "")

returns {6;0;2;2;0;"";"";"";""}.

#### Step 3 - Cacluate frequency

The FREQUENCY function calculates how often values occur in a range.

FREQUENCY(IF(B3=$B$3:$B$11, COUNTIF($C$3:$C$11, "<"&$C$3:$C$11), ""), (COUNTIF($C$3:$C$11, "<"&$C$3:$C$11))

returns {1;2;2;0;0;0;0;0;0;0}

#### Step 4 - Is number larger than 0 (zero)?

The value is unique distinct if the corresponding number in the array is larger than 0 (zero).

FREQUENCY(IF(B3=$B$3:$B$11, COUNTIF($C$3:$C$11, "<"&$C$3:$C$11), ""), (COUNTIF($C$3:$C$11, "<"&$C$3:$C$11))>0

returns {TRUE; TRUE; TRUE; FALSE; FALSE; FALSE; FALSE; FALSE; FALSE; FALSE}

#### Step 5 - Convert boolean values

The SUM function can't sum boolean value so we must convert them into their numerical equivalents.

--(FREQUENCY(IF(B3=$B$3:$B$11, COUNTIF($C$3:$C$11, "<"&$C$3:$C$11), ""), (COUNTIF($C$3:$C$11, "<"&$C$3:$C$11)))>0)

returns {1;1;1;0;0;0;0;0;0;0}

#### Step 6 - Sum numbers

The SUM function adds numbers in the array and returns a total.

SUM(--(FREQUENCY(IF(B3=$B$3:$B$11, COUNTIF($C$3:$C$11, "<"&$C$3:$C$11), ""), (COUNTIF($C$3:$C$11, "<"&$C$3:$C$11)))>0))

becomes SUM({1;1;1;0;0;0;0;0;0;0}) and returns 3.

### Count unique distinct values category

This post demonstrates how to build formulas that counts unique distinct values based on criteria. The image above demonstrates an […]

This article describes how to count unique distinct values. What are unique distinct values? They are all values but duplicates are […]

### Excel categories

### 21 Responses to “Count unique distinct values based on a condition”

### Leave a Reply

### How to comment

**How to add a formula to your comment**

<code>Insert your formula here.</code>

**Convert less than and larger than signs**

Use html character entities instead of less than and larger than signs.

< becomes < and > becomes >

**How to add VBA code to your comment**

[vb 1="vbnet" language=","]

Put your VBA code here.

[/vb]

**How to add a picture to your comment:**

Upload picture to postimage.org or imgur

Paste image link to your comment.

**Contact Oscar**

You can contact me through this contact form

How would I modify this formula to then allow me to filter another row of data?

For example, if there was a yes / no entry in column e, that would then further pair down the entries from 3 to between 3-0. Is it possible to do this with this formula? The formula works great for counting the unique occurrencies based on two columns, but I would like to add a third column requirement that I can move around to then filter information as needed.

Thanks,

Dave

David,

Formula in cell D3:

[…] try this How to count unique distinct occurrences for each date in excel | Get Digital Help - Microsoft Excel… […]

Is there a way to subtotal the unique distinct values on each date with counting each date only once?

[…] https://www.get-digital-help.com/2017/08/28/count-unique-distinct-values-based-on-a-condition/ […]

It seems as the counting formula can be a bit shorter.

{=SUM(((B5:B26="Jennifer"))/COUNTIFS(B5:B26,B5:B26,C5:C26,C5:C26))}

----------------------------

Michael (Micky) Avidan

“Microsoft® Answers" - Wiki author & Forums Moderator

“Microsoft®” Excel MVP – Excel (2009-2018)

ISRAEL

Michael (Micky) Avidan,

Why didn't I think of that.

Thank you, I appreciate your comment.

While failing to apply the above formula on my data, i copied the above example and function and the result i get is 1.

What am i doing wrong?

Thanos,

Did you enter the formula as an array formula?

Are there curly brackets around your formula in the formula bar?

Like this:

{=SUM(--(FREQUENCY(IF(B3=$B$3:$B$11, COUNTIF($C$3:$C$11, "<"&$C$3:$C$11), ""), (COUNTIF($C$3:$C$11, "<"&$C$3:$C$11)))>0))}

Don't enter those characters yourself, they appear if you successfully entered the array formula.

Just as done by one day. How can it be done with number of days. I want to get unique numbers whose days difference is 30.

Mickey,

and faster too. Amazing! Can you explain how it works?

Ed

i recreate your data to my excel sheet, and using your formula. why the result different with your result. mine give result div 0, and if i change the range it give result #value

thx

Andry

There are many things that could be wrong.

1. Did you enter the formula as an array formula?

2. Are the cell references correct?

Thank you very much. It helps my problem. :)

If I want to skip blank field for count, how would I do that?

May I know , what if I want to ignored "BLANKS" for unique count?

Does anyone know a way to apply this to a Google Sheet? It's exactly what I need!

Hi,

I'm just curious, could we use SUMIF/SUMIFS instead?

Ichsan,

The SUMIF and SUMIFS functions requires cell references in the sum_range arguments. It is, as far as I know, not possible to use these functions to perform the calculations demonstrated in this article.

SUMIF(range, criteria, [sum_range])

SUMIFS(sum_range, criteria_range1, criteria1, [criteria_range2], [criteria2], ...)

Try these:

=SUMIF($B$5:$B$26,"Jennifer",1/(COUNTIFS($B$5:$B$26,"Jennifer",$C$5:$C$26,$C$5:$C$26)))

=SUMIFS(1/(COUNTIFS($B$5:$B$26,"Jennifer",$C$5:$C$26,$C$5:$C$26)),$B$5:$B$26,"Jennifer")

They will return an error dialog box.

Hi,

thanks for your reply.

is it possible if we use IF first, then SUM?

Ichsan,

We need to create an array before we calculate a sum, I don't think it is possible.

Why do you need IF first and then SUM?