Sub topic 5
USERPRINCIPALNAME() and how it differs from USERNAME()
Dynamic row-level security (RLS) depends on one question: who is looking at this report right now? DAX answers it with two functions, USERPRINCIPALNAME() and USERNAME(). They often return the same text in the Power BI service, and that similarity causes bugs. This chapter explains what each function returns, where the results differ, and which one to use in a role filter.
This chapter assumes you can create a role in Power BI Desktop and write a simple DAX table filter expression. It also assumes you know that a role filter on a dimension table flows to related fact tables through relationships. The next chapter uses these functions to build a full dynamic role.
What each function returns
USERPRINCIPALNAME() returns the user principal name (UPN) of the person who is viewing the model. The UPN is the sign-in name stored in Microsoft Entra ID (formerly Azure AD), such as [email protected].
USERNAME() returns the user's name in the form the host environment uses. In the Power BI service, that form is the UPN, so the two functions return the same value. In Power BI Desktop, and in on-premises hosts such as SQL Server Analysis Services, USERNAME() returns the Windows form, DOMAIN\username.
| Situation | `USERPRINCIPALNAME()` | `USERNAME()` |
|---|---|---|
| Power BI service, member user | `[email protected]` | `[email protected]` |
| Power BI Desktop, signed in | Your UPN or sign-in name | Usually `DOMAIN\username` |
| On-premises Analysis Services | `[email protected]`-style UPN | `CONTOSO\alopez` |
| Guest (B2B) user in the service | The guest's UPN, often with `#EXT#` in it | The same value |
Both functions take no arguments and return text. Neither returns the user's display name.
See the value for yourself
Do not guess what the model sees. Add a measure that prints the identity, put it in a card visual, and look at it in every environment where you test.
Current User = USERPRINCIPALNAME()To compare both functions side by side, use a second measure:
Identity Check =
"UPN: " & USERPRINCIPALNAME() & " | USERNAME: " & USERNAME()Publish the report and open it in the service. Then open the same report in Desktop. If the two values differ in Desktop and match in the service, you have seen the difference described above.
Where the value comes from
The identity is not stored in your data. The host, either Desktop or the service, supplies it each time a query runs. The model reads it when it evaluates the role filter for that query.
The viewer cannot change this value from inside the report, so it is safe to build filters on it. Do not confuse it with a slicer selection. A slicer is a user choice, and the identity is not.
Using the function in a role filter
A dynamic role filter compares the identity with a column of sign-in names. The simplest case is a table that already has a column holding each manager's email address:
[ManagerEmail] = USERPRINCIPALNAME()You enter this expression as the table filter on that table in Manage roles. When Ana queries the model, the expression becomes [ManagerEmail] = "[email protected]", and the table keeps only the matching rows. Relationships then carry the filter to related tables.
In the real world you usually look the user up in a separate mapping table, so one role serves every user:
[Region] IN CALCULATETABLE (
VALUES ( Security[Region] ),
Security[UserEmail] = USERPRINCIPALNAME ()
)The next chapters cover how to design that Security table. For now, notice that the function is the only part of the expression that changes between users.
Why prefer USERPRINCIPALNAME()
Both functions work in the service, so the choice matters mostly for consistency and for testing.
- It states what you mean. The function name says you expect a UPN. A reader of the role does not have to know that
USERNAME()happens to return one in the service. - It behaves the same in more places.
USERNAME()returnsDOMAIN\userin Desktop and in on-premises hosts. A filter that compares it with email addresses returns no rows there, and you may mistake a working role for a broken one. - It matches the cloud identity model. Entra ID, security groups, and guest accounts all work in UPNs.
Use USERNAME() only when your mapping data really contains Windows-style names and the model runs where they are returned, for example on-premises Analysis Services. For a semantic model in the service, use USERPRINCIPALNAME().
Gotchas
The UPN is not always the email address
A UPN looks like an email address, and it often is one, but the two are separate attributes in Entra ID. A user's UPN might be [email protected] while her mail address is [email protected]. If your mapping table holds email addresses taken from an HR system, the comparison fails for those users, and they see an empty report.
Check the real value with the Current User measure for a few users, not just for yourself. Then load the UPNs into your mapping table, or ask your Entra administrator to confirm that UPN and mail match.
Guest users have long UPNs
External guests invited through B2B collaboration have a UPN in a form like partner_fabrikam.com#EXT#@contoso.onmicrosoft.com. If you store [email protected] in your mapping table, the match fails. Use the guest's actual UPN as shown in Entra ID, and verify it by testing as that user.
Case and whitespace
DAX text comparison with = is not case-sensitive, so [email protected] matches [email protected]. Extra spaces are different. A trailing space in the mapping table makes the comparison fail with no error. Clean the column in Power Query with Transform > Format > Trim before loading it.
Desktop shows a different identity
When you use View as in Desktop, you can enter the name the model should see. The value you type is what USERPRINCIPALNAME() returns during that test. Outside of View as, the function returns whatever Desktop knows about your sign-in. That is why a role can seem to work in Desktop and fail in the service, or the reverse. Always confirm in the service with Test as role before you tell users the report is ready.
The function works outside roles too
You can use USERPRINCIPALNAME() in measures and calculated columns, not just in role filters. A measure evaluates for each viewer, so you can show "Welcome, ..." text or build an audit card.
Do not use the function to hide data with ordinary measures or visual filters. A visual-level filter based on the user can be undone by someone who can edit the report or connect to the model. Only a role filter is enforced by the model itself.
Embedded scenarios
In "embed for your customers" scenarios, the application authenticates to Power BI and passes an effective identity with the embed token. The name you pass in that token is the value the functions return, and it does not have to be a real Entra user. Your mapping table must then contain whatever string the application sends.
A quick diagnosis routine
When a dynamic role returns no rows or the wrong rows, check the identity before you check the filter.
- Add the
Current Usermeasure to a card. - Open the report in the service as the affected user, or use Test as role with Now viewing as set to that person.
- Compare the displayed value with the mapping column, character by character.
- If they differ, fix the data. If they match, move on to relationships and filter direction.
Most failures in this area are data mismatches, not DAX errors, so step 3 resolves the majority of cases.
Summary of the decision
- In a semantic model published to the service, use
USERPRINCIPALNAME()in role filters. - Treat
USERNAME()as the legacy, environment-dependent function. It returnsDOMAIN\userin Desktop and on-premises, and a UPN in the service. - Store UPNs, not assumed email addresses, in your mapping data.
- Always verify the live value with a measure.
Exercise
A role on the Sales table uses the filter [OwnerEmail] = USERNAME(). It returns the correct rows in the service, but when you test it in Power BI Desktop it returns no rows. Why does this happen, and what change makes the role behave the same in both places?
Show answer
In the service, USERNAME() returns the user's UPN, which matches the email-style values in [OwnerEmail]. In Desktop, USERNAME() returns the Windows form DOMAIN\username, which never equals an email-style value, so the filter matches no rows. Replace USERNAME() with USERPRINCIPALNAME() so the filter compares a UPN with the column in both environments, and confirm the value with a Current User measure. If the column holds mail addresses that differ from the UPNs, load the UPNs instead.
Exercise
Several external guest users report empty reports, while employees see their data. The mapping table holds each guest's email address, such as [email protected]. What is the likely cause, and how do you confirm and fix it?
Show answer
A guest's UPN usually has a different form, such as partner_fabrikam.com#EXT#@contoso.onmicrosoft.com, so the comparison with USERPRINCIPALNAME() fails. Confirm it by testing as the guest in the service (or reading their UPN in Entra ID) with a Current User measure on a card. Fix it by storing the guest's actual UPN in the mapping table.