Tableau function Aggregate

Tableau COUNT to SQL

COUNT(expression) returns how many non-null values the group has. In SQL that is count(Sales), never count(*): count(*) counts rows, including rows where the field is empty. To count each value once, use COUNTD.

Updated · Checked on the xmetric engine

What is the syntax?

COUNT(expression)
expression
a field of any type, or a row-level calculation.

Returns an integer: the number of 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 lines have a sales value, per region?

Tableau

COUNT([Sales])

SQL (DuckDB)

SELECT Region, count(Sales) AS result
FROM input
GROUP BY Region
ORDER BY Region

East has five lines and four sales values. North has two lines and no values, so 0.

RegionResult
East4
North0
South1
West4

The Tableau formula and the SQL return the same 4 rows.

How many lines name a product?

Tableau

COUNT([Product])

SQL (DuckDB)

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

COUNT works on text too. The two North lines have no product.

RegionResult
East5
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(*) give a bigger number?

count(*) counts rows. Tableau's COUNT skips nulls, so East is 4, not 5, and North is 0, not 2.

Tableau

COUNT([Sales])

Tempting SQL

SELECT Region, count(*) AS result
FROM input
GROUP BY Region
ORDER BY Region

SQL that matches

SELECT Region, count(Sales) AS result
FROM input
GROUP BY Region
ORDER BY Region
RegionTableauTempting SQLSQL that matches
East454
North020
South111
West444

The SQL that matches returns the Tableau result on every row; the marked cells are where the tempting SQL differs.

What does COUNT return for a group with only nulls?

North has two order lines and no sales value on either. COUNT returns 0, not null. Each result column is one mark, by Region.

CaseTableau, then SQL (DuckDB)EastNorthSouthWest
all-null groupCOUNT([Sales])Region, count(Sales)4014

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(Sales) AS result
FROM input
GROUP BY Region
ORDER BY Region

Snowflake documentation

BigQuery

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

BigQuery documentation

What does your own COUNT 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 COUNT?

How do I count rows in Tableau?

Count a field that is never null, such as an ID, or use the generated count of the table (COUNT([Orders])). In SQL that is count(*).

Does COUNT return null for an empty group?

No, it returns 0. North has no sales values and COUNT returns 0, where SUM and AVG return null.

How is COUNT different from COUNTD?

COUNT counts every non-null value; COUNTD counts each distinct value once. East has sales 100, 300, 50 and 100: COUNT is 4, COUNTD is 3.

Where is this documented?