Tableau function Pass-through (RAWSQL)

Tableau RAWSQL_REAL to SQL

RAWSQL_REAL("%2 / %1", [Units], [Sales]) sends Sales / Units to the database: %1 is the first argument after the string, %2 the second. The SQL is that expression with the columns put back. Division by a null gives null, and how division by zero behaves depends on the database.

Updated · Checked on the xmetric engine

What is the syntax?

RAWSQL_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 a real number per row: whatever the database returns for the expression. Null when the expression is null.

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 sales value per unit?

Tableau

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

SQL (DuckDB)

SELECT Sales / Units AS result
FROM input
ORDER BY id

%2 is Sales and %1 is Units, so the text means Sales / Units. Lines 2 and 6 have no sales and return null.

UnitsSalesResult
1224120.04
7null
58517
941045.56
360.2520.08
4null

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

How many units fit in each box?

Tableau

RAWSQL_REAL("%1 / %2", [Units], [Boxes])

SQL (DuckDB)

SELECT Units / Boxes AS result
FROM input
ORDER BY id

DuckDB's / always returns a fraction, so line 2 gives 3.5. On SQL Server or PostgreSQL the same text divides two integers and gives 3.

UnitsBoxesResult
1243
723.5
515
933
3null
441

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

Where does a straight translation go wrong?

Common mistake

Why do the ratios come out tiny?

Putting the arguments back in the order they were listed, instead of by their numbers, turns Sales / Units into Units / Sales. Read %2 / %1 as "second argument over first".

Tableau

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

Tempting SQL

SELECT Units / Sales AS result
FROM input
ORDER BY id

SQL that matches

SELECT Sales / Units AS result
FROM input
ORDER BY id
UnitsSalesTableauTempting SQLSQL that matches
1224120.040.0520.04
7nullnullnull
585170.0617
941045.560.0245.56
360.2520.080.0520.08
4nullnullnull

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 Sales / NULLIF(Units, 0) AS result
FROM input
ORDER BY id

Snowflake documentation

BigQuery

SELECT SAFE_DIVIDE(Sales, Units) AS result
FROM input
ORDER BY id

BigQuery documentation

What does your own RAWSQL_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 RAWSQL_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 happens when the divisor is zero?

It depends on the database the text runs on. DuckDB returns null; Snowflake and BigQuery raise a division-by-zero error, which is why the dialect SQL above uses NULLIF and SAFE_DIVIDE.

Where is this documented?