Tableau function Pass-through (RAWSQL)

Tableau RAWSQLAGG_INT to SQL

RAWSQLAGG_INT("SUM(%1)", [Units]) sends SUM(Units) to the database once per mark and reads a whole number back. In SQL, write the aggregate with the column in place of %1 and GROUP BY the view's dimensions. COUNT(%1) counts non-null values only, which is not the same as counting rows.

Updated · Checked on the xmetric engine

What is the syntax?

RAWSQLAGG_INT("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 integer 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.

How many units did each region order?

Tableau

RAWSQLAGG_INT("SUM(%1)", [Units])

SQL (DuckDB)

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

One row per region, as in a view with Region on Rows.

RegionResult
East24
North4
West12

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

How many lines in each region have a sales value?

Tableau

RAWSQLAGG_INT("COUNT(%1)", [Sales])

SQL (DuckDB)

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

North's only line has no sales, so it counts 0.

RegionResult
East2
North0
West2

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

Where does a straight translation go wrong?

Common mistake

Why does count(*) count lines with no sales?

COUNT(%1) with [Sales] counts the rows where Sales is not null. count(*) counts every row, so East gets 3 instead of 2 and North 1 instead of 0.

Tableau

RAWSQLAGG_INT("COUNT(%1)", [Sales])

Tempting SQL

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

SQL that matches

SELECT Region, count(Sales) AS result
FROM input
GROUP BY Region
ORDER BY Region
RegionTableauTempting SQLSQL that matches
East232
North010
West222

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

Snowflake documentation

BigQuery

SELECT Region, SUM(Units) AS result
FROM input
GROUP BY Region
ORDER BY Region

BigQuery documentation

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

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?