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
ORDERBY 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
ORDERBY 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.
Owner
Result
TIN\analyst
true
mai
false
bao
false
TIN\analyst
true
null
linh
false
✓ 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
ORDERBY 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
ORDERBY i.id
Owner
Tableau
Tempting SQL
SQL that matches
TIN\analyst
true
false
true
mai
false
false
false
bao
false
false
false
TIN\analyst
true
false
true
null
null
null
linh
false
false
false
✓ 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
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.