Sub topic 6
Dynamic RLS: one role for every user
This chapter assumes you can create roles in Power BI Desktop, write simple DAX filters, and build a star schema with one-to-many relationships. It also assumes you know that USERPRINCIPALNAME() returns the sign-in name of the current user.
Why static roles stop scaling
A static role hard-codes its values, such as [Region] = "West". Each audience needs its own role, and each role needs its own member list in the service. With 40 regions or 500 managers, you would maintain hundreds of roles, and each new hire could mean editing the model and republishing it.
Dynamic RLS moves the "who sees what" decision out of the roles and into data. You create one role. Its filter compares a column in your model to the identity of the signed-in user. The model then returns different rows to different people.
The core idea
The filter expression in a dynamic role calls USERPRINCIPALNAME(). In the service, this returns the user's Microsoft Entra ID sign-in name, usually an email address such as [email protected]. In Power BI Desktop, it returns your local account name, so test with View as, which the testing chapter covers.
You can add a measure to see what the model receives:
Current User = USERPRINCIPALNAME()Place this measure in a card while testing a role. If the value is not what you expect, the filter will not match.
Pattern 1: the email is already in the table
If a table has a column that stores the owner's email address, filter on that column directly. Suppose an Employee table has a ManagerEmail column. Add this expression to the Employee table in a role:
[ManagerEmail] = USERPRINCIPALNAME()Each user sees only the rows whose ManagerEmail matches their own sign-in name. The filter propagates from Employee to related fact tables through relationships, as long as the relationships filter in the right direction.
This pattern is simple, but it only works when every row has exactly one owner and the email is stored in the data.
Pattern 2: a Security mapping table
When one user can see several regions, or many users share a region, a mapping table is cleaner. The Security table has one row per permitted pair:
| UserEmail | Region |
|---|---|
| [email protected] | West |
| [email protected] | East |
| [email protected] | East |
The role filter goes on the Region dimension table:
[Region] IN CALCULATETABLE (
VALUES ( Security[Region] ),
Security[UserEmail] = USERPRINCIPALNAME ()
)Reading from the inside out:
Security[UserEmail] = USERPRINCIPALNAME ()keeps only the Security rows for the current user.VALUES ( Security[Region] )returns the distinct regions in those rows.[Region] IN ...keeps only the Region rows that appear in that list.
Ana sees West and East. Ben sees East. Adding access for a new person means adding a row to the Security table. You do not edit the role.
Keeping the Security table out of reach
The Security table itself should not be readable by report users, or they could see everyone's assignments. Two common choices:
- Hide the table from report view. This is cosmetic, not security.
- Do not relate it to other tables and use it only inside the role expression, as in the example above. Because no relationship exists, the table does not filter anything else.
If you also add a filter on the Security table, such as [UserEmail] = USERPRINCIPALNAME(), users can only see their own rows even if they query it.
Common errors
- Empty report for everyone. The Security emails do not match what
USERPRINCIPALNAME()returns. Compare them using theCurrent Usermeasure. - Filter does not reach the fact table. The relationship direction or an inactive relationship blocks propagation. Check that the filter flows from the dimension to the fact table.
- Everyone sees all data. The user is an Admin, Member, or Contributor in the workspace, which bypasses RLS, or they were never assigned to the role.
- Testing in Desktop shows odd names. Desktop does not return the same value as the service. Use View as with a specific user.
Exercise
A Security table has columns UserEmail and Region. Write the role filter for the Region table, and explain why adding a new user needs no change to the role.
Show answer
Use [Region] IN CALCULATETABLE ( VALUES ( Security[Region] ), Security[UserEmail] = USERPRINCIPALNAME () ). The role only compares each region to the regions listed for the current user. A new user needs a new row in Security, and the same role handles it after the next data refresh.