{ 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
GROUPBY Customer
ORDERBY Customer
Holly's invoice INV-11 has two lines, so Holly has 1 invoice. Aster has 3, across two teams.
Customer
Result
Aster
3
Birch
1
Clover
1
Dahlia
2
Fern
2
Gale
1
Holly
1
✓ 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
GROUPBY Customer
ORDERBY Customer
COUNT counts every non-null line: Holly has 2.
Customer
Result
Aster
3
Birch
1
Clover
1
Dahlia
2
Fern
2
Gale
1
Holly
2
✓ 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
GROUPBY Customer
ORDERBY Customer
SQL that matches
SELECT Customer, count(DISTINCT Invoice) AS result
FROM input
GROUPBY Customer
ORDERBY Customer
Customer
Tableau
Tempting SQL
SQL that matches
Aster
3
3
3
Birch
1
1
1
Clover
1
1
1
Dahlia
2
2
2
Fern
2
2
2
Gale
1
1
1
Holly
1
2
1
✓ 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
GROUPBY Customer
ORDERBY Customer
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.