Sub topic 5
USERPRINCIPALNAME() and how it differs from USERNAME()
This chapter assumes you can create a role in Power BI Desktop and write a table filter expression. It also assumes you know that a role filter on a dimension table reaches the fact table through relationships. Here you learn which function returns the signed-in user's identity, and why you should pick it over its older sibling.
Why identity functions matter
A static role filters on a fixed value such as [Region] = "West". A dynamic role needs to know who is asking. DAX has two functions for this. Each returns text that you compare with a column in your model.
USERNAME()returns the user name of the current user.USERPRINCIPALNAME()returns the user principal name (UPN) of the current user. The UPN is the sign-in name in Microsoft Entra ID, which usually looks like[email protected].
Both take no arguments. Both are evaluated when a query runs, so each user gets their own result from the same model.
Where the two functions differ
The difference shows up in Desktop and in on-premises setups.
| Context | USERNAME() | USERPRINCIPALNAME() |
|---|---|---|
| Power BI Desktop | `DOMAIN\user` | `[email protected]` |
| Power BI service | `[email protected]` | `[email protected]` |
In the service, the two usually return the same UPN. In Desktop, USERNAME() returns the domain-and-user form. A role that compares USERNAME() to an email column can therefore pass in the service and return nothing in Desktop. USERPRINCIPALNAME() returns the UPN in both places, so you test the same logic that you publish.
See what the model sees
Before you write a filter, check what the function returns. Add this measure to your model:
Current User = USERPRINCIPALNAME()Put the measure in a card visual. Run it first as yourself, then with View as and a role selected. If the card shows [email protected], your filter column must hold exactly that text.
Use it in a role filter
Suppose a table has a ManagerEmail column. This table filter expression limits each manager to their own rows:
[ManagerEmail] = USERPRINCIPALNAME()You add this under Manage roles, on the table that holds the column. DAX text comparison with = ignores case, so [email protected] still matches [email protected]. Spelling, spaces, and a different domain do not match.
Common mistakes
- Storing the wrong value. The UPN can differ from the person's email address. A user may sign in as
[email protected]while their mail address is[email protected]. Fill your mapping column with the sign-in name, and confirm it with theCurrent Usermeasure. - Using USERNAME() for a Desktop test. The
DOMAIN\userresult will not match an email column, and the report looks empty. - Expecting the function to work outside a role. You can use it in a measure, as above, but it only restricts data when you use it inside a role's filter expression.
- Leaving whitespace in the mapping data. A trailing space makes an otherwise correct email fail to match.
Which function should you choose
Use USERPRINCIPALNAME() for new models. It returns the same kind of value in Desktop and in the service, so your test results predict production. Use USERNAME() only when your data already stores DOMAIN\user values and you control the environment where it runs.
The next chapters use this function in a role that filters through a security mapping table.
Exercise
A role on the Employee table uses [Email] = USERNAME(). It returns the right rows in the Power BI service but no rows in View as in Desktop. What causes this, and what is the fix?
Show answer
In Desktop, USERNAME() returns the DOMAIN\user form, which never matches an email column. In the service it returns the UPN, which does match. Change the filter to [Email] = USERPRINCIPALNAME(), which returns the UPN in both places. Then check the value with a Current User measure.
Exercise
Why might a correct-looking filter of [ManagerEmail] = USERPRINCIPALNAME() still show an empty report for one user?
Show answer
The user's sign-in name (UPN) may differ from the email stored in ManagerEmail, for example a different domain or a stray space. Show USERPRINCIPALNAME() in a card while viewing as that user, then correct the stored value to match it exactly.