How to Count Distinct Values in Excel
In this article
Yes, you can count distinct values in Excel. The standard way is using the SUMPRODUCT and COUNTIF functions together, or the newer UNIQUE and FILTER functions in modern Excel. This method counts how many unique items appear in a selected range, ignoring duplicates, which is essential for accurate reporting on customer lists, product IDs, or any dataset where each entry should be counted only once.
What You'll Need
- A computer with Microsoft Excel installed (Desktop, Web, or Mac versions).
- A dataset containing the information you want to analyze, such as a list of names, dates, or product SKUs.
- Basic familiarity with Excel formulas and functions (copying and pasting is sufficient).
Step-by-Step
You can use one of two main methods. The SUMPRODUCT method works on almost all versions of Excel. The UNIQUE method is faster and cleaner but requires Excel 2021 or Microsoft 365.
Method 1: The SUMPRODUCT Formula (Works in All Versions)
- Identify your data. Click on the cell where you want the distinct count to appear. This cell should be empty or contain a header. Ensure your data is in a contiguous column or range. For this example, assume your data is in cells
A2:A100. - Enter the formula. Type the following formula into the cell, replacing
A2:A100with your actual data range:=SUMPRODUCT(1/COUNTIF(A2:A100, A2:A100))This formula works by creating a temporary array for each cell. It counts how many times each value appears in the entire range and then divides 1 by that count. The SUMPRODUCT adds up all the resulting 1s, effectively counting each unique value once. - Verify the result. Press Enter. The cell will display the number of distinct items. If you see a
#DIV/0!error, it means your data range is empty or contains only one cell. If you see a#VALUE!error, ensure your data does not contain non-numeric text mixed with numbers in the same range. - Handle errors gracefully (Optional). For very large datasets, the formula can sometimes be slow or error-prone if a cell is blank. A more robust version of this formula handles blank cells automatically:
=SUMPRODUCT((A2:A100<>&"")/COUNTIF(A2:A100, A2:A100&""))This version checks that the cell is not empty (<>"") before performing the division, preventing the error.
Method 2: The UNIQUE and FILTER Functions (Excel 2021 / Microsoft 365)
- Identify your data. Select the cell where you want the distinct count.
- Enter the formula. Type the following formula:
=ROWS(UNIQUE(FILTER(A2:A100, A2:A100<>&"")))This is a three-step process broken into one formula.- UNIQUE: Takes the range
A2:A100and removes duplicate values, returning a list of only the unique items. - FILTER: Removes any blank rows from the list to ensure accurate counting.
- ROWS: Calculates the total number of rows in the resulting list, which is your distinct count.
- UNIQUE: Takes the range
- Verify the result. Press Enter. The result should match the count from the SUMPRODUCT method. This method is generally faster and easier to read.
Common Mistakes
- Mixing data types. If your range contains both numbers and text (e.g., “100” and 100), the formula may fail or count them as distinct. Ensure your data is formatted consistently, usually as text or numbers, but not both.
- Using the standard COUNT function. Using
=COUNT(A2:A100)will count every cell, including duplicates, not just unique ones. It is a common error to use the standard count when looking for distinct values. - Forgetting to adjust the range. When copying the formula to other cells, ensure the range reference is absolute (using dollar signs like
$A$2:$A$100) if you want to keep counting the same dataset, or adjust the range for each new calculation. - Ignoring errors. If your data contains logical values (TRUE/FALSE) or errors like
#N/A, the SUMPRODUCT formula will return a#DIV/0!error. You must clean your data or use the error-handling version of the formula.
Tips
- Use named ranges. For long or complex datasets, define a named range (e.g., “MyData”) for your data column. This makes the formula much easier to read and maintain:
=SUMPRODUCT(1/COUNTIF(MyData, MyData)). - Press Ctrl+Shift+Enter. While not strictly necessary for SUMPRODUCT, older array formulas required this key combination. If you find yourself using older array formulas, this is a good habit to revisit.
- Sort your data. Sorting your data alphabetically or numerically can help you visually verify that the distinct count is correct by spotting missing or repeated items.
- Check for hidden characters. Sometimes, distinct values appear to be duplicates because of hidden spaces or non-breaking spaces. Use the TRIM function to clean your data before counting.
Troubleshooting
If the formula does not go to plan, follow these steps in order.
If you get a #DIV/0! Error
This usually means the formula is dividing by zero. In the context of the SUMPRODUCT formula, this happens if the range is empty or if you have a single data point. Ensure your data range A2:A100 actually contains data. If your data contains blank cells, use the robust formula =SUMPRODUCT((A2:A100<>&"")/COUNTIF(A2:A100, A2:A100&"")).
If you get a #VALUE! Error
This indicates that the formula contains text or something other than numbers. Ensure your data range is entirely numbers or entirely text, and that there are no mixed data types. If you are using the UNIQUE method, ensure your Excel version is 2021 or newer.
If the result is lower than expected
Check for hidden characters or formatting issues. Sometimes, values that look the same are actually different due to leading or trailing spaces. Use the TRIM() function on your data column first, or the CLEAN() function to remove non-printable characters.
If the result is higher than expected
This is usually due to counting blank cells as a distinct value. Use the FILTER function in the UNIQUE method (=ROWS(UNIQUE(FILTER(A2:A100, A2:A100<>&"")))) or the robust SUMPRODUCT formula to exclude blanks.
Frequently Asked Questions
Does the SUMPRODUCT formula work on Excel 2010?
Yes, the SUMPRODUCT and COUNTIF combination works on all modern versions of Excel, including Excel 2010, 2013, 2016, and 2019. It is the standard method for distinct counting on older versions.
Can I count distinct values across multiple columns?
You can, but you must combine the columns into one range first. For example, if you have data in columns A and B, you can use =SUMPRODUCT(1/COUNTIF(A2:B100, A2:B100)) to count distinct values across both columns.
Why does the formula return 0?
A result of 0 indicates an error or an empty range. Check for the #DIV/0! or #VALUE! errors first. If the range is truly empty, the formula will return 0.
Is there a way to count distinct values based on a condition?
Yes, you can use the COUNTIFS function within the SUMPRODUCT formula. For example, to count distinct values in column A where column B is “Yes”, you would use: =SUMPRODUCT(1/COUNTIFS(A2:A100, A2:A100, B2:B100, "Yes")).
Does the UNIQUE function work on older versions of Excel?
No, the UNIQUE function is a dynamic array function introduced in Excel 2021 and Microsoft 365. It will not work on Excel 2019 or earlier versions. You must use the SUMPRODUCT method on those versions.