Tableau function Pass-through (RAWSQL)

Tableau RAWSQLAGG_REAL to SQL

RAWSQLAGG_REAL("AVG(%1)", [Sales]) sends AVG(Sales) to the database once per mark. In SQL, write the aggregate with the column in place of %1 and GROUP BY the view's dimensions. Translate the aggregate exactly: a ratio of sums is not an average of ratios, and AVG skips nulls.

Updated · Checked on the xmetric engine

What is the syntax?

RAWSQLAGG_REAL("sql_expression", [argument1], [argument2], ...)
sql_expression
a string constant of SQL in your data source's dialect. %1, %2, ... are placeholders for the arguments that follow, numbered from 1.
argument, ...
fields or expressions. Tableau puts the first one where %1 is, the second where %2 is, and so on.

Returns one real number per mark: the database's answer to an aggregate expression, grouped by the view's dimensions.

What does it return, and what is the SQL?

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

What is the average sale per region?

Tableau

RAWSQLAGG_REAL("AVG(%1)", [Sales])

SQL (DuckDB)

SELECT Region, avg(Sales) AS result
FROM input
GROUP BY Region
ORDER BY Region

East averages its two lines with a sales value; the line with none is left out.

RegionResult
East163
Northnull
West235

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

What is the sales value per unit for each region?

Tableau

RAWSQLAGG_REAL("SUM(%1) / SUM(%2)", [Sales], [Units])

SQL (DuckDB)

SELECT Region, sum(Sales) / sum(Units) AS result
FROM input
GROUP BY Region
ORDER BY Region

%1 is Sales, %2 is Units. Both sums run over the same rows, including East's line with no sales value.

RegionResult
East13.56
Northnull
West39.19

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

Where does a straight translation go wrong?

Common mistake

Why is the average of line prices different from the region price?

The text divides two totals. Averaging each line's Sales / Units gives every line the same weight, whatever its size, and skips the line with no sales value: East moves from 13.56 to 18.52.

Tableau

RAWSQLAGG_REAL("SUM(%1) / SUM(%2)", [Sales], [Units])

Tempting SQL

SELECT Region, avg(Sales / Units) AS result
FROM input
GROUP BY Region
ORDER BY Region

SQL that matches

SELECT Region, sum(Sales) / sum(Units) AS result
FROM input
GROUP BY Region
ORDER BY Region
RegionTableauTempting SQLSQL that matches
East13.5618.5213.56
Northnullnullnull
West39.1932.8239.19

The SQL that matches returns the Tableau result on every row; the marked cells are where the tempting SQL differs.

Common mistake

Why does dividing by count(*) lower the average?

AVG ignores nulls. Dividing the sum by every row counts East's line with no sales as a zero and drops its average from 162.75 to 108.5.

Tableau

RAWSQLAGG_REAL("AVG(%1)", [Sales])

Tempting SQL

SELECT Region, sum(Sales) / count(*) AS result
FROM input
GROUP BY Region
ORDER BY Region

SQL that matches

SELECT Region, avg(Sales) AS result
FROM input
GROUP BY Region
ORDER BY Region
RegionTableauTempting SQLSQL that matches
East163109163
Northnullnullnull
West235235235

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

Snowflake documentation

BigQuery

SELECT Region, AVG(Sales) AS result
FROM input
GROUP BY Region
ORDER BY Region

BigQuery documentation

What does your own RAWSQLAGG_REAL formula return?

Edit the formula and run it on the 6 sample rows. Fields: [id], [Region], [Product], [Units], [Boxes], [Sales], [Ordered], [Shipped].

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

Which functions go with it?

What do people ask about RAWSQLAGG_REAL?

What does %1 stand for?

The first argument after the SQL string. %2 is the second, and so on, whatever order the placeholders appear in the text. To translate a RAWSQL call, replace each %n with the column it points at and keep the rest of the text.

Does Tableau check the SQL inside RAWSQL?

No. Tableau passes the text to the database as it is, so a typo or a function your database lacks fails at query time, in the database. The text is also tied to that database's dialect: point the workbook at a different database and the same text can fail or mean something else.

What is the difference between RAWSQL and RAWSQLAGG?

RAWSQL_ functions are row-level: the expression runs once per row. RAWSQLAGG_ functions hold an aggregate such as SUM(%1) and run once per mark, grouped by the dimensions in the view. In SQL the first is a plain column expression; the second needs a GROUP BY on the view's dimensions.

Where is this documented?