## Count unique distinct values in a filtered Excel defined Table

This article demonstrates a formula that counts unique distinct values filtered from an Excel defined Table.

Debra Dalgleish described in an article how to create a Line Between Dates in Filtered List. She modified a formula by Laurent Longre found here: Excel Experts E-letter from John Walkenbach's web site.

I remember a post I did about extracting unique distinct values from a filtered table (udf and array formula that also was inspired by Laurent Longre's formula.

The Excel defined Table shown in the image above is named Table2. There are forenames in column A. The table is filtered with "Finland" and "Mexico" in column C, the following array formula counts unique distinct values based on a filter applied to an Excel defined Table.

### What you will learn in this article

- Build a formula that counts unique distinct values in a filtered table using a modified version of Laurent Longre's formula.
- Identify filtered values using the SUBTOTAL and OFFSET function.
- How to reference a column in an Excel defined Table, in a formula.
- Convert boolean values to numerical equivalents
- Convert text values to unique numbers based on the order as if they had been sorted from A to Z.
- Count unique distinct numbers

Array formula in A26:

### How to create an array formula

- Copy above array formula
- Double click cell A26 to see the prompt.
- Paste array formula to cell.
- Press and hold Ctrl + Shift simultaneously.
- Press Enter once.
- Release all keys.

### Explaining array formula in cell A26

*Step 1 - Create an array of ranges*

When you use an array of numbers in OFFSETs second argument an array of ranges is returned, this makes it possible to evaluate each value using the SUBTOTAL function.

The MATCH function creates an array of numbers in sequence from 1 to as many as there are values in column 'First Name' in Excel defined Table Table2.

MATCH(ROW(Table2[First Name]), ROW(Table2[First Name]))

becomes

MATCH({2; 3; 4; 5; 6; 7; 8; 9; 10; 11; 12; 13; 14; 15; 16; 17; 18; 19; 20; 21}, {2; 3; 4; 5; 6; 7; 8; 9; 10; 11; 12; 13; 14; 15; 16; 17; 18; 19; 20; 21})

and returns

{1; 2; 3; 4; 5; 6; 7; 8; 9; 10; 11; 12; 13; 14; 15; 16; 17; 18; 19; 20}

OFFSET(Table2[First Name], MATCH(ROW(Table2[First Name]), ROW(Table2[First Name]))-1, 0, 1)

becomes

OFFSET(Table2[First Name], {0; 1; 2; 3; 4; 5; 6; 7; 8; 9; 10; 11; 12; 13; 14; 15; 16; 17; 18; 19}, 0, 1)

and returns (if you press F9 in formula bar)

{"Fraser"; "Kaya"; "Horace"; "Kelton"; "Linnie"; "Jui"; "Mckinley"; "Emmit"; "Siena"; "Cyril"; "Tiara"; "Charley"; "Kaya"; "Fraser"; "Fraser"; "Lynna"; "Jui"; "Lynna"; "Damien"; "Spencer"}

or returns an array of #VALUE errors if you use the "Evaluate formula" feature found on tab "Formula" on the ribbon.

**Step 2 - Find visible values**

The SUBTOTAL function is capable of identifying filtered values if you use the OFFSET function demonstrated in the previous step.

SUBTOTAL(3, OFFSET(Table2[First Name], MATCH(ROW(Table2[First Name]), ROW(Table2[First Name]))-1, 0, 1))

becomes

SUBTOTAL(3, {"Fraser"; "Kaya"; "Horace"; "Kelton"; "Linnie"; "Jui"; "Mckinley"; "Emmit"; "Siena"; "Cyril"; "Tiara"; "Charley"; "Kaya"; "Fraser"; "Fraser"; "Lynna"; "Jui"; "Lynna"; "Damien"; "Spencer"})

and returns the following array:

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

0 (zero) is a hidden value and 1 is a visible value in the Excel defined Table.

**Step 3 - Calculate rank by alphabetical order**

The COUNTIF function lets you assign a unique number to all values based on the order if they were sorted from A to Z or vice versa. Simply append a less than or greater than character to the second argument in the COUNTIF function.

This step is necessary in order to count unique distinct values, duplicates will have the same number assigned to them which is handy in this case.

COUNTIF(Table2[First Name], "<"&Table2[First Name])

returns

{18; 2; 14; 8; 14; 4; 4; 10; 0; 19; 1; 17; 3; 16; 8; 13; 10; 7; 10; 4}

**Step 4 - Filter visible value's alphabetical rank**

The IF function replaces filtered values with the numbers calculated in the previous step.

IF(SUBTOTAL(3, OFFSET(Table2[First Name], MATCH(ROW(Table2[First Name]), ROW(Table2[First Name]))-1, 0, 1)), COUNTIF(Table2[First Name], "<"&Table2[First Name]), "")

becomes

IF({0; 1; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 1; 1; 1; 0; 1; 0; 0; 0}, {18; 2; 14; 8; 14; 4; 4; 10; 0; 19; 1; 17; 3; 16; 8; 13; 10; 7; 10; 4}, "")

and returns

{""; ""; ""; 8; ""; 4; 4; 10; ""; ""; ""; ""; ""; ""; ""; ""; ""; ""; 10; ""}

**Step 5 - Calculate frequency**

The FREQUENCY function counts each number in the array and returns the total at the same relative psoition as the first occuring number.

FREQUENCY(IF(SUBTOTAL(3, OFFSET(Table2[First Name], MATCH(ROW(Table2[First Name]), ROW(Table2[First Name]))-1, 0, 1)), COUNTIF(Table2[First Name], "<"&Table2[First Name]), ""), COUNTIF(Table2[First Name], "<"&Table2[First Name]))

becomes

FREQUENCY({""; ""; ""; 8; ""; 4; 4; 10; ""; ""; ""; ""; ""; ""; ""; ""; ""; ""; 10; ""},{18; 2; 14; 8; 14; 4; 4; 10; 0; 19; 1; 17; 3; 16; 8; 13; 10; 7; 10; 4})

and returns

{0, 0, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

**Step 6 - Sum values larger than zero**

The SUM function adds all values if they are larger than 0 (zero), first we need to check if the total is larger than zero, TRUE or FALSE are returned. Then we need to convert the boolean values to their numerical equivalent so the SUM function will be able to add numbers and return a total.

SUM(--(FREQUENCY(IF(SUBTOTAL(3, OFFSET(Table2[First Name], MATCH(ROW(Table2[First Name]), ROW(Table2[First Name]))-1, 0, 1)), COUNTIF(Table2[First Name], "<"&Table2[First Name]), ""), COUNTIF(Table2[First Name], "<"&Table2[First Name]))>0))

becomes

SUM(--({0, 0, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}>0))

becomes

SUM(--({FALSE; FALSE; FALSE; TRUE; FALSE; TRUE; FALSE; TRUE; FALSE; FALSE; FALSE; FALSE; FALSE; FALSE; FALSE; FALSE; FALSE; FALSE; FALSE; FALSE; FALSE}))

becomes

SUM({0;0;0;1;0;1;0;1;0;0;0;0;0;0;0;0;0;0;0;0;0})

and returns 3 in cell A26. Kaya, Fraiser and Jui are the unique distinct names and they are three which is correct.

Count unique distinct values that meet multiple criteria

This post demonstrates how to build an array formula that counts unique distinct values based on criteria. What's on this [โฆ]

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

Count cells between specified values

This article demonstrates formulas that calculate the number of cells between two values, the first scenario involves two search values [โฆ]

Count unique distinct values in two columns

Formula in C12: =SUM(1/COUNTIF($B$3:$B$8, $B$3:$B$8))+SUM(IF(COUNTIF($B$3:$B$8, $D$3:$D$8)=0, 1/COUNTIF($D$3:$D$8, $D$3:$D$8), 0)) How to create an array formula Double click on cell C12 [โฆ]

How to use an Excel Table name in Data Validation Lists and Conditional Formatting formulas

This article demonstrates different ways to reference an Excel defined Table in a drop-down list and Conditional Formatting. There are [โฆ]

Extract unique distinct values from a filtered Excel defined Table [UDF and Formula]

Robert Jr asks: Oscar, I am using the VBA code & FilterUniqueSort array to generate unique lists that drive Selection [โฆ]

Populate drop down list with filtered Excel Table values

This article demonstrates how to populate a drop down list with filtered values from an Excel defined Table. The animated [โฆ]

Remove common records between two data sets

This article demonstrates how to filter records occurring in only one out of two Excel defined tables. It also shows [โฆ]

### 11 Responses to โCount unique distinct values in a filtered Excel defined Tableโ

### 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

Thanks so very much.. you saved my day with your wisdom :-)

grz from holland

Louis,

Thank you for commenting!

[...] Count unique distinct values in a filtered table [...]

This is great but I have a problem. I am counting a field that has text and numbers (stored as text). The formula is treating the numbers stored as text like they are the same. For example, if my values are a1,b1,1,2 the formula returns 3. Do you know how to fix this?

Jamie, the formula counts visible unique distinct values in a single column. Can you explain in greater detail?

Sure...in your example you have the first name Kaya twice. If you change the first Kaya to a "1" and the second instance to a "2" so that your visible cells are 1,2,Fraser, Fraser, Jui the count is 4 as it should be UNTIL you filter by names instead of country. Filter by names and select the 1, the 2, and one of the other names and you will see the count off. (For me, it doesn't matter what column I filter by though...if there are numbers in my results then the counts are off.)

This doesn't seem to work in Excel for Mac 2011 version 14.4.1, see screenshot at https://postimg.org/image/cotsj3mhr/

Using OS X Mavericks.

Any idea why?

Most Most Power Full Farmula

Ajay Kumar

I am happy you find it useful.

This is the first formula that I have found that does a unique count using Table references. Thank you!

Unfortunately it doesn't quite meet my use case.

I need to do exactly this, but instead of filtering the table, I need to feed the formula a value (either "text" hard-coded in the formula or by reference from a cell value)

Any change you have a solution to this?

Casey Missal

I need to do exactly this, but instead of filtering the table, I need to feed the formula a value (either "text" hard-coded in the formula or by reference from a cell value)Can you provide an example and the desired output?