# 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 press with left mouse button on 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]))

returns {1; 2; ...; 20}

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

returns (if you press F9 in formula bar)

{"Fraser"; "Kaya"; ... ; "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))

returns the following array: {0; 1; ... ; 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; ... ; 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]), "")

returns {""; ""; ...; ""}

**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]))

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

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

### Count 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 […]

This article explains how to count cells highlighted with Conditional Formatting (CF). The image above shows data in cell range […]

### Table category

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

This article demonstrates two formulas that extract distinct values from a filtered Excel Table, one formula for Excel 365 subscribers […]

The image above demonstrates a macro linked to a button. Press with left mouse button on the button and the […]

## How to use Excel Tables

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

You absolutely nailed it. I spent day trying to figure this out then I found your solution. Thanks a lot!