Tableau function Table calculation

Tableau RUNNING_COUNT to SQL

RUNNING_COUNT(SUM([Sales])) counts the marks from the start of the partition to the current one where the expression is not null. In SQL that is count(sum(Sales)) OVER (PARTITION BY Region ORDER BY Month). To count every mark, nulls included, use INDEX().

Updated · Checked on the xmetric engine

What is the syntax?

RUNNING_COUNT(expression)
expression
an aggregate, such as SUM([Sales]). Compute Using sets which marks it runs along and where it restarts.

Returns an integer: the number of non-null values of the expression from the first mark in the partition to the current mark.

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 do I count the months with sales so far, per region?

Tableau

RUNNING_COUNT(SUM([Sales]))

Compute using Month, restarting for each Region

SQL (DuckDB)

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

Compute using Month: East counts 1 to 4. West month 3 has no sales, so West stays at 2 there and reaches 3 in month 4.

RegionMonthResult
East11
East22
East33
East44
West11
West22
West32
West43

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

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

Tableau

RUNNING_COUNT(SUM([Sales]))

Compute using Region, restarting for each Month

SQL (DuckDB)

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

Compute using Region: the same formula now runs East then West inside each month.

RegionMonthResult
East11
East21
East31
East41
West12
West22
West31
West42

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

Where does a straight translation go wrong?

Common mistake

Why does count(*) give West one more?

count(*) counts rows whatever their values. RUNNING_COUNT counts non-null values of its expression, so West month 3 is not counted.

Tableau

RUNNING_COUNT(SUM([Sales]))

Compute using Month, restarting for each Region

Tempting SQL

SELECT
  Region,
  Month,
  count(*) OVER (PARTITION BY Region ORDER BY Month) 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 ORDER BY Month) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month
RegionMonthTableauTempting SQLSQL that matches
East1111
East2222
East3333
East4444
West1111
West2222
West3232
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 ORDER BY Month) 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 ORDER BY Month) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month

BigQuery documentation

What does your own RUNNING_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 RUNNING_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.

What happens to the month with no sales?

It is skipped: the running result carries the value from the months before it. West month 3 has no sales, so its running value equals month 2's. SQL aggregate windows skip nulls the same way.

How is RUNNING_COUNT different from INDEX()?

INDEX numbers every mark. RUNNING_COUNT skips marks where the expression is null; they agree when no mark is null.

How do I restart it every year?

Put the year in the partition: in Tableau, compute using Month only, with Year on the view; in SQL, PARTITION BY Region, Year ORDER BY Month.

Where is this documented?