COUNTD(expression) returns how many different non-null values the group has. In SQL it is count(DISTINCT Product). Nulls are not counted, and a value that repeats counts once.
Updated · Checked on the xmetric engine
What is the syntax?
COUNTD(expression)
expression
a field of any type, or a row-level calculation.
Returns an integer: the number of distinct non-null values. 0 when every value is null.
What does it return, and what is the SQL?
Each example runs on the same 12 sample rows twice: the Tableau formula on the xmetric engine, and the SQL in DuckDB.
How many different products does each region sell?
Tableau
COUNTD([Product])
SQL (DuckDB)
SELECT Region, count(DISTINCT Product) AS result
FROM input
GROUPBY Region
ORDERBY Region
East sells Chair three times, Desk and Lamp: 3 products. North names none: 0.
Region
Result
East
3
North
0
South
1
West
1
✓ The Tableau formula and the SQL return the same 4 rows.
How many different sales amounts are there?
Tableau
COUNTD([Sales])
SQL (DuckDB)
SELECT Region, count(DISTINCT Sales) AS result
FROM input
GROUPBY Region
ORDERBY Region
East has 100 twice, so 100, 300 and 50 make 3.
Region
Result
East
3
North
0
South
1
West
4
✓ The Tableau formula and the SQL return the same 4 rows.
Where does a straight translation go wrong?
Common mistake
Why does count(Product) count Chair three times?
count() counts every non-null value. COUNTD counts each value once, so East has 3 products, not 5 lines.
Tableau
COUNTD([Product])
Tempting SQL
SELECT Region, count(Product) AS result
FROM input
GROUPBY Region
ORDERBY Region
SQL that matches
SELECT Region, count(DISTINCT Product) AS result
FROM input
GROUPBY Region
ORDERBY Region
Region
Tableau
Tempting SQL
SQL that matches
East
3
5
3
North
0
0
0
South
1
1
1
West
1
4
1
✓ The SQL that matches returns the Tableau result on every row; the marked cells are where the tempting SQL differs.
Common mistake
Why can't I add up distinct counts from smaller groups?
A value can appear in several groups. Counting distinct products per region and adding the four regions counts Chair in East and South twice: 5, where COUNTD over the whole table is 3.
Tableau
COUNTD([Product])
Tempting SQL
SELECTsum(n) AS result
FROM (
SELECT Region, count(DISTINCT Product) AS n
FROM input
GROUPBY Region
)
SQL that matches
SELECTcount(DISTINCT Product) AS result
FROM input
Tableau
Tempting SQL
SQL that matches
3
5
3
✓ The SQL that matches returns the Tableau result on every row; the marked cells are where the tempting SQL differs.
What does COUNTD return for a group with only nulls?
North's two lines name no product. COUNTD returns 0 there. Each result column is one mark, by Region.
Case
Tableau, then SQL (DuckDB)
East
North
South
West
all-null group
COUNTD([Product])Region, count(DISTINCT Product)
3
0
1
1
✓ On every row, each Tableau formula and its SQL return the same value.
How do I write it in Snowflake or BigQuery?
The first example, written for each warehouse from its documentation. These are not run here; DuckDB is.
Snowflake
SELECT Region, COUNT(DISTINCT Product) AS result
FROM input
GROUPBY Region
ORDERBY Region
No. North's lines have no product, and COUNTD returns 0 for North. SQL count(DISTINCT x) also skips nulls; SELECT DISTINCT does not, so don't count the rows of a SELECT DISTINCT.
Why is COUNTD slow on big data?
An exact distinct count has to keep every value it has seen. If an estimate is enough, Snowflake and BigQuery have APPROX_COUNT_DISTINCT, and DuckDB has approx_count_distinct.
How do I count distinct customers per region at a fixed level?