Apr
7

How to count distinct and unique values in Excel

In this tutorial, you will learn how to count unique values in Excel with formulas, and how to get an automatic count of distinct values in a pivot table. We will also discuss a number of formula examples for counting unique names, texts, numbers, cased-sensitive unique values, and more.

When working with a large dataset in Excel, you may often need to know how many duplicate and unique values are there. And sometimes, you may want to count only the distinct (different) values.

If you have been visiting this blog on a regular basic, you already know the Excel formula to count duplicates. And today, we are going to explore different ways to count unique values in Excel. But for the sake of clarity, let's define the terms first.

  • Unique values - these are the values that appear in the list only once.
  • Distinct values - these are all different values in the list, i.e. unique values plus 1st occurrences of duplicate values.

The following screenshot demonstrates the difference:

Unique and distinct values

And now, let's see how you can count unique and distinct values in Excel using formulas and PivotTable features.

How to count unique values in Excel

Here's a common task that all Excel users have to perform once in a while.  You have a list of data and you need to find out the number of unique values in that list. How do you do that? Easier than you may think :) Below you will find a few formulas to count unique values of different types.

Count unique values in a column

Supposing you have a column of names in your Excel worksheet, and you need to count unique names in that column. The solution is to use the SUM function in combination with IF and COUNTIF:

=SUM(IF(COUNTIF(range, range)=1,1,0))

Note. This is an array formula, so be sure to press Ctrl + Shift + Enter to complete it. Once you do it, Excel will automatically enclose the formula in {curly braces} like in the screenshot below.

In this example, we are counting unique names in range A2:A10, so our formula takes the following shape:

=SUM(IF(COUNTIF(A2:A10,A2:A10)=1,1,0))

Counting unique values in Excel

Further on in this tutorial, we are going to discuss a handful of other formulas to count unique values of different types. And because all those formulas are variations of the basic Excel unique values formula, it makes sense to break down the above formula, so you can fully understand how it works and tweak it for your data. If someone is not interested in technicalities, you can skip right to the next formula example.

How the Excel count unique values formula works

As you see, 3 different functions are used in our unique values formula - SUM, IF and COUNTIF. Looking from the inside out, here's what each function does:

  • The COUNTIF function counts how many times each individual value appears in the specified range.

    In this example, COUNTIF(A2:A10,A2:A10) returns the array {1;2;2;1;2;2;2;1;2}.

  • The IF function evaluates each value in the array returned by COUNTIF, keeps all 1's (unique values), and replaces all other values with zeros.

    So, the function IF(COUNTIF(A2:A10,A2:A10)=1,1,0) becomes IF(1;2;2;1;2;2;2;1;2) = 1,1,0, which turns into the array {1;0;0;1;0;0;0;1;0} where 1 is a unique value and 0 is a duplicate value.

  • Finally, the SUM function adds up the values in the array returned by IF and outputs the total number of unique values, which is exactly what we wanted.
Tip. To see what a specific part of your Excel unique values formula evaluates to, select that part in the formula bar and press the F9 key.

Count unique text values in Excel

If your Excel list contains both numerical and text values, and you want to count only unique text values, add the ISTEXT function to the array formula discussed above:

=SUM(IF(ISTEXT(A2:A10)*COUNTIF(A2:A10,A2:A10)=1,1,0))

As you know, the Excel ISTEXT function returns TRUE if an evaluated value is text, FALSE otherwise. Since the asterisk (*) works as the AND operator in array formulas, the IF function returns 1 only if a value is both text and unique, 0 otherwise. And after the SUM function adds up all 1's, you will get a count of unique text values in the specified range.

Don't forget to press Ctrl + Shift + Enter to correctly enter the array formula, and you will get a result similar to this:

Counting unique text values in Excel

As you can see in the screenshot above, the formula returns the total number of unique text values, excluding blank cells, numbers, logical values of TRUE and FALSE, and errors.

Count unique numeric values in Excel

To count unique numbers in a list of data, utilize an array formula like we've just used for counting unique text values, with the only difference that you embed ISNUMBER instead of ISTEXT in your unique values formula:

=SUM(IF(ISNUMBER(A2:A10)*COUNTIF(A2:A10,A2:A10)=1,1,0))

Counting unique numeric values in Excel

Note. Since Microsoft Excel stores dates and times as serial numbers, they are also counted.

