Tableau function Pass-through (RAWSQL)

Tableau RAWSQL_BOOL to SQL

RAWSQL_BOOL("%1 > 100", [Sales]) hands the text Sales > 100 to the database, row by row, and reads the answer as true or false. In SQL you write the expression itself: Sales > 100. A null Sales gives null, not false, which matters as soon as the result feeds an IIF.

Updated · Checked on the xmetric engine

What is the syntax?

RAWSQL_BOOL("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 boolean 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.

Which lines sold more than 100?

Tableau

RAWSQL_BOOL("%1 > 100", [Sales])

SQL (DuckDB)

SELECT Sales > 100 AS result
FROM input
ORDER BY id

%1 becomes Sales. Lines 2 and 6 have no sales, so the comparison is null there, not false.

SalesResult
241true
null
85false
410true
60.25false
null

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

Which lines filled every box exactly once?

Tableau

RAWSQL_BOOL("%1 = %2", [Units], [Boxes])

SQL (DuckDB)

SELECT Units = Boxes AS result
FROM input
ORDER BY id

%1 is the first argument (Units), %2 the second (Boxes). Line 5 has no box count, so it returns null.

UnitsBoxesResult
124false
72false
51false
93false
3null
44true

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

Where does a straight translation go wrong?

Common mistake

Why does a CASE WHEN turn the null rows into "small"?

Tableau's IIF returns null when its test is null, so lines 2 and 6 stay null. CASE WHEN ... ELSE sends a null test to the ELSE branch. Give the null case its own branch, or test both outcomes explicitly.

Tableau

IIF(RAWSQL_BOOL("%1 > 100", [Sales]), 'big', 'small')

Tempting SQL

SELECT
  CASE WHEN Sales > 100 THEN 'big' ELSE 'small' END AS result
FROM input
ORDER BY id

SQL that matches

SELECT
  CASE
    WHEN Sales > 100 THEN 'big'
    WHEN NOT (Sales > 100) THEN 'small'
    END AS result
FROM input
ORDER BY id
SalesTableauTempting SQLSQL that matches
241bigbigbig
nullsmallnull
85smallsmallsmall
410bigbigbig
60.25smallsmallsmall
nullsmallnull

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 > 100 AS result
FROM input
ORDER BY id

Snowflake documentation

BigQuery

SELECT Sales > 100 AS result
FROM input
ORDER BY id

BigQuery documentation

What does your own RAWSQL_BOOL 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_BOOL?

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?