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.
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
ORDERBY id
Line 5 has no box count, so the product is null.
Units
Boxes
Result
12
4
48
7
2
14
5
1
5
9
3
27
3
null
4
4
16
✓ 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)
SELECTlength(Product) AS result
FROM input
ORDERBY id
length is DuckDB's name. On SQL Server the same text would need LEN, which is the dialect risk in one word.
Product
Result
Desk
4
Lamp
4
Chair
5
Shelf
5
null
Desk
4
✓ 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
ORDERBY id
SQL that matches
SELECT Units - Boxes AS result
FROM input
ORDERBY id
Units
Boxes
Tableau
Tempting SQL
SQL that matches
12
4
8
−8
8
7
2
5
−5
5
5
1
4
−4
4
9
3
6
−6
6
3
null
null
null
4
4
0
0
0
✓ 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
ORDERBY id
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.