Count unique values among duplicates
Excel for Microsoft 365 Excel for Microsoft 365 for Mac Excel for the web Excel 2021 Excel 2021 for Mac Excel 2019 Excel 2019 for Mac Excel 2016 Excel 2016 for Mac Excel 2013 Excel 2010 Excel 2007 Excel for Mac 2011 More…Less
Let’s say you want to find out how many unique values exist in a range that contains duplicate values. For example, if a column contains:
-
The values 5, 6, 7, and 6, the result is three unique values — 5 , 6 and 7.
-
The values «Bradley», «Doyle», «Doyle», «Doyle», the result is two unique values — «Bradley» and «Doyle».
There are several ways to count unique values among duplicates.
You can use the Advanced Filter dialog box to extract the unique values from a column of data and paste them to a new location. Then you can use the ROWS function to count the number of items in the new range.
-
Select the range of cells, or make sure the active cell is in a table.
Make sure the range of cells has a column heading.
-
On the Data tab, in the Sort & Filter group, click Advanced.
The Advanced Filter dialog box appears.
-
Click Copy to another location.
-
In the Copy to box, enter a cell reference.
Alternatively, click Collapse Dialog to temporarily hide the dialog box, select a cell on the worksheet, and then press Expand Dialog .
-
Select the Unique records only check box, and click OK.
The unique values from the selected range are copied to the new location beginning with the cell you specified in the Copy to box.
-
In the blank cell below the last cell in the range, enter the ROWS function. Use the range of unique values that you just copied as the argument, excluding the column heading. For example, if the range of unique values is B2:B45, you enter =ROWS(B2:B45).
Use a combination of the IF, SUM, FREQUENCY, MATCH, and LEN functions to do this task:
-
Assign a value of 1 to each true condition by using the IF function.
-
Add the total by using the SUM function.
-
Count the number of unique values by using the FREQUENCY function. The FREQUENCY function ignores text and zero values. For the first occurrence of a specific value, this function returns a number equal to the number of occurrences of that value. For each occurrence of that same value after the first, this function returns a zero.
-
Return the position of a text value in a range by using the MATCH function. This value returned is then used as an argument to the FREQUENCY function so that the corresponding text values can be evaluated.
-
Find blank cells by using the LEN function. Blank cells have a length of 0.
Notes:
-
The formulas in this example must be entered as array formulas. If you have a current version of Microsoft 365, then you can simply enter the formula in the top-left-cell of the output range, then press ENTER to confirm the formula as a dynamic array formula. Otherwise, the formula must be entered as a legacy array formula by first selecting the output range, entering the formula in the top-left-cell of the output range, and then pressing CTRL+SHIFT+ENTER to confirm it. Excel inserts curly brackets at the beginning and end of the formula for you. For more information on array formulas, see Guidelines and examples of array formulas.
-
To see a function evaluated step by step, select the cell containing the formula, and then on the Formulas tab, in the Formula Auditing group, click Evaluate Formula.
-
The FREQUENCY function calculates how often values occur within a range of values, and then returns a vertical array of numbers. For example, use FREQUENCY to count the number of test scores that fall within ranges of scores. Because this function returns an array, it must be entered as an array formula.
-
The MATCH function searches for a specified item in a range of cells, and then returns the relative position of that item in the range. For example, if the range A1:A3 contains the values 5, 25, and 38, the formula =MATCH(25,A1:A3,0) returns the number 2, because 25 is the second item in the range.
-
The LEN function returns the number of characters in a text string.
-
The SUM function adds all the numbers that you specify as arguments. Each argument can be a range, a cell reference, an array, a constant, a formula, or the result from another function. For example, SUM(A1:A5) adds all the numbers that are contained in cells A1 through A5.
-
The IF function returns one value if a condition you specify evaluates to TRUE, and another value if that condition evaluates to FALSE.
Need more help?
You can always ask an expert in the Excel Tech Community or get support in the Answers community.
See Also
Filter for unique values or remove duplicate values
Need more help?
Want more options?
Explore subscription benefits, browse training courses, learn how to secure your device, and more.
Communities help you ask and answer questions, give feedback, and hear from experts with rich knowledge.
To count the number of different values in A2:A100 (not counting blanks):
=SUMPRODUCT((A2:A100<>"")/COUNTIF(A2:A100,A2:A100&""))
Copied from an answer by @Ulli Schmid to What is this COUNTIF() formula doing?:
=SUMPRODUCT((A1:A100<>"")/COUNTIF(A1:A100,A1:A100&""))
Counts unique cells within A1:A100, excluding blank cells and ones with an empty string («»).
How does it do that? Example:
A1:A100 = [1, 1, 2, "apple", "peach", "apple", "", "", -, -, -, ...]
then:
A1:A100&"" = ["1", "1", "2", "apple", "peach", "apple", "", "", "", "", "", ...]
so this &»» is needed to turn blank cells (-) into empty strings («»). If you were to count directly using blank cells, COUNTIF() returns 0. Using the trick, both «» and — are counted as the same:
COUNTIF(A1:A100,A1:A100) = [2, 2, 1, 2, 1, 2, 94, 94, 0, 0, 0, ...]
but:
COUNTIF(A1:A100,A1:A100&"") = [2, 2, 1, 2, 1, 2, 94, 94, 94, 94, 94, ...]
If we now want to get the count of all unique cells, excluding blanks and «», we can divide
(A1:A100<>""), which is [1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, ...]
by our intermediate result, COUNTIF(A1:A100,A1:A100&»»), and sum up over the values.
SUMPRODUCT((A1:A100<>"")/COUNTIF(A1:A100,A1:A100&""))
= (1/2 + 1/2 + 1/1 + 1/2 + 1/1 + 1/2 + 0/94 + 0/94 + 0/94 + 0/94 + 0/94 + ...)
= 4
Had we used COUNTIF(A1:A100,A1:A100)
instead of COUNTIF(A1:A100,A1:A100&"")
, then some of those 0/94 would have been 0/0. As division by zero is not allowed, we would have thrown an error.
A common task in Excel is to find out the number of different entries in a list. For example, you have a list of names and want to know, how many different people are listed as some people might be multiple times on the list. This article introduces 5 different methods of counting the number of unique records in a list, regarding two major differences.
- You simply want to know the number of unique records. You don’t need to consider any condition.
- Or you want to know the number of different entries under one or more conditions.
Example for this article to count the number of unique records
The screenshot on the right-hand side shows the example table. It is a list of ten persons contains three columns. The values are the results from a game a group of friends were playing. Column A has the name of the person, column B the number of trial and column C the result per person and trial. You want to answer two questions:
- How many people were playing?
- How many people had a result larger than 40 in the first trial?
Method 1. Using Excel function “Remove Duplicates”
If you only want to count the number of unique records once and don’t have to automatically update the result, you could use the function called “Remove Duplicates”. It’s a built-in function in Excel and you can find it within the “Data”-ribbon.
- Select the list or column you want to know the number of unique values of and copy it to a new sheet or empty space on the same sheet.
Recommendation: Instead of simply pasting the cells, paste them as values by pressing Ctrl + Alt + V on the keyboard. Select “Values” and press enter. That way, formulas will be removed so that the values don’t change through possible underlying formulas within the pasting process. - Select the pasted values and click on “Remove Duplicates” on the “Data” ribbon.
- A new window opens. If your data has headers, set the relating tick at “My data has headers”. Confirm with OK.
- Select the list which now doesn’t have any duplicates any longer. In the bottom left corner of the window Excel shows the number of entries selected like this “Count: x”.
Alternatively, you could use the COUNT and COUNTIFS formulas.
Please note:
- Blank cells are ignored in the number in the bottom left corner of the screen.
- This method is not suitable for the second question of our example.
Method 2. Using advanced filters
The next method—using advanced filters—works very similar to the previous method removing duplicates. Advanced filters in Excel provide a function to filter to unique records only. This advanced filter hides duplicates as you are probably familiar from normal filters. The screenshot on the right-hand side shows the steps.
- Start by selecting the list or column containing the values you want to count the unique items from.
- Click on “Advanced” within the “Sort & Filter”-group on the “Data”-ribbon.
- In the new window check “Filter the list, in-place”, make sure the correct “List range” is selected and set the tick at “Unique reconds only”. Confirm with OK.
- The status bar now shows already the number of records left. In this example it says “8 of 10 records found”.
Method 3. PivotTables
PivotTables are very powerful in Excel. Coming with the versatility, they are often complex to set up. In this part we take a rough look at the necessary steps to answer the questions using PivotTables. Because PivotTables could fill books themselves, we concentrate on the crucial steps rather than going too much into detail of all their basics.
PivotTables have the advantage that with an update of the data, they can be refreshed. Our previous methods 1 and 2 can’t be easily refreshed—at least not without going through all the steps again. On the other hand, PivotTables aren’t as dynamic as using Excel formulas.
Steps for creating the PivotTable and count the number of unique records
The necessary steps are shown in the image on the right-hand side.
- Select your list or data.
- Insert a PivotTable by clicking on “PivotTable” on the left-hand side of the “Insert”-ribbon. Follow the steps on the screen until the PivotTable is created.
- Pull the column name you want to count into the “Row” field.
- Select all names and look at the number shown in the status bar similar to the third step of the method 1 before.
Answering the second question is a little bit trickier. The question is “How many people had a result higher than 40 in the first trial?”. You can use the same PivotTable you’ve just created for answering the first question and add some modifications.
- Pull the column names “Trial” down into the Filter field and “Result” into the Values field.
- Now use an overall filter to the data. Set the filter of “Trial” in cell N2 to “1st Trial”.You could use the same approach as in number 6 before for the result time. But the overall filter doesn’t
- have advanced options like filtering all values larger than a given number. To achieve this, you need to use the Filter on the “Values” column. Click on the small arrow in the headline of the columns (here in cell M5). Define a Value Filter larger than 40. The result should look similar to the image above.
Do you want to boost your productivity in Excel?
Get the Professor Excel ribbon!
Add more than 120 great features to Excel!
Method 4. Formulas SUMPRODUCT and COUNTIFS
The previous methods 1, 2 and 3 aren’t entirely dynamic. That means, with an update of the data, the results don’t automatically change without further steps. Even the PivotTable in our method 3 requires a refresh. Out methods 4 and 5 don’t have such constraints. They automatically update their results because they are based on Excel formulas.
Structure of the SUMPRODUCT / COUNTIFS functions
This method is based on the two Excel formulas SUMPRODUCT and COUNTIFS. For more information about these two formulas please refer to SUMPRODUCT and COUNTIFS.
The formula is comparatively short and works for up to one criteria. So, the second question of our example―as it requires two criteria―can’t be solved with this formula combination. In such case, please proceed with the following method 5.
The base formula combination is shown in the image above.
The COUNTIFS formula returns an array of numeric values. It has one value for each record of your data saying how many times it occurs. If one value only appears once, it will have the number 1. If it one the other hand appears twice, it will have two times 2. The &”” signs prevent blank cells (will be regarded as zeroes) to be regarded. If you don’t add &””, blank cells will be regarded as one record.
Now, these numbers just have to be added up. To make the formula combination universally usable, we choose SUMPRODUCT right away. In a simplified version of the formula, this also works just using SUM and inserting it as an array formula.
After applying this formula to our example in this article―counting the number of different persons in cell range A2 to A11―you will have the following formula.
=SUMPRODUCT(1/COUNTIFS(A2:A11,A2:A11&""))
Add a condition to count unique records
Now, we add a condition. Because this formula regards empty cells as one unique record, the condition might be, that cells mustn’t be empty. The condition can be entered instead of the 1 in the beginning of the SUMPRODUCT formula like in the image on the right-hand side.
If the condition is, that empty cells don’t count, the condition would be COUNT_RANGE<>””. For your example that means
=SUMPRODUCT((A2:A11<>"")/COUNTIFS(A2:A11,A2:A11&""))
You can of course use different criteria. But again, only one criteria or condition is possible. The second question of your example is “How many people had a result higher than 40 in the first trial?”. This question requires two conditions (higher than 40 and the first trial). But answering the question of how many people had a result higher than 40 (without the condition that it must have happened in the first trial) is possible to answer with this formula. The result is given in cells C2 to C11. That means the criteria is (C2:C11>40). The complete formula is
=SUMPRODUCT((C2:C11>40)/COUNTIFS(A2:A11,A2:A11&""))
Method 5. Array formula using FREQUENCY
The last of our five methods is also the most complex one. That’s because it involves up to five different formulas combined to one long formula combination. But this is the only entirely dynamic solution for counting the number of unique records with multiple criteria.
Structure of the array formula
The structure of the base formula combination is shown in the figure on the right-hand side. You only have to insert two different parts.
- The “COUNT RANGE” is the list or range you want to get the number of different values from. In the example of this chapter it’s A2:A11.
- The “FIRST CELL OF COUNT RANGE” is basically the first part of the “COUNT RANGE”. Because the count range in your example is A2:A11, it’s just A2.
Applying this formula on the previous example leads to the following formula.
=SUM((FREQUENCY(MATCH(A2:A11,A2:A11,0),ROW(A2:A11)-ROW(A2)+1)>0)*1)
Explanation of the FREQUENCY array formula to count the number of unique records
What does this formula do in the background? The basic formula is FREQUENCY (please refer to this article for more information).
- The array version of the MATCH formula returns the first position of each record within the list of records. If a record appears twice, the second time it’s first position is repeated. That way, the “DATA ARRAY” of text is converted into an array of number.
The formula of the example at this step: =SUM((FREQUENCY({1,2,3,4,4,6,7,8,9,9},ROW(A2:A11)-ROW(A2)+1)>0)*1) - The second argument of the FREQUENCY formula creates an array of numbers, starting with 1 counting up until there is one value for each record. This array is used as the “BIN ARRAY” of the FREQUENCY formula.
The formula of the example after this step: =SUM((FREQUENCY({1,2,3,4,4,6,7,8,9,9},{1,2,3,4,5,6,7,8,9,10}>0)*1) - The FREQUENCY formula returns an array of numbers. These number say for each record, saying how many times it occurs. If a record appears the first time, it’s being counted to the respective bin of how many occurences there are in total. The second and any following occurrence of the same item (already converted to a number by the MATCH formula) is counted into the bin “0”.
The formula of the example after this step: =SUM(({1,1,1,2,0,1,1,1,2,0,0}>0)*1) - The “>0” checks for each number of the FREQUENCY formula, if it’s larger than 0. All first occurences get TRUE and all second, third and more often occurences get a FALSE.
The formula of the example after this step: =SUM(({TRUE,TRUE,TRUE,TRUE,FALSE,TRUE,TRUE,TRUE,TRUE,FALSE,FALSE})*1) - Multiplying this array of TRUEs and FALSEs by 1 converts them into the numerical values of 0 and 1.
=SUM({1,1,1,1,0,1,1,1,1,0,0}) - The enlosing SUM formula adds up all 1 which stand for the first occurrence of an item.
=8
Adding the first criteria
After solving the first question of the example, it’s time to add one criteria. Because the formula as shown before can’t handle blank cells, the first condition will be to skip blank cells.
The image on the right-hand side shows the structure of the FREQUENCY formula with one condition. As you can see, the formula hasn’t changed much. Just one part is added, illustrated with number 3. You insert the condition using the IF formula. The corresponding closing blanket of the IF formula is after the MATCH formula. If the condition is not met, this part of the formula returns FALSE. This results in also FALSE at this point of the “DATA ARRAY” of the FREQUENCY formula.
Add the condition itself the following way: (CONDITION_RANGE=CONDITION)
The condition of our example table is to skip empty cells. The CONDITION_RANGE is A2:A11 and the condition is <>””. Putting the condition into the complete formula leads to the following formula.
=SUM((FREQUENCY(IF(B2:B11<>"",MATCH(A2:A11,A2:A11,0)),ROW(A2:A11)-ROW(A2)+1)>0)*1)
Adding the second criteria
After knowing how to handle one condition, it’s time to proceed with the second question of our example: How many people had a result higher than 40 in the first trial?
The same way the first condition is added, a second can be inserted. Just add one more IF formula before the existing IF formula. The corresponding closing bracket needs to be entered after the MATCH formula.
In the example of this chapter, the trial number is located in cells B2 to B11 and should be the first trial. The second criteria range is the result, located in cell range C2 to C11 and should be higher than 40. Regarding these two criteria leads to the following formula.
=SUM((FREQUENCY(IF(C2:C11>40,IF(B2:B11="1st trial",MATCH(A2:A11,A2:A11,0))),ROW(A2:A11)-ROW(A2)+1)>0)*1)
Once again, the hint: All formulas shown in this method are array formulas. After pressing Ctrl + Shift + Enter on the keyboard, curled brackets are added so that the formula―when seeing in the formula bar―look like this.
{=SUM((FREQUENCY(IF(C2:C11>40,IF(B2:B11="1st trial",MATCH(A2:A11,A2:A11,0))),ROW(A2:A11)-ROW(A2)+1)>0)*1)}
Method 6 (new). COUNTA and UNIQUE functions
Because the UNIQUE function is comparatively new in Excel, not all versions support this method. But because is by far the easiest, I have listed it first.
Just type the following function:
=COUNTA(UNIQUE(A:A))
in order to count the number of unique values in column A. Please note: A blank cell is also counted as one cell. If you don’t want to include blank cells in your counting, please extend the function like this:
=COUNTA(UNIQUE(A:A))-IFERROR(MIN(1,COUNTBLANK(A:A)),0)
Download
Please feel free to download all examples above in this Excel workbook. Click here and the download starts.
Image by RitaE from Pixabay
Counting Unique Values in Excel
Unique value in excel appears in a list of items only once and the formula for counting unique values in Excel is “=SUM(IF(COUNTIF(range,range)=1,1,0))”. The purpose of counting unique and distinct values is to separate them from the duplicates of a list of Excel.
A duplicate value appears in a list of items more than once. A distinct value refers to all the different values of the list of items. So, distinct values are unique values plus the first occurrences of duplicate values.
For example, a list contains the numbers 10, 12, 15, 15, 18, 18, and 19. The unique values of this list are 10, 12, and 19. The duplicate values are 15 and 18. The distinct values are 10, 12, 15, 18, and 19.
This article focuses on counting the distinct values of Excel. For counting the unique values of Excel, refer to the first question under the heading “frequently asked questions” of this article.
Table of contents
- Counting Unique Values in Excel
- How to Count the Distinct Values in Excel?
- Example #1–Count Unique Excel Values by Using the SUM and COUNTIF Functions
- Example #2–Count Unique Excel Values by Using the SUMPRODUCT and COUNTIF Functions
- Example #3–Count Unique Excel Values by Excluding the Empty Cells of the Range
- Frequently Asked Questions
- Recommended Articles
- How to Count the Distinct Values in Excel?
How to Count the Distinct Values in Excel?
The methods of counting the distinct values in Excel are listed as follows:
- SUM and COUNTIF functions
- SUMPRODUCT and COUNTIF functions
Let us discuss the two methods with the help of examples.
You can download this COUNT Unique Values Excel Template here – COUNT Unique Values Excel Template
Example #1–Count Unique Excel Values by Using the SUM and COUNTIF Functions
The following image shows the names of customers (column A) and the dates (column B) on which sales were made to them. The revenue generated (in $) from each customer is given in column C.
The entire dataset belongs to an organization. It relates to the period April 2018. Count the unique values of excel column A with the help of the SUM and COUNTIF functions of Excel.
The steps to count the unique excel values by using the SUMThe SUM function in excel adds the numerical values in a range of cells. Being categorized under the Math and Trigonometry function, it is entered by typing “=SUM” followed by the values to be summed. The values supplied to the function can be numbers, cell references or ranges.read more and COUNTIFThe COUNTIF function in Excel counts the number of cells within a range based on pre-defined criteria. It is used to count cells that include dates, numbers, or text. For example, COUNTIF(A1:A10,”Trump”) will count the number of cells within the range A1:A10 that contain the text “Trump”
read more functions of Excel are listed as follows:
Step 1: Enter the following formula in cell E3.
Step 2: Press the keys “Ctrl+Shift+Enter” together. This is because the given formula is an array formulaArray formulas are extremely helpful and powerful formulas that are used in Excel to execute some of the most complex calculations. There are two types of array formulas: one that returns a single result and the other that returns multiple results.read more. On pressing the CSE (Ctrl+Shift+EnterCtrl-Shift Enter In Excel is a shortcut command that facilitates implementing the array formula in the excel function to execute an intricate computation of the given data. Altogether it transforms a particular data into an array format in excel with multiple data values for this purpose.read more) keys, the curly braces appear at the beginning and end of the formula, as shown in the following image.
Note: An array formula is always completed by pressing the CSE keys. Even after editing an array formula, the CSE keys must be pressed to save the changes made. An array formula cannot be applied to merged cells.
Step 3: Once the CSE keys are pressed, the output appears in cell E3. This is shown in the following image. Hence, there are 12 distinct values in column A. In other words, the organization sold to 12 different customers in April 2018.
Explanation of the formula: The formula entered in step 1 has three parts, namely, the COUNTIF function, “1/,” and the SUM function. These are shown in the following image.
Ignore the arrows of parts 2 and 1, which are slightly misplaced in the following image.
In this formula, the COUNTIF function is processed first as it is the innermost function. Thereafter, “1/” and the SUM function are processed. The entire formula works as follows:
a. The COUNTIF function is supplied a single range (A2:A25) twice. This tells the function to count the number of times a value appears in this range. Since there are 24 values in this range, the COUNTIF function returns an array of 24 values. These are shown in the following image.
So, the first 3 implies that the name “Ruturaj” appears thrice in the range A2:A25. Likewise, the following 1 implies that the name “Kamal” appears once in this range.
b. Next, the number 1 is divided by all the values returned in the preceding array. The output is again an array of 24 values. These are shown in the following image.
Since “Ruturaj” appears thrice (in A2:A25), the value 3 divided by 1 returns 0.33. Likewise, “Kamal” appears once, so 1 divided by 1 is equal to 1. Therefore, all values that appear once in the stated range (A2:A25) return 1. The values that return a decimal number have more than one occurrence.
c. Then, the SUM function sums the values returned in the preceding array. Note that if a value appears thrice in the stated range, 0.33 (1/3=0.33) appears thrice. So, 0.33+0.33+0.33 is equal to 1. Likewise, if a value appears twice in the range A2:A25, 0.5 (1/2=0.5) appears twice. So, 0.5+0.5 is equal to 1. In this way, the sum of all occurrences of a value is always equal to 1. Therefore, the SUM function returns the total of all the different values in the range A2:A25.
Hence, the count of unique excel values (in the range A2:A25) is 12. This 12 is the sum of two unique values (Kamal and Raju) and the first occurrence of ten duplicate values (Ruturaj, Rohit Gulia, Abhishek Tanwar, Srinidhi, Karuna Jain, Andrew Flint, Cummins, Rahul, Ramesh, and Christoper).
Note: To view the array of values returned by the COUNTIF function in pointer “a,” follow the listed steps:
- Select cell E3 containing the formula.
- Double-click within the selected cell or press the key F2. This helps enter the Edit mode.
- Select the COUNTIF part of the formula, i.e., “COUNTIF(A2:A25,A2:A25).”
- Press the key F9.
Likewise, to view the array of pointer “b,” select cell E3 and double click within the selected cell. Next, select the part “1/COUNTIF(A2:A25,A2:A25)” and press F9.
To exit the Edit mode, press the escape (Esc) key.
Example #2–Count Unique Excel Values by Using the SUMPRODUCT and COUNTIF Functions
The following image shows the dataset of example #1. Count the distinct values of column A with the help of the SUMPRODUCT and the COUNTIF functions of Excel.
The steps to count the distinct values by using the SUMPRODUCTThe SUMPRODUCT excel function multiplies the numbers of two or more arrays and sums up the resulting products.read more and COUNTIF functions are listed as follows:
Step 1: Enter the following formula in cell E6.
Step 2: Press the “Enter” key. The output appears in cell E6, as shown in the following image. So, there are 12 distinct values in the range A2:A25.
Notice that the output of the SUM and COUNTIF (example #1) is the same as that of the SUMPRODUCT and COUNTIF (example #2). Hence, since the outputs are the same, one can choose either of the two formulas based on convenience.
Explanation of the formula: The formula given in step 1 (of this example) works exactly the same way the formula of example #1 works. The only difference between the formulas of examples #1 and #2 is the usage of the CSE keys in the former and the usage of the “Enter” key in the latter.
In the current formula, the COUNTIF function and the division by 1 return the same array as that of pointers “a” and “b” of example #1. Being a single array, the SUMPRODUCT sums the values of this array. The output is 12. So, this 12 consists of two unique values and the first occurrences of ten duplicate values.
Note 1: For the detailed working of the current formula, refer to the “explanation of the formula” given at the end of example #1.
Note 2: To see the complete array values returned by the COUNTIF and “1/” part, one can select these parts and press the F9 key. For more details, refer to the note at the end of example #1.
Example #3–Count Unique Excel Values by Excluding the Empty Cells of the Range
Working on the dataset of example #1, we have inserted row 10 as a blank row. As a result, the formulas of examples #1 and #2 show a “#DIV/0!” errorErrors in excel are common and often occur at times of applying formulas. The list of nine most common excel errors are — #DIV/0, #N/A, #NAME?, #NULL!, #NUM!, #REF!, #VALUE!, #####, Circular Reference.read more in cells E3 and E6. This error is displayed when a number is divided by zero (or an empty cell) in Excel.
Count the number of distinct values of column A by excluding the empty cell A10. Use the following functions of Excel:
- SUM and COUNTIF functions
- SUMPRODUCT and COUNTIF functions
The steps to count the distinct values by excluding the empty cell are listed as follows:
Step 1: Enter the following formulas (without the beginning and ending double quotation marks) in cells E3 and E6 respectively.
“=SUM(1/COUNTIF(A2:A26,A2:A26&“”))-1”
“=SUMPRODUCT(1/COUNTIF(A2:A26,A2:A26&“”))-1”
Step 2: Press the CSE keys (Ctrl+Shift+Enter) after entering the SUM and COUNTIF formula. Press the “Enter” key after entering the SUMPRODUCT and COUNTIF formula.
The outputs of both the formulas are shown in the following image. Notice that in this image, the SUM and COUNTIF formula is displayed (in curly braces) in the formula bar.
Hence, the output of both formulas is 12. This implies that there are 12 distinct values in column A. The empty cell A10 is excluded from this count.
Explanation of the formulas: The preceding two formulas work as follows:
a. The COUNTIF function is instructed to count the non-blank cells within the range A2:A26. Since the ampersand operator (&) along with an empty string (“”) is supplied to the COUNTIF function, it treats the empty cell A10 as a unique value. So, the COUNTIF returns the following array of values:
{3;1;2;2;3;2;2;2;1;3;2;2;2;3;2;2;2;3;2;2;1;3;2;2;2}
b. Next, the values of the preceding array are divided by 1. This division returns the following array of values:
{0.333333333333333;1;0.5;0.5;0.333333333333333;0.5;0.5;0.5;1;0.333333333333333; 0.5;0.5;0.5;0.333333333333333;0.5;0.5;0.5;0.333333333333333;0.5;0.5;1;0.333333333333333;0.5;0.5;0.5}
c. At last, the single array is summed up by the SUM or the SUMPRODUCT functions. This returns 13 as the sum. From this sum, 1 is subtracted to exclude the empty cell from the count. So, the final output is 12.
Hence, Excel returns the same output irrespective of the formula used. Notice that in this example, the COUNTIF returned 1 for the single empty cell A10.
Likewise, had there been two empty cells in the range A2:A26, the COUNTIF would have returned 2 at two places in the array. For three empty cells, the COUNTIF would have returned 3 at three places in the array. So, the sum of all occurrences of empty cells is always equal to 1.
Therefore, the given two formulas would have returned the correct output even if there had been more than one empty cell in the supplied range.
Note: To view the array of values in pointers “a” or “b,” select the parts “COUNTIF(A2:A26,A2:A26&“”)” or “1/COUNTIF(A2:A26,A2:A26&“”)” of the formula. Next, press the key F9.
Frequently Asked Questions
1. Define unique values and state the formula for counting them in Excel.
Unique values are those that appear in a list of items only once. The formula for counting unique values in Excel is stated as follows:
“=SUM(IF(COUNTIF(range,range)=1,1,0))”
This formula works as follows:
a. The COUNTIF function is supplied with a single range twice. It counts the number of values that appear only once in the stated range. It returns an array of values.
b. The IF function considers the argument “COUNTIF(range,range)=1” as the logical test. If the COUNTIF returns 1, the IF function also returns 1 (value_if_true). However, if the COUNTIF returns a value other than 1, the IF function returns 0 (value_if_false). All ones returned by the IF function are unique values and all zeroes are duplicate values.
c. The SUM function sums the single array of ones and zeroes returned by the IF function.
The final output is the count of the unique values of the list of Excel.
Note 1: The given formula is an array formula. So, ensure that the CSE (Ctrl+Shift+Enter) keys are pressed after entering the formula in Excel. Exclude the beginning and ending double quotation marks while entering the formula in Excel.
Note 2: To extract the unique values, the duplicates need to be removed from the list.
2. Define distinct values and state the formula for counting them in Excel.
Distinct values are all the different values of a list of items. So, distinct values are the unique values plus the first occurrences of duplicate values.
To count the distinct values in Excel, either of the following formulas can be used (without the beginning and ending double quotation marks):
• “=SUM(1/COUNTIF(range,range))”
• “=SUMPRODUCT(1/COUNTIF(range,range))”
The only difference between the two formulas is that the former is completed with the CSE (Ctrl+Shift+Enter) keys, while the latter is completed with the “Enter” key.
Note: For more details on the working of the preceding formulas, refer to examples #1 and #2 of this article. For counting distinct values by excluding the empty cells, refer to example #3 of this article.
3. How to count the unique and distinct values by using the COUNTIFS function of Excel?
The formula for counting the unique rows of ranges A2:A12 and B2:B12 is stated as follows:
“=SUM(IF(COUNTIFS(A2:A12,A2:A12,B2:B12,B2:B12)=1,1,0))”
The formula for counting the distinct rows of ranges A2:A12 and B2:B12 is stated as follows:
“=SUM(1/COUNTIFS(A2:A12,A2:A12,B2:B12,B2:B12))”
Both the COUNTIFS formulas work similar to their COUNTIF counterparts. The benefit of using the COUNTIFS function is that it allows checking multiple columns for unique or distinct values.
Note: A row will be counted by the two formulas only if the cells in both columns A and B are unique (or distinct). Further, both the stated formulas are array formulas. So, do press the CSE (Ctrl+Shift+Enter) keys to complete these formulas.
Recommended Articles
This has been a guide to counting the unique and distinct values in Excel. Here, we explain top 2 easy methods along with step by step examples. You may also look at these useful functions of Excel–
- Calculate SUMPRODUCT with Multiple CriteriaIn Excel, using SUMPRODUCT with several Criteria allows you to compare different arrays using multiple criteria.read more
- COUNTIF with Multiple Criteria
- ISNA Function in ExcelThe ISNA function is an error handling function in Excel. It helps to find out whether any cell has a “#N/A error” or not. This function returns the value “true” if “#N/A error” is identified or «false» if not identified.read more
- OFFSET Excel ExampleThe OFFSET function in excel returns the value of a cell or a range (of adjacent cells) which is a particular number of rows and columns from the reference point. read more
Working with large data sets, we often require the count of unique and distinct values in Excel. Though this may be required in many cases, Excel does not have any pre-defined formula to count unique and distinct values. In this tutorial, you will see a few techniques to count unique and distinct values in Excel.
How to Count Unique and Distinct Values in Excel
The unique values are the ones that appear only once in the list, without any duplications. The distinct values are all the different values in the list.
In this example, you have a list of numbers ranging from 1-6. The unique values are the ones that appear only once in the list, without any duplications. The distinct values are all the different values in the list. The tables below show the unique and distinct values in this list.
Count unique values in Excel
You can use the combination of the SUM and COUNTIF functions to count unique values in Excel. The syntax for this combined formula is = SUM(IF(1/COUNTIF(data, data)=1,1,0)). Here the COUNTIF formula counts the number of times each value in the range appears.
The resulting array looks like {1;2;1;1;1;1}. In the next step, you divide 1 by the resulting values. The IF function implements the logic such that if the values appear only once in the range, this step will generate 1, otherwise it will be a fraction value. The SUM function then sums all the values and returns the result. This is an array formula, so you have to assign it using Ctrl + Shift + Enter.
The following example contains a list of automobile products with their product ID and Names. You will count the unique items in this example.
{=SUM(IF(1/COUNTIF(A2:A11,A2:A11)=1,1,0))}
This counts the number of unique values in the list. It follows the syntax mentioned above and returns the count for unique items, which is 5.
{=SUM(IF(ISNUMBER(A2:A11)*COUNTIF(A2:A11,A2:A11)=1,1,0))}
This counts the number of unique numeric values in the list. The only difference with the previous formula is here is the nested ISNUMBER formula that makes sure that you only count the numeric values. Returns the number 3, which is the count of the unique numeric values.
{=SUM(IF(ISTEXT(A2:A10)*COUNTIF(A2:A11,A2:A11)=1,1,0))}
Works the same as the previous formula, counts the unique number of text values instead. The ISTEXT function is used to make sure that only the text values are counted. Returns the number 2.
Count Distinct Values in Excel
Count Distinct Values using a Filter
You can extract the distinct values from a list using the Advanced Filter dialog box and use the ROWS function to count the unique values. To count the distinct values from the previous example:
- Select the range of cells A1:A11.
- Go to Data > Sort & Filter > Advanced.
- In the Advanced Filter dialog box, click Copy to another location.
- Set both List Range and Criteria Range to $A$1:$A$11.
- Set Copy to to $F$2.
- Keep the Unique Records Only box checked. Click OK.
- Column F will now contain the distinct values.
- Select H6.
- Enter the formula
=ROWS(F3:F9)
. Click Enter.
This will show the count of distinct values, 7.
Count Distinct Values using Formulas
You can use the combination of the IF, MATCH, LEN and FREQUENCY functions with the SUM function to count distinct values.
{=SUM(IF(A2:A11<>"",1/COUNTIF(A2:A11, A2:A11), 0))}
Counts the number of distinct values between cells A2 to A11. Like the unique count, here the COUNTIF function returns a count for each individual value which is then used as a denominator to divide 1. The returning values are then summed if they are not 0. This gives you a count for the distinct values, regardless of their types.
=SUM(IF(FREQUENCY(MATCH(A2:A11,A2:A11,0),MATCH(A2:A11,A2:A11,0))>0,1))
Does the same thing as the previous formula. The only differences being the use of the FREQUENCY and MATCH functions. The frequency function returns the number of occurrences for a value for the first occurrence. For the next occurrence of that value, it returns 0. The MATCH function is used to return the position of a text value in the range. These are returned as then used as an argument to the FREQUENCY function which gives us a count of the total number of distinct values.
=SUM(IF(FREQUENCY(A2:A11,A2:A11)>0,1))
Counts the number of distinct numeric values. As the FREQUENCY function ignores text and blanks, it returns 5, which is the number of numeric values.
{=SUM(IF(ISTEXT(A2:A11),1/COUNTIF(A2:A11, A2:A11),""))}
Returns the count of distinct text values in a range. Like the first example, this counts the distinct values, but the ISTEXT function makes sure only the text values are taken into count.
Count Distinct Values using a Pivot Table
You can also count distinct values in Excel using a pivot table. To find the distinct count of the bike names from the previous example:
To count the distinct items using a pivot table:
- Select cells A1:B11. Go to Insert > Pivot Table.
- In the dialog box that pops up, check New Worksheet and Add this to the Data Model.
- Drag the Product ID field to Rows, Names field to Values in the PivotTable Fields.
- Right-click on any value in column B. Go to Value Field Settings. In the Summarize Values By tab, go to Summarize Value field by and set it to Distinct Count.
This will show the distinct count 7 in cell B11.
Still need some help with Excel formatting or have other questions about Excel? Connect with a live Excel expert here for some 1 on 1 help. Your first session is always free.