Tableau function User

Tableau ISUSERNAME to SQL

ISUSERNAME("mai") is true only when the person viewing is mai; it is shorthand for USERNAME() = "mai". In SQL, compare the viewer your app passes in with the name. current_user is the database login, not the viewer. The sample runs as a stand-in viewer: user name TIN\analyst, full name Local Analyst, domain TIN, in no groups. In SQL the viewer is a one-row viewer table; in production your app passes it in.

Updated · Checked on the xmetric engine

What is the syntax?

ISUSERNAME("username")
username
a string constant: the user name to test, such as 'mai'.

Returns a boolean: true when the viewer's user name equals the string. The same value on every row.

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.

Is the viewer TIN\analyst?

Tableau

ISUSERNAME('TIN\analyst')

SQL (DuckDB)

WITH viewer AS (
  SELECT
    'TIN\analyst' AS username,
    'Local Analyst' AS fullname,
    'TIN' AS domain
)
SELECT v.username = 'TIN\analyst' AS result
FROM input i, viewer v
ORDER BY i.id

True on every row: the test is about the viewer, not the row.

Result
true
true
true
true
true
true

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

How do I show sales only to mai?

Tableau

IF ISUSERNAME('mai') THEN [Sales] END

SQL (DuckDB)

WITH viewer AS (
  SELECT
    'TIN\analyst' AS username,
    'Local Analyst' AS fullname,
    'TIN' AS domain
)
SELECT CASE WHEN v.username = 'mai' THEN i.Sales END AS result
FROM input i, viewer v
ORDER BY i.id

The stand-in viewer is not mai, so every value is null.

SalesResult
420null
310null
150null
275null
90null
515null

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

Where does a straight translation go wrong?

Common mistake

Why does current_user not match the viewer?

current_user returns the database login (duckdb here), so the test is false for the very viewer it names.

Tableau

ISUSERNAME('TIN\analyst')

Tempting SQL

SELECT current_user = 'TIN\analyst' AS result
FROM input
ORDER BY id

SQL that matches

WITH viewer AS (
  SELECT
    'TIN\analyst' AS username,
    'Local Analyst' AS fullname,
    'TIN' AS domain
)
SELECT v.username = 'TIN\analyst' AS result
FROM input i, viewer v
ORDER BY i.id
TableauTempting SQLSQL that matches
truefalsetrue
truefalsetrue
truefalsetrue
truefalsetrue
truefalsetrue
truefalsetrue

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 $ viewer = 'mai' AS result
FROM input
ORDER BY id

Snowflake documentation

BigQuery

SELECT @ viewer = 'mai' AS result
FROM input
ORDER BY id

BigQuery documentation

What does your own ISUSERNAME formula return?

Edit the formula and run it on the 6 sample rows. Fields: [id], [Region], [Owner], [OwnerName], [Sales].

Runs in your browser on the xmetric engine. Nothing is uploaded.

Which functions go with it?

What do people ask about ISUSERNAME?

How do I get the Tableau user into the database?

The database only knows the account Tableau connects with. Pass the viewer in: Tableau's Initial SQL can use the [TableauServerUser] parameter, or your app sets a session variable or a query parameter. Then filter with it, as the viewer table does on this page.

What does it return in Tableau Desktop?

When you are signed in to Tableau Server or Cloud, the server's user. Otherwise Desktop uses your local or network identity. Test row-level security as a real viewer on the server, not from Desktop.

How do I compare the viewer with a column instead of a fixed name?

Use [Owner] = USERNAME(). ISUSERNAME is meant for a fixed name typed into the formula.

Where is this documented?