Tableau function User

Tableau USERDOMAIN to SQL

USERDOMAIN() returns the domain of the person viewing: their Tableau Server domain when signed in, or the Windows domain of a Desktop user on one. In SQL, pass it in with the user name and test it there. Splitting the database login apart gives the connection account's domain, not the viewer's. 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?

USERDOMAIN()

Returns a string: the viewer's domain. 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.

What is the viewer's domain?

Tableau

USERDOMAIN()

SQL (DuckDB)

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

One value, repeated on every row.

Result
TIN
TIN
TIN
TIN
TIN
TIN

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

How do I show rows only to viewers in the TIN domain?

Tableau

USERDOMAIN() = 'TIN' AND [Owner] = USERNAME()

SQL (DuckDB)

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

Rows 1 and 4 pass. Row 5 has no owner, so the test is null.

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 splitting the login give the wrong domain?

On SQL Server, a login looks like DOMAIN\name, so it is tempting to cut the domain out of it. But the login is the account Tableau connects with, not the viewer: here it is duckdb and the split returns duckdb.

Tableau

USERDOMAIN()

Tempting SQL

SELECT split_part(current_user, '\', 1) 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.domain AS result
FROM input i, viewer v
ORDER BY i.id
TableauTempting SQLSQL that matches
TINduckdbTIN
TINduckdbTIN
TINduckdbTIN
TINduckdbTIN
TINduckdbTIN
TINduckdbTIN

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_domain = 'TIN' AS result
FROM input
ORDER BY id

Snowflake documentation

BigQuery

SELECT @ viewer_domain = 'TIN' AS result
FROM input
ORDER BY id

BigQuery documentation

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

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.

Why is USERDOMAIN empty?

On Tableau Desktop, off a Windows domain and not signed in to a server, there is no domain to return. Guard filters that depend on it.

Where is this documented?