Tableau function Table calculation

Tableau SIZE to SQL

SIZE() returns how many marks the current partition has. With Region and Month on the view and Compute Using Month, each region has four marks, so every row gets 4. In SQL it is count(*) OVER (PARTITION BY Region) on the grouped marks, not on the raw rows.

Updated · Checked on the xmetric engine

What is the syntax?

SIZE()

Returns an integer: the number of marks in the current partition.

What does it return, and what is the SQL?

Each example runs on the same 9 sample rows twice: the Tableau formula on the xmetric engine, and the SQL in DuckDB.

How many months does each region have?

Tableau

SIZE()

Compute using Month, restarting for each Region

SQL (DuckDB)

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

Compute using Month: four marks per region, including West's month with no sales.

RegionMonthResult
East14
East24
East34
East44
West14
West24
West34
West44

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

How many regions are there in each month?

Tableau

SIZE()

Compute using Region, restarting for each Month

SQL (DuckDB)

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

Compute using Region: the partition is now a month, holding two marks.

RegionMonthResult
East12
East22
East32
East42
West12
West22
West32
West42

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

Where does a straight translation go wrong?

Common mistake

Why does the SQL give East five instead of four?

Counting before grouping counts source rows. East has two rows in month 2, so a window over the raw table sees five rows; Tableau sees four marks.

Tableau

SIZE()

Compute using Month, restarting for each Region

Tempting SQL

SELECT
  DISTINCT Region,
  Month,
  count(*) OVER (PARTITION BY Region) AS result
FROM input
ORDER BY Region, Month

SQL that matches

SELECT
  Region,
  Month,
  count(*) OVER (PARTITION BY Region) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month
RegionMonthTableauTempting SQLSQL that matches
East1454
East2454
East3454
East4454
West1444
West2444
West3444
West4444

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
  Region,
  Month,
  COUNT(*) OVER (PARTITION BY Region) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month

Snowflake documentation

BigQuery

SELECT
  Region,
  Month,
  COUNT(*) OVER (PARTITION BY Region) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month

BigQuery documentation

What does your own SIZE formula return?

Edit the formula and run it on the 9 sample rows. Fields: [id], [Region], [Month], [Sales], [Profit].

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

Which functions go with it?

What do people ask about SIZE?

Why GROUP BY Region, Month in the SQL?

Tableau computes the table calculation on marks, one per Region and Month in this view. The GROUP BY builds those marks; East has two rows in month 2 (80 and 40), and they become one mark of 120.

Does SIZE() skip null measures?

No. It counts marks, whatever their values. West month 3 has no sales and is still one of West's four marks. To count non-null values, use WINDOW_COUNT.

How do I flag regions with fewer than four months of data?

SIZE() < 4 in Tableau; in SQL, count(*) OVER (PARTITION BY Region) < 4 in the grouped query.

Where is this documented?