Tableau function Level of detail

Tableau FIXED invoice count per customer to SQL

{ FIXED [Customer] : COUNTD([Invoice]) } counts the distinct invoices of each customer, whatever the view shows. In SQL it is count(DISTINCT Invoice) per customer. Use COUNTD, not COUNT: an invoice with two lines is still one invoice.

Updated · Checked on the xmetric engine

What is the syntax?

{ FIXED [Customer] : COUNTD([Invoice]) }
dimensions
the fields the aggregate is grouped by, whatever the view shows.
aggregate
COUNTD([Invoice]) to count invoices; COUNT([Invoice]) counts lines.

Returns an integer per row: how many different invoices the customer has.

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 invoices does each customer have?

Tableau

{ FIXED [Customer] : COUNTD([Invoice]) }

SQL (DuckDB)

SELECT Customer, count(DISTINCT Invoice) AS result
FROM input
GROUP BY Customer
ORDER BY Customer

Holly's invoice INV-11 has two lines, so Holly has 1 invoice. Aster has 3, across two teams.

CustomerResult
Aster3
Birch1
Clover1
Dahlia2
Fern2
Gale1
Holly1

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

How many invoice lines does each customer have?

Tableau

{ FIXED [Customer] : COUNT([Invoice]) }

SQL (DuckDB)

SELECT Customer, count(Invoice) AS result
FROM input
GROUP BY Customer
ORDER BY Customer

COUNT counts every non-null line: Holly has 2.

CustomerResult
Aster3
Birch1
Clover1
Dahlia2
Fern2
Gale1
Holly2

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

Where does a straight translation go wrong?

Common mistake

Why does count(Invoice) give Holly 2 invoices?

Holly's one invoice has two lines. count() counts lines; COUNTD counts each invoice number once.

Tableau

{ FIXED [Customer] : COUNTD([Invoice]) }

Tempting SQL

SELECT Customer, count(Invoice) AS result
FROM input
GROUP BY Customer
ORDER BY Customer

SQL that matches

SELECT Customer, count(DISTINCT Invoice) AS result
FROM input
GROUP BY Customer
ORDER BY Customer
CustomerTableauTempting SQLSQL that matches
Aster333
Birch111
Clover111
Dahlia222
Fern222
Gale111
Holly121

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

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 Customer, COUNT(DISTINCT Invoice) AS result
FROM input
GROUP BY Customer
ORDER BY Customer

Snowflake documentation

BigQuery

SELECT Customer, COUNT(DISTINCT Invoice) AS result
FROM input
GROUP BY Customer
ORDER BY Customer

BigQuery documentation

What does your own Invoice count per customer formula return?

Edit the formula and run it on the 12 sample rows. Fields: [id], [Region], [Team], [Customer], [Invoice], [Revenue], [Date].

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

Which functions go with it?

What do people ask about Invoice count per customer?

Do filters change it?

Ordinary dimension filters on the Filters shelf do not: FIXED is computed before them. Add a filter to context (or filter the data source) to make it count only the rows that survive. In SQL, a context filter is a WHERE inside the subquery or window; an ordinary filter is a WHERE that does not reach it.

How do I count customers by number of invoices?

Make the FIXED field a dimension, put it on Columns, and put COUNTD([Customer]) on Rows: a purchase-frequency histogram.

Why FIXED rather than plain COUNTD?

With only Customer in the view they agree. FIXED keeps the per-customer count when the view is by Team, Region or month.

Where is this documented?