Tableau function Table calculation

Tableau WINDOW_COUNT to SQL

WINDOW_COUNT(SUM([Sales])) counts the marks in the partition where the expression is not null. In SQL that is count(sum(Sales)) OVER (PARTITION BY Region). To count every mark, nulls included, use SIZE().

Updated · Checked on the xmetric engine

What is the syntax?

WINDOW_COUNT(expression, [start, end])
expression
an aggregate, such as SUM([Sales]).
start, end
optional offsets from the current mark that bound the window; without them it is the whole partition. FIRST() and LAST() work here.

Returns an integer: the number of non-null values of the expression over the marks in the window.

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 with sales does each region have?

Tableau

WINDOW_COUNT(SUM([Sales]))

Compute using Month, restarting for each Region

SQL (DuckDB)

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

Compute using Month: East has 4 marks with sales, West 3; West month 3 is a mark with no sales.

RegionMonthResult
East14
East24
East34
East44
West13
West23
West33
West43

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

How do I compute it across regions, within each month?

Tableau

WINDOW_COUNT(SUM([Sales]))

Compute using Region, restarting for each Month

SQL (DuckDB)

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

Compute using Region: the window is now the two regions of each month.

RegionMonthResult
East12
East22
East31
East42
West12
West22
West31
West42

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

What does the bounded form return?

With start -1 and end 1, the window moves with the current mark and is clipped at the edges of each region. Each result column is one mark, by Region and Month.

CaseTableau, then SQL (DuckDB)East 1East 2East 3East 4West 1West 2West 3West 4
bounded (-1, 1)WINDOW_COUNT(SUM([Sales]), -1, 1)Region, Month, count(sum(Sales)) OVER (PARTITION BY Region ORDER BY Month ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING)23322221

On every row, each Tableau formula and its SQL return the same value.

Where does a straight translation go wrong?

Common mistake

Why does count(*) give West 4?

count(*) counts rows, null or not. WINDOW_COUNT counts non-null values of its expression, and West month 3 has no sales.

Tableau

WINDOW_COUNT(SUM([Sales]))

Compute using Month, restarting for each Region

Tempting SQL

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

SQL that matches

SELECT
  Region,
  Month,
  count(sum(Sales)) OVER (PARTITION BY Region) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month
RegionMonthTableauTempting SQLSQL that matches
East1444
East2444
East3444
East4444
West1343
West2343
West3343
West4343

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

Snowflake documentation

BigQuery

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

BigQuery documentation

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

Why does the expression need SUM inside it?

A table calculation runs on the marks in the view, so its argument must be an aggregate. A bare field such as [Sales] raises an error; see All fields must be aggregate or constant.

Why no ORDER BY in the SQL?

Without start and end, a WINDOW_ function covers the whole partition. A SQL window with ORDER BY and no frame ends at the current row, so leave ORDER BY out, or write ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING.

What do the start and end arguments mean?

They are offsets from the current mark: -1, 1 covers the previous, current and next marks, and FIRST(), 0 runs from the first mark to the current one. In SQL they become ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING. Use ROWS, not RANGE: Tableau counts marks, and RANGE counts values of the ORDER BY column.

Where is this documented?