Tableau function Table calculation

Tableau RUNNING_AVG to SQL

RUNNING_AVG(SUM([Sales])) averages the current mark and every mark before it in the partition. In SQL that is avg(sum(Sales)) OVER (PARTITION BY Region ORDER BY Month). Null marks are skipped, so they count in neither the sum nor the number of marks.

Updated · Checked on the xmetric engine

What is the syntax?

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

Returns a number: the average 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 get a running average per region, month by month?

Tableau

RUNNING_AVG(SUM([Sales]))

Compute using Month, restarting for each Region

SQL (DuckDB)

SELECT
  Region,
  Month,
  avg(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 averages 100, then (100 + 120) / 2 = 110, and so on. West month 3 has no sales and stays at 60.

RegionMonthResult
East1100
East2110
East393.33
East4100
West150
West260
West360
West470

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

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

Tableau

RUNNING_AVG(SUM([Sales]))

Compute using Region, restarting for each Month

SQL (DuckDB)

SELECT
  Region,
  Month,
  avg(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
East1100
East2120
East360
East4120
West175
West295
West360
West4105

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

Where does a straight translation go wrong?

Common mistake

Why does dividing the running total by the row number come out low?

row_number() counts every mark, including West month 3 with no sales, so West's month 3 is 120 / 3 = 40. RUNNING_AVG divides by the number of non-null marks: 120 / 2 = 60.

Tableau

RUNNING_AVG(SUM([Sales]))

Compute using Month, restarting for each Region

Tempting SQL

SELECT
  Region,
  Month,
  sum(sum(Sales)) OVER (PARTITION BY Region ORDER BY Month)
    / row_number() 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,
  avg(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
East1100100100
East2110110110
East393.3393.3393.33
East4100100100
West1505050
West2606060
West3604060
West47052.570

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,
  AVG(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,
  AVG(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_AVG 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_AVG?

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.

Is RUNNING_AVG the same as averaging the rows so far?

No. It averages marks. East month 2 is one mark of 120 (80 + 40), not two rows of 80 and 40.

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?