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
ORDERBY 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
IFISUSERNAME('mai') THEN[Sales]END
SQL (DuckDB)
WITH viewer AS (
SELECT'TIN\analyst'AS username,
'Local Analyst'AS fullname,
'TIN'AS domain
)
SELECTCASEWHEN v.username ='mai'THEN i.Sales ENDAS result
FROM input i, viewer v
ORDERBY i.id
The stand-in viewer is not mai, so every value is null.
Sales
Result
420
null
310
null
150
null
275
null
90
null
515
null
✓ 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
ORDERBY 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
ORDERBY i.id
Tableau
Tempting SQL
SQL that matches
true
false
true
true
false
true
true
false
true
true
false
true
true
false
true
true
false
true
✓ 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
ORDERBY id
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.