Tableau function Pass-through (RAWSQL)

Tableau RAWSQL_STR to SQL

RAWSQL_STR("upper(%1)", [Product]) sends upper(Product) to the database and reads the answer as a string. In SQL, write the expression with the column in place of %1. Watch string joins: || returns null when any part is null, concat() skips the null part, and databases disagree on which one their CONCAT does.

Updated · Checked on the xmetric engine

What is the syntax?

RAWSQL_STR("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 string 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 each product name in capitals?

Tableau

RAWSQL_STR("upper(%1)", [Product])

SQL (DuckDB)

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

Line 5 has no product name, so the result is null.

ProductResult
DeskDESK
LampLAMP
ChairCHAIR
ShelfSHELF
null
DeskDESK

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

How do I label each line "Region / Product"?

Tableau

RAWSQL_STR("%1 || ' / ' || %2", [Region], [Product])

SQL (DuckDB)

SELECT Region || ' / ' || Product AS result
FROM input
ORDER BY id

|| is null as soon as one side is null, so line 5 returns null rather than "West / ".

RegionProductResult
EastDeskEast / Desk
EastLampEast / Lamp
EastChairEast / Chair
WestShelfWest / Shelf
Westnull
NorthDeskNorth / Desk

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

Where does a straight translation go wrong?

Common mistake

Why does concat() label the line with no product?

The RAWSQL text uses ||, which returns null when a part is null. DuckDB's concat() skips nulls and returns "West / " for line 5. Keep the operator the text used, or wrap the result in a null check.

Tableau

RAWSQL_STR("%1 || ' / ' || %2", [Region], [Product])

Tempting SQL

SELECT concat(Region, ' / ', Product) AS result
FROM input
ORDER BY id

SQL that matches

SELECT Region || ' / ' || Product AS result
FROM input
ORDER BY id
RegionProductTableauTempting SQLSQL that matches
EastDeskEast / DeskEast / DeskEast / Desk
EastLampEast / LampEast / LampEast / Lamp
EastChairEast / ChairEast / ChairEast / Chair
WestShelfWest / ShelfWest / ShelfWest / Shelf
WestnullWest / null
NorthDeskNorth / DeskNorth / DeskNorth / Desk

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 || ' / ' || Product AS result
FROM input
ORDER BY id

Snowflake documentation

BigQuery

SELECT CONCAT(Region, ' / ', Product) AS result
FROM input
ORDER BY id

BigQuery documentation

What does your own RAWSQL_STR 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_STR?

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.

Is CONCAT null-safe everywhere?

No. DuckDB's concat() skips nulls; Snowflake's CONCAT and BigQuery's CONCAT return null if any argument is null, the same as ||. Check the target database before you swap one for the other.

Where is this documented?