Count case-sensitive unique values in Excel

If your table contains case-sensitive data, the easiest way to count unique values would be creating a helper column with the following array formula to identify duplicate and unique items:

=IF(SUM((--EXACT($A$2:$A$10,A2)))=1,"Unique","Dupe")

And then, use a simple COUNTIF function to count unique values:

=COUNTIF(B2:B10, "unique")

Counting case-sensitive unique values in Excel

Count distinct values in Excel (unique and 1st duplicate occurrences)

To get a count of distinct values in a list, use the following formula:

=SUM(1/COUNTIF(range, range))

Remember, it's an array formula, and therefore you should press the Ctrl + Shift + Enter shortcut instead of the usual Enter keystroke.

Alternatively, you can use the SUMPRODUCT function and complete the formula in the usual way by pressing the Enter key:

=SUMPRODUCT(1/COUNTIF(range, range))

For example, to count the distinct values in range A2:A10, you can go with either:

=SUM(1/COUNTIF(A2:A10,A2:A10))

Or

=SUMPRODUCT(1/COUNTIF(A2:A10,A2:A10))

Counting distinct values in Excel

How the Excel distinct formula works

As you already know, we use the COUNTIF function to find out how many times each individual value appears in the specified range. In the above example, the result of the COUNTIF function is the following array: {2;2;3;1;2;2;3;1;3}.

After that, a number of division operations are performed, where each value of the array is used as a divisor with 1 as the dividend. This turns all duplicates values into fractional numbers corresponding to the number of duplicate occurrences. For example, if a value appears 2 times in the list, it generates 2 items in the array with a value of 0.5 (1/2=0.5). And if a value appears 3 times, it produces 3 items in the array with a value of 0.3(3). In our example, the result of 1/COUNTIF(A2:A10,A2:A10)) is the array {0.5;0.5;0.3(3);1;0.5;0.5;0.3(3);1;0.3(3)}.

Doesn't make much sense so far? That's because we haven't applied the SUM / SUMPRODUCT function yet. When one of these functions adds up the values in the array, the sum of all fractional numbers for each individual item always yields 1, no matter how many occurrences of that item exist in the list. And because all unique values appear in the array as 1's (1/1=1), the final result returned by the formula is the total number of all different values in the list.

Formulas to count distinct values of different types

As is the case with counting unique values in Excel, you can use variations of the basic Excel count distinct formula to handle specific value types such as numbers, text, and case-sensitive values.

Please remember that all of the below formulas are array formulas and require pressing Ctrl + Shift + Enter.

Count distinct values ignoring empty cells

