Tableau function User

Tableau USERNAME to SQL

USERNAME() returns the user name of whoever is looking at the view, so [Owner] = USERNAME() keeps each viewer's own rows. SQL has no such function: current_user is the database account Tableau connects with, often one service account for everyone. Pass the viewer in and compare against it. 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?

USERNAME()

Returns a string: the user name of the person viewing the workbook. 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.

Who is viewing?

Tableau

USERNAME()

SQL (DuckDB)

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

One value, repeated on every row.

Result
TIN\analyst
TIN\analyst
TIN\analyst
TIN\analyst
TIN\analyst
TIN\analyst

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

Which rows belong to the viewer?

Tableau

[Owner] = USERNAME()

SQL (DuckDB)

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

Rows 1 and 4 are the viewer's. Row 5 has no owner, so the test is null and a filter drops it.

OwnerResult
TIN\analysttrue
maifalse
baofalse
TIN\analysttrue
null
linhfalse

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 is the database login, here duckdb, not the person looking at the dashboard. Every row fails the test. Behind a shared service account, every viewer would see the same thing.

Tableau

[Owner] = USERNAME()

Tempting SQL

SELECT Owner = current_user 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 i.Owner = v.username AS result
FROM input i, viewer v
ORDER BY i.id
OwnerTableauTempting SQLSQL that matches
TIN\analysttruefalsetrue
maifalsefalsefalse
baofalsefalsefalse
TIN\analysttruefalsetrue
nullnullnull
linhfalsefalsefalse

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

Snowflake documentation

BigQuery

SELECT Owner = @ viewer AS result
FROM input
ORDER BY id

BigQuery documentation

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

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.

Where is this documented?