Tableau function Aggregate

Tableau COUNTD to SQL

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
GROUP BY Region
ORDER BY Region

East sells Chair three times, Desk and Lamp: 3 products. North names none: 0.

RegionResult
East3
North0
South1
West1

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
GROUP BY Region
ORDER BY Region

East has 100 twice, so 100, 300 and 50 make 3.

RegionResult
East3
North0
South1
West4

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
GROUP BY Region
ORDER BY Region

SQL that matches

SELECT Region, count(DISTINCT Product) AS result
FROM input
GROUP BY Region
ORDER BY Region
RegionTableauTempting SQLSQL that matches
East353
North000
South111
West141

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

SELECT sum(n) AS result
FROM (
  SELECT Region, count(DISTINCT Product) AS n
  FROM input
  GROUP BY Region
)

SQL that matches

SELECT count(DISTINCT Product) AS result
FROM input
TableauTempting SQLSQL that matches
353

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.

CaseTableau, then SQL (DuckDB)EastNorthSouthWest
all-null groupCOUNTD([Product])Region, count(DISTINCT Product)3011

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
GROUP BY Region
ORDER BY Region

Snowflake documentation

BigQuery

SELECT Region, COUNT(DISTINCT Product) AS result
FROM input
GROUP BY Region
ORDER BY Region

BigQuery documentation

What does your own COUNTD formula return?

Edit the formula and run it on the 12 sample rows. Fields: [id], [Region], [Product], [Sales], [Units].

Runs in your browser on the xmetric engine. Nothing is uploaded.

Which functions go with it?

What do people ask about COUNTD?

Does COUNTD count null as a value?

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?

Use { FIXED [Region] : COUNTD([Customer]) }. See Distinct customers per region.

Where is this documented?