If a column where you want to count distinct values might contain blank cells, you should add an IF function that will check the specified range for blanks (the basic Excel distinct formula discussed above would return the #DIV/0 error in this case):

=SUM(IF(range<>"",1/COUNTIF(range, range), 0))

For example, to count distinct values in range A2:A10, use the following array formula:

=SUM(IF(A2:A10<>"",1/COUNTIF(A2:A10, A2:A10), 0))

The formula to count distinct values ignoring empty cells

Formula to count distinct text values

To count distinct text values in a column, we'll be using the same approach that we've just used to exclude empty cells.

As you can easily guess, we will simply embed the ISTEXT function into our Excel count distinct formula:

=SUM(IF(ISTEXT(range),1/COUNTIF(range, range),""))

And here's a real-life formula example:

=SUM(IF(ISTEXT(A2:A10),1/COUNTIF(A2:A10, A2:A10),""))

Formula to count distinct numbers

To count distinct numeric values (numbers, dates and times), use the ISNUMBER function:

=SUM(IF(ISNUMBER(range),1/COUNTIF(range, range),""))

For example, to count all different numbers in range A2:A10, use the following formula:

=SUM(IF(ISNUMBER(A2:A10),1/COUNTIF(A2:A10, A2:A10),""))

Count case-sensitive distinct values in Excel

Similarly to counting case-sensitive unique values, the easiest way to count case-sensitive distinct values is to add a helper column with the array formula that identifies unique values including first duplicate occurrences. The formula is basically the same as the one we used to count case-sensitive unique values, with one small change in a cell reference that makes a great difference:

=IF(SUM((--EXACT($A$2:$A2,$A2)))=1,"Distinct","")

As you remember, all array formulas in Excel require pressing Ctrl + Shift + Enter.

After the above formula is finished, write a usual COUNTIF formula like =COUNTIF(B2:B10, "distinct") to count distinct values:

Counting case-sensitive distinct values in Excel

If there is no way you can add a helper column to your worksheet, you can use the following complex array formula to count case-sensitive distinct values without creating an additional column:

=SUM(IFERROR(1/IF($A$2:$A$10<>"", FREQUENCY(IF(EXACT($A$2:$A$10, TRANSPOSE($A$2:$A$10)), MATCH(ROW($A$2:$A$10), ROW($A$2:$A$10)), ""), MATCH(ROW($A$2:$A$10), ROW($A$2:$A$10))), 0), 0))

Count unique and distinct rows in Excel

Counting unique / distinct rows in Excel is akin to counting unique and distinct values, with the only difference that you use the COUNTIFS function instead of COUNTIF, which lets you specify several columns to check for unique values.

For example, to count unique or distinct names based on the values in columns A (First Name) and B (Last Name), use one of the following formulas:

Formula to count unique rows:

=SUM(IF(COUNTIFS(A2:A10,A2:A10, B2:B10,B2:B10)=1,1,0))

Formula to count distinct rows:

=SUM(1/COUNTIFS(A2:A10,A2:A10,B2:B10,B2:B10))

Counting unique and distinct rows in Excel

Naturally, you are not limited to counting unique rows based only on two columns, the Excel COUNTIFS function can process up to 127 range/criteria pairs.

Count distinct values in Excel using a PivotTable

The latest versions of Excel 2013 and Excel 2016 have a special feature that allows counting distinct values automatically in a pivot table. The following screenshot gives an idea of how the Excel Distinct Count looks like:

Distinct count in a pivot table

To create a pivot table with the distinct count for a certain column, perform the following steps.

  1. Select the data to be included in a pivot table, switch to the Insert tab, Tables group, and click the PivotTable button.
  2. In the Create PivotTable dialog, choose whether to place your pivot table in a new or existing worksheet, and be sure to select the Add this data to the Data Model checkbox.
    Select the 'Add this data to the Data Model' checkbox.
  3. When your pivot table opens, arrange the Rows, Columns and Values areas the way you want. If you don't have much experience with Excel pivot tables, the following detailed guidelines may prove helpful: Creating a PivotTable in Excel.
  4. Move the field whose distinct count you want to calculate (Item field in this example) to the Values area, click on it, and select Field Value Settings… from the drop-down menu:
    Move the field whose distinct count you want to calculate to the Values area, and select Field Value Settings…
  5. The Value Field Settings dialog window will open, you scroll down to Distinct Count, which is the very last option in the list, select it and click OK.

You can also give a custom name to your Distinct Count if you want to.
Scroll down to the very last option and select Distinct Count.

Done! The newly created pivot table will display the distinct count like shown in the very first screenshot in this section.

Tip. After updating your source data, remember to update the PivotTable to bring the distinct count up to date. To refresh a pivot table, just click the Refresh button on the Analyze tab, in the Data group.

This is how you count distinct and unique values in Excel. If someone wants to have a closer look at the formulas discussed in this tutorial, you are welcome to download the sample Excel Count Unique workbook.

I thank you for reading and hope to see you again next week. In the next article, we are going to discuss various ways to find, filter, extract and highlights unique values in Excel. Please stay tuned!

You may also be interested in:

25 Responses to "How to count distinct and unique values in Excel"

  1. Fiona says:

    Hi,
    Would you please help me on this scenario. I have a spreadsheet where column a shows patient name. Column b shows type of service. Column a have duplicate patient names since some patient gets more than one services. Now I need to identify( count) how many patients were receiving more than one services. Would you help me with the fomular? Or a way to do that. Thank you very much.

    • Hello Fiona,

      You can do it in the following way:

      1. Add the following formula, say in column C, to identify duplicate names:
      =IF(COUNTIF($A$2:$A2, $A2)>1, "Duplicate", "")

      Where A2 is the first cell in the Patient Names column.

      2. Apply Excel's filter, filter out the unique name so that only duplicate names are visible, and copy those duplicate names to another sheet.

      3. In that other sheet, use the following array formula to count distinct names (unique +1st duplicate occurrences). Remember to press Ctrl+Shift+Enter to complete the formula:
      =SUM(1/COUNTIF(A2:A8,A2:A8))

      Where A2 is the first and A8 is the last cell.

      I've create a quick example for you, and you can download it here.

      • John says:

        After filtering out duplicates, what you have are unique names. So, applying CountA(A2:A8) would give the count of those names. Why do we need to apply the array formula as mentioned by you? Any specific reason?

        • Hi John,

          Fiona's task was to count how many patients were receiving more than one services. For this, we needed to get a list of duplicates names, not unique names, and then count how many different (distinct) names appear in that list. I've re-worded point 2 in my previous comment, and hope now it makes more sense.

          For example, after filtering out the unique names, we get the following list of duplicate names: Peter, Maria, Peter, Rob, Maria. And now, we need an array formula mentioned above to count how many different names appear in this list (3 names). You can find the detailed explanation of the formula in How the distinct formula works.

  2. Manikandan C says:

    Dear all,

    i need to unq name wise sum of the Amount. not pivot required. Only formula.

    Ex:-
    Name Amount
    A 391
    B 145
    C 268
    D 514
    A 526
    C 991
    B 456

    Ans should be:-

    Count Amount
    4 1318

    Please share this ans.

  3. akshay says:

    hi,
    How to count the number of distinct values. If the values are neither numbers nor text but the mix of both like 12000078fg?

  4. Dani says:

    Hello,

    We download a report from Just Giving each time we have a donation and the format is not very easy to follow so we would just like to extract the information we need (eventually in a macro).

    I would like to lookup all the distinct values in the user ID column, then calculate the total number of donations for each of the distinct ID values. I can then apply this formula to the other fee columns.

    Any help would be much appreciated!

    Many thanks,

    Dani

  5. Fab says:

    Hi,

    I don't see the item "Add this data to the data model" in the Create Pivot Table window. How come? I am using Excel 2010.

    • Hi Fab,

      Regrettably, this option is not available in Excel 2010. It was introduced in Excel 2013, and also exists in Excel 2016. In earlier Excel versions, you can count unique values using formulas as demonstrated in the first parts of this tutorial.

  6. Derek says:

    I have Excel 2013, but the checkbox for "add this data to data model" is grayed-out and unchecked. Is there an Excel Add-in required to get this area functional?

    Thanks,
    Derek

  7. Ivan says:

    Hi,
    In Excel 2013 I have a table with more fields. The table is sorted on first field. In a new generated field of my table I have to count for each row the number of rows in the table depending on filter of the sorted field and also depending on filters of few other fields. I could solve this problem with COUNTIFS function, but there is another condition. One of the "fileds - filter" must be distinct, the others may be duplicates too.

    ?

    Any help would be much appreciated!

    Many thanks,

    Ivan

  8. Anand Kumar says:

    Nice Article.........

    Thanks ☻.

  9. Ranjeet Ranjan says:

    I did not find option Add data modeling during creating pivot table in Excel 2016

  10. megann mcdaniel says:

    I have Excel 2013 and use Pivot tables often. I tried using the Count Distinct Values feature but it is not listed in the Value Field Settings. Is it possible I need to install an update?

  11. Shruthi says:

    Hi I want the formula to count how many clients have completed by one person according to the date.
    Client Name Auditor name Date
    SFM Realty Corp. Shruthi 10/25/2016
    SFM Realty Corp. Shruthi 10/25/2016
    SFM Realty Corp. Shruthi 10/25/2016
    American Kiosk Shruthi 10/26/2016
    American Kiosk Shruthi 10/26/2016
    American Kiosk Shruthi 10/26/2016

    Here I need the Count for the Auitor name Shruthi Audited clients as 1 on 10/25/2016 & 1 on 10/26/2016.It should take the distinct names in the clients coloumn.

    Please its Immediately required

  12. Chetan says:

    Hi I want to count values excluding the duplicates.
    For example, I have two materials with different batch numbers for each material which contains 10 containers each belonging to multiple shipping lines. from this 20 containers of two batches, I want to count no. of containers under each shipping lines by using countifs and criterias are material name, batch number and shipping line and type of container (20' or 40'). Each containers weighs 24.75 tons but I have one container 12.375 in first batch and 12.375 in another batch. I don't want this container to be counted twice when I enter the batch number.

  13. agostonz says:

    Hi, I wish to all af you nice & new challanges, for 2017.
    I have my own and asking for your help... My data are as below, in Excel 2013:
    -column A: incomming invoice date (can be more identical, as there are more invoices on the same day). Like jan01, jan01, jan02, jan15, jan15, ...
    The column is filtered, / Month or whatever.
    I need to count the dates where there is only one input (row) in the filtered range (needed result is 1 (jan02) from above example, as that is the day when just one invoise was received.
    There is no option to add more columns. Is it possible to get the right result by using the date column only?

    Thanks and looking forward to your advice.

  14. LW says:

    How would I count distinct dates in a selection of cells? I tried changing the dates to number format and then using =SUM(IF(ISNUMBER(range),1/COUNTIF(range, range),"")), but I got an incorrect answer with an error saying that the formula "omits adjacent cells".

  15. christy says:

    Hi,

    I forgot to select Add this data to the Data Model checkbox.what to do now

  16. Paul says:

    First - thank you so much for this site. Very helpful.

    I have a spreadsheet with a large number of distinct account numbers. Each time an account number appears on the spreadsheet, whether duplicate or unique, it represents a different transaction with a value for each transaction. There are 3 columns, (A) for acct. #; (B) for transaction 1; and (C) for interest on transaction 1. I would like to get a list of distinct account numbers with the sum of the values for each distinct acct. number. (At the end, the list would contain a column for: (A) Distinct Acct.#; and, (B) sum or total transaction value for all values for each acct#; and (C) total or sum of interest for each acct#.

    • Hello Paul,

      Thank you very much for your feedback, we're happy to hear you find our site helpful.

      It sounds like the Subtotal option in Excel will do what you need:
      - Select your records and go to Data tab
      - Click on Subtotal and choose the following options:
      - At each change in - column with the account number, use function - SUM, add subtotal to - select the columns with the values you want to sum.
      - Click OK

      I hope this helps.

  17. Adarsh says:

    my inputs are

    Abc 2 500
    def 1 2000
    abc 4 300

    Output should be:
    Abc 6 800
    def 1 2000

  18. Ashok says:

    Hi Team,

    Need help in adding total development hours of particular developer,irrespective of their name in Dev1, Dev2 or Dev3

    Ticket ID Dev1 Dev2 Dev3 #Dev1hrs #Dev2hrs #Dev3hrs
    1 Basanth Sriram Shoks 10 12 10
    2 Shoks Basanth Sriram 5 10 17

  19. Tugcan says:

    Hi,

    I have an excel sheet for training records which has columns named "name of course", "instructor", "course number", "attendee", "duration"etc. As I have multiple attendees on each training, training number is remaining same for different attendees. I would like to see the total duration for each instructor seperately on pivot table however, when I use "sum of duration" as a pivot table column, it is calculating all the numbers shown under duration column. (I mean, if there are 5 attendees for same training and if the duration is 8 hours, value that I want to see on pivot table is 8 hours, unfortunately it multiplies the number of attendees and duration and seems 40 hours) Is there a way to solve this problem?

    Thanks in advance.

Post a comment



Unfortunately, due to the volume of comments received we cannot guarantee that we will be able to give you a timely response. When posting a question, please be very clear and concise. We thank you for understanding!
Ultimate Suite for Excel Professionals
 
 
50+ professional tools for Excel 2016-2007 to do your daily work
Incredible product, even better tech support…AbleBits totally delivers!
Deborah Bryant
Anyone who works with Excel is sure to find their work made easier
Jackie Lee
The best spent money on software I've ever spent!
Patrick Raugh
Ablebits is a fantastic product - easy to use and so efficient.
Debra Celmer
Excel is at its best now
Annie C.
I don't know how to thank you enough for your Excel add-ins
Jennifer Morningstar
Anybody who experiences it, is bound to love it!
Kumar Nepa
AbleBits suite has really helped me when I was in a crunch!
Nelda Fink
I have enjoyed every bit of it and time am using it
Christian Onyekachi Nwosu
It's the best $100 we've ever spent!
Mike Cavanagh
I love the program, and I can't imagine using Excel without it!
Robert Madsen
One word… WOW!
Dave Brown
Love the products!
David Johnston
It is like having an expert at my shoulder helping me…
Linda Shakespeare
Your software really helps make my job easier
Jeannie C.
Thanks for a terrific product that is worth every single cent!
Dianne Young
I love your product
Brad Gibson
Awesome!!!
Sheila Blanchard