Tableau Unique Count: A Step-by-Step Guide
In this article
To get a unique count in Tableau, use the COUNTD function. Create a calculated field with the formula COUNTD([Field Name]) and drag it to a view. This returns the number of distinct values in that field, ignoring duplicates. The steps below walk you through creating the calculation, using it in a worksheet, and fixing common problems.
What You'll Need
- Tableau Desktop or Tableau Cloud/Server with a workbook open and a data source connected.
- A field containing the values you want to count distinctly (for example, Customer ID, Order Number, or Product Name).
- Basic familiarity with the Tableau interface: Data pane, shelves, and calculated fields.
Step-by-Step
- Open your workbook and connect to data. Launch Tableau and open the workbook that contains the data you want to analyze. If you are starting fresh, connect to your data source and go to a worksheet.
- Locate the field to count. In the Data pane (left side), find the dimension or measure you want to count uniquely. For example, if you want to know how many distinct customers placed orders, find the Customer ID field.
- Create a calculated field. Right-click anywhere in the Data pane and choose Create Calculated Field. A calculation editor window opens.
- Name the calculation. In the Name box at the top, type a descriptive name such as Unique Customers or Distinct Orders.
- Enter the COUNTD formula. In the formula area, type
COUNTD([Customer ID])(replace Customer ID with your actual field name). Tableau may suggest field names as you type. The formula is valid when the message at the bottom says the calculation is valid. - Click OK. The new calculated field appears in the Data pane under Measures (it will have an =# icon).
- Add the calculation to a view. Drag the new calculated field onto the Rows shelf or the Text card in the Marks pane. Tableau displays the unique count as a number.
- Optionally, break it down by category. Drag a dimension such as Region or Category onto the Columns shelf or Rows shelf to see the unique count per group.
- Check the aggregation. By default, COUNTD is an aggregate calculation, so it works at the level of detail of the view. If you need a row-level flag instead, use a different approach (see Tips).
- Format and save. Right-click the measure in the view and choose Format to adjust number formatting. Save the workbook.
Common Mistakes
- Using COUNT instead of COUNTD. COUNT counts all rows, including duplicates. COUNTD counts distinct values. If your number looks too high, check the formula.
- Counting a field with nulls. COUNTD ignores null values. If you need to include nulls as a distinct category, you may need to handle them separately, for example with a calculated field that replaces nulls.
- Forgetting the aggregation context. COUNTD is an aggregate; placing it on a shelf without any dimensions gives one total. Adding dimensions changes the level of detail and therefore the count per group.
- Using a measure that is not a unique identifier. Counting distinct values of a field like Sales Amount rarely makes sense because most values are unique. Use an ID field instead.
- Expecting COUNTD to work in all data sources the same way. Some data sources (especially live connections) may handle COUNTD differently or not support it. Tableau will show an error if the function is unsupported.
Tips
- Use COUNTD for distinct counts in calculated fields. It is the standard Tableau function for unique counts and works in most data sources.
- Create a row-level distinct flag. If you need to mark each row as the first occurrence of a value, use a calculated field like
IF INDEX() = 1 THEN 1 ELSE 0 ENDwith a table calculation, or useCOUNTDin a LOD expression. - Use Level of Detail (LOD) expressions for complex counts. For example,
{ FIXED [Region] : COUNTD([Customer ID]) }gives the unique customer count per region regardless of the view's dimensions. - Test with a small dataset. Before applying to a large dataset, verify the count on a few rows to ensure the formula is correct.
- Consider performance. COUNTD can be slower on large datasets. If performance is an issue, consider pre-aggregating the data or using extracts.
Troubleshooting
If the unique count does not appear as expected, work through these checks in order:
- Verify the calculation is valid. Open the calculated field and confirm there are no red error messages. Common issues include misspelled field names or missing brackets.
- Check the field type. COUNTD works on dimensions and measures, but the field must be a valid data type. If the field is a string, ensure it is not being treated as a measure incorrectly.
- Look at the aggregation. Right-click the measure in the view and check that it is set to Count (Distinct) if you used a direct field, or that your calculated field is being aggregated correctly.
- Test with a simple view. Remove all dimensions except one, and place only the COUNTD calculation on the Text card. If the number is correct, add dimensions back one at a time to find where it changes.
- Check for data source limitations. If you are using a live connection to a database that does not support COUNTD, switch to an extract or use a different method. Tableau's error message will indicate if the function is unsupported.
- Restart Tableau. If the interface behaves unexpectedly, close and reopen Tableau. Save your workbook first.
Frequently Asked Questions
What is the difference between COUNT and COUNTD in Tableau?
COUNT returns the number of rows, including duplicates. COUNTD returns the number of distinct values, ignoring duplicates and nulls.
Can I use COUNTD in a calculated field with conditions?
Yes. You can combine COUNTD with IF statements, for example COUNTD(IF [Sales] > 100 THEN [Customer ID] END) to count unique customers with sales over 100.
Does COUNTD work with all data sources?
Most data sources support COUNTD, but some live connections may not. If you get an error, try using an extract or check your data source documentation.
How do I count unique values per category?
Drag the category dimension to a shelf and place the COUNTD calculation on another shelf. Tableau computes the distinct count for each category.
Can I count unique values across multiple fields?
COUNTD works on a single field. To count unique combinations, create a calculated field that concatenates the fields, then apply COUNTD to that concatenated field.
Why does my COUNTD show a higher number than expected?
Check for leading or trailing spaces, case differences, or inconsistent formatting in the field. These can cause values that look identical to be counted as distinct.