Tableau function Pass-through (RAWSQL)

Tableau RAWSQL_INT to SQL

RAWSQL_INT("%1 * %2", [Units], [Boxes]) sends Units * Boxes to the database and reads the answer as a whole number. To translate it, swap each %n for the n-th argument after the SQL string. The numbers point at arguments, not at the order the placeholders appear in the text.

Updated · Checked on the xmetric engine

What is the syntax?

RAWSQL_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 an integer per row, read from 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.

How many units went out across all boxes?

Tableau

RAWSQL_INT("%1 * %2", [Units], [Boxes])

SQL (DuckDB)

SELECT Units * Boxes AS result
FROM input
ORDER BY id

Line 5 has no box count, so the product is null.

UnitsBoxesResult
12448
7214
515
9327
3null
4416

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

How long is each product name?

Tableau

RAWSQL_INT("length(%1)", [Product])

SQL (DuckDB)

SELECT length(Product) AS result
FROM input
ORDER BY id

length is DuckDB's name. On SQL Server the same text would need LEN, which is the dialect risk in one word.

ProductResult
Desk4
Lamp4
Chair5
Shelf5
null
Desk4

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

Where does a straight translation go wrong?

Common mistake

Why does reading the placeholders left to right flip the sign?

In "%2 - %1" the first placeholder in the text is %2, which is the second argument, Units. Substituting in reading order gives Boxes - Units and every sign flips. Match each number to its argument.

Tableau

RAWSQL_INT("%2 - %1", [Boxes], [Units])

Tempting SQL

SELECT Boxes - Units AS result
FROM input
ORDER BY id

SQL that matches

SELECT Units - Boxes AS result
FROM input
ORDER BY id
UnitsBoxesTableauTempting SQLSQL that matches
1248−88
725−55
514−44
936−66
3nullnullnull
44000

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 Units * Boxes AS result
FROM input
ORDER BY id

Snowflake documentation

BigQuery

SELECT Units * Boxes AS result
FROM input
ORDER BY id

BigQuery documentation

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

Where is this documented?