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.
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
ORDERBY id
%2 is Sales and %1 is Units, so the text means Sales / Units. Lines 2 and 6 have no sales and return null.
Units
Sales
Result
12
241
20.04
7
null
5
85
17
9
410
45.56
3
60.25
20.08
4
null
✓ 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
ORDERBY 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.
Units
Boxes
Result
12
4
3
7
2
3.5
5
1
5
9
3
3
3
null
4
4
1
✓ 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
ORDERBY id
SQL that matches
SELECT Sales / Units AS result
FROM input
ORDERBY id
Units
Sales
Tableau
Tempting SQL
SQL that matches
12
241
20.04
0.05
20.04
7
null
null
null
5
85
17
0.06
17
9
410
45.56
0.02
45.56
3
60.25
20.08
0.05
20.08
4
null
null
null
✓ 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
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.
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.