Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

COUNTIF in Tableau: How to Count Conditionally

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Tableau does not have a COUNTIF function, but you can get the same result with a calculated field that returns 1 or 0 and then summing it. The shortest working method is: create a calculated field such as IF [Status] = "Open" THEN 1 ELSE 0 END, drag it to a view, and set the aggregation to SUM. That sum is your conditional count. Alternatively, drag the dimension you want to test onto the Filters shelf, filter to the value you want, then drag any field onto Text and set it to COUNT. Both approaches are valid; the calculated field is more reusable and works in more places.

What You'll Need

  • Tableau Desktop or Tableau Public (the steps below apply to both; menu labels can differ slightly by version, edition or region).
  • A data source already connected and at least one dimension and one measure you can test against.
  • A basic understanding of calculated fields and how Tableau aggregates measures.

Step-by-Step

  1. Decide what you are counting. COUNTIF answers a question like "how many orders have a status of Open?" or "how many customers are in the West region?" Write the condition down before you build anything. You need the field to test and the value to test against.
  2. Open the calculation editor. In Tableau Desktop, click Analysis in the top menu and choose Create Calculated Field. You can also right-click in the Data pane and choose Create Calculated Field. In Tableau Public the same option is available from the Data pane context menu. Labels can vary slightly between versions.
  3. Name the field something clear. Use a name that describes the count, such as "Open Orders Count" or "West Customers". Avoid generic names like "Calc1" because you will likely build several of these.
  4. Write the conditional flag. The pattern is IF [Field] = "Value" THEN 1 ELSE 0 END. For a numeric test use IF [Sales] > 1000 THEN 1 ELSE 0 END. For a null-safe test use IF ISNULL([Field]) THEN 1 ELSE 0 END. Press OK. Tableau will show the field in the Data pane with an equals sign icon, meaning it is a calculated field.
  5. Place the field on the view. Drag the new calculated field onto the Text card, the Rows shelf, or the Columns shelf. Tableau will default to SUM aggregation because the field returns numbers. If it defaults to something else, right-click the pill and choose Measure > Sum.
  6. Add the dimension you want to group by. If you want a count per category, drag that dimension (for example Region or Product Category) onto Rows. The calculated field then shows the conditional count for each category.
  7. Verify the total. Cross-check one value by filtering the underlying data to that condition and reading the row count in the status bar. If the numbers match, your calculation is correct.
  8. Optional: use COUNTD for distinct counts. If you need to count unique values that meet a condition, use COUNTD(IF [Condition] THEN [Customer ID] END). Tableau ignores the nulls produced when the condition is false, so this returns a distinct count of only the matching rows. This is the closest equivalent to a conditional distinct count.
  9. Optional: use a filter-based count instead. Drag the dimension you want to test onto the Filters shelf, select the value you want, click OK, then drag any field onto Text and change the aggregation to COUNT. This counts rows after filtering. It is faster to set up but less reusable, because the filter applies to the whole view.
  10. Save and reuse. Once the calculated field works, you can use it in other sheets, dashboards and tooltips. Right-click the field in the Data pane and choose Add to Saved Calculations if you want to keep it handy across workbooks.

Common Mistakes

  • Using COUNTIF as a function name. Tableau will return an error because COUNTIF does not exist. Use the IF/THEN/1/ELSE/0 pattern or a filtered COUNT instead.
  • Forgetting the ELSE 0. If you write IF [Status] = "Open" THEN 1 END, Tableau returns null for non-matching rows. SUM ignores nulls, which is often fine, but COUNT does not, and mixing the two can confuse results. Including ELSE 0 makes the behavior explicit.
  • Leaving the aggregation as COUNT instead of SUM. A calculated field returning 1 or 0 counted with COUNT gives the total row count, not the conditional count. Always check the pill's aggregation.
  • Comparing text to a number. If the field is a string, quote the value: "100", not 100. If it is numeric, do not quote it. Mismatched types cause errors or silent mismatches.
  • Ignoring case and whitespace. Tableau string comparisons are case-sensitive in many data sources. Trim spaces with TRIM and consider UPPER or LOWER if your data is inconsistent.
  • Applying a filter that hides rows you wanted to count. A filter on the Filters shelf removes rows before the calculation runs. Use context filters or table calculations only when you understand the order of operations.

Tips

  • Use IF [Field] IN ("Open", "Pending") THEN 1 ELSE 0 END to count multiple values in one field.
  • Use IF [Sales] > 1000 AND [Region] = "West" THEN 1 ELSE 0 END for multi-condition counts.
  • Name calculated fields with a prefix like "Flag - " so they are easy to find in the Data pane.
  • For percentages, divide the conditional count by a total count using a calculated field, then format as a percentage.
  • If you need the count to respond to a parameter, replace the hard-coded value with a parameter reference, for example IF [Status] = [Status Parameter] THEN 1 ELSE 0 END.

Troubleshooting

If the calculated field shows an error, check the field names and brackets first. Tableau requires square brackets around field names and double quotes around text values. If the field returns all zeros, confirm the comparison value matches the data exactly, including case and trailing spaces. If the number looks too high, check whether a filter or a join is duplicating rows. If the number looks too low, check whether nulls are being excluded and whether the aggregation is SUM rather than COUNT. If the field appears greyed out, it may be invalid because a referenced field was renamed or removed; edit the calculation and re-select the field. If the result changes when you add a filter, that is expected: filters run before the calculation in most cases. If you need the count to ignore a filter, right-click the filter and choose Add to Context.

Frequently Asked Questions

Does Tableau have a COUNTIF function?

No. Tableau does not include COUNTIF. The standard workaround is a calculated field that returns 1 or 0 and is summed, or a filtered COUNT.

Can I count distinct values that meet a condition?

Yes. Use COUNTD(IF [Condition] THEN [Field] END). The nulls produced when the condition is false are ignored, so you get a distinct count of matching rows only.

Why does my calculated field show the total row count instead of the conditional count?

The pill is probably set to COUNT rather than SUM. Right-click the pill and change the aggregation to Sum.

Can I use COUNTIF with multiple conditions?

Yes. Combine conditions with AND or OR inside the IF statement, for example IF [Region] = "West" AND [Sales] > 500 THEN 1 ELSE 0 END.

Does COUNTIF work in Tableau Public?

Yes. Tableau Public supports calculated fields and the same IF/THEN pattern. Menu labels can differ slightly from Tableau Desktop.

What is the difference between COUNT and COUNTD in Tableau?

COUNT returns the number of rows, including duplicates. COUNTD returns the number of unique values. Both can be combined with an IF statement for conditional results.