Sub topic 6
Dynamic RLS: one role for every user
This chapter assumes you can build a star schema in Power BI Desktop, write simple DAX filters, and create a static role. You should also know that USERPRINCIPALNAME() returns the signed-in user's sign-in name, which the previous chapter covered. Here you will replace many static roles with one dynamic role.
Why static roles stop scaling
A static role hard-codes its filter, such as [Region] = "West". Each audience needs its own role, and each role needs its own member list in the service. With four regions this is manageable. With 40 sales managers, every new hire, transfer, or reorganization means editing the model, republishing, and reassigning members.
A dynamic role moves the "who sees what" decision out of the role definition and into data. The role contains one expression that compares the current user's identity with a column in the model. The model reads the identity at query time, so the same role gives each member a different result.
| Aspect | Static role | Dynamic role |
|---|---|---|
| Roles to maintain | One per audience | One for everyone |
| Filter value | Typed into the DAX | Looked up from data |
| Adding a user | Edit members, often edit the role | Add a row to a table |
| Needs republish for access changes | Yes, when roles change | No, when only data changes |
| Typical risk | Role sprawl | Wrong or missing identity match |
The identity function
Dynamic RLS depends on the function that returns the current user. Use USERPRINCIPALNAME(), which returns the user principal name (UPN) of the signed-in user, for example [email protected]. In most tenants the UPN matches the email address, but that is not guaranteed. Check the value before you build anything on it.
The simplest check is a measure that displays the identity:
Current User = USERPRINCIPALNAME()Add this measure to a card visual. In Desktop without a role applied, it shows the account you are signed in with. Under View as or Test as role it shows the identity being tested. Compare that string with the values stored in your data. A mismatch in domain, capitalization of a suffix, or an alias is the most common reason a dynamic role returns nothing.
Pattern 1: the identity column is on the table you are securing
When a table already stores the user's sign-in name, the role needs only one comparison. Suppose a Manager dimension has a ManagerEmail column. Create a role, select that table, and enter this table filter expression:
[ManagerEmail] = USERPRINCIPALNAME()For each signed-in user, the model keeps only rows in Manager where ManagerEmail equals that user's UPN. Relationships then carry the restriction to related tables, as long as the filter direction runs from the filtered table toward the table you want restricted. A one-to-many relationship from Manager to Sales passes the filter to Sales automatically.
This pattern is quick, but it has limits:
- Each person sees only their own row, unless the column holds an identity for each row they should see.
- It does not handle a user who should see several managers' data.
- Identity values must be stored on the dimension, which may not fit your source data.
Pattern 2: a security mapping table
When access does not line up with one column, add a small table that maps users to the values they may see. Call it Security:
| UserEmail | Region |
|---|---|
| [email protected] | West |
| [email protected] | East |
| [email protected] | North |
| [email protected] | West |
Each row grants one user access to one region. Ana has two rows because she covers two regions. The Region dimension has one row per region, and the Sales fact table relates to it.
Create one role, for example RegionAccess, and put this filter on the Region dimension:
[Region] IN CALCULATETABLE (
VALUES ( Security[Region] ),
Security[UserEmail] = USERPRINCIPALNAME ()
)Here is how the expression evaluates for Ana:
CALCULATETABLEapplies the filterSecurity[UserEmail] = "[email protected]"to theSecuritytable.VALUES ( Security[Region] )returns the distinct regions left in that filtered table: West and East.- The
INoperator keeps onlyRegionrows whose[Region]value is in that list. - The remaining
Regionrows filterSalesthrough the existing relationship.
For Ben the same expression returns only North. Nothing in the role names a person or a region. Adding a user is now a data change, not a model change.
Why the Security table is not related to anything
In this version the Security table has no relationship to other tables. The role expression reaches into it with CALCULATETABLE and a direct column comparison. This keeps the security table out of report visuals and avoids ambiguity in the model. Hide the table from report view so authors do not use it by mistake. Hiding is not protection. The role is what restricts rows.
Alternative: a related security table
Another common design relates Security to the Region dimension and puts a simple filter on Security itself:
[UserEmail] = USERPRINCIPALNAME()The filter then reaches Region only if the relationship runs from Security to Region with filter propagation in that direction. This usually needs a many-to-one relationship pointing the "wrong" way and sometimes bidirectional filtering. It also needs the Apply security filter in both directions setting on that relationship, which is easy to miss. The IN CALCULATETABLE form avoids both problems, so prefer it unless you have a reason to relate the table.
Deciding which pattern to use
Use pattern 1 when each secured row belongs to exactly one person whose identity is already in the data. Use pattern 2 when a person can see several values, several people can share a value, or access is managed by someone other than the model author. Pattern 2 is the one most teams settle on.
You can secure more than one dimension in the same role, for example Region and Department. The filters on different tables combine so that a row is visible only if it passes all of them. Be careful when you do this: if a user has a mapping for a region but none for a department, the department filter returns nothing and the user sees an empty report.
Edge cases that change the result
Users with no mapping row
If the signed-in user has no row in Security, VALUES returns an empty list. IN of an empty list is false for every row, so the user sees no data. This fails safe, but it produces support tickets that look like "the report is broken." Document this behavior for report consumers.
Case and whitespace
DAX text comparison with = is case-insensitive, so [email protected] matches [email protected]. It is not insensitive to extra spaces. A trailing space in the Security table silently breaks the match. Clean the column in Power Query with trim, and consider lowercasing it as well so the data stays consistent for other tools.
Aliases and guest accounts
The UPN is not always the primary email address. Guest users invited from another tenant have a UPN that looks like ana_partner.com#EXT#@contoso.onmicrosoft.com. If your mapping table stores [email protected], the match fails. Store the value that USERPRINCIPALNAME() actually returns for those accounts, which you can confirm with the Current User measure while testing.
Wildcard or "see everything" users
Some users, such as executives, should see all regions. The mapping table has no natural way to say "all." Two approaches work:
- Add a row for each region for that user. This is simple and explicit, but you must update it when a region is added.
- Add an
AllAccessflag column and extend the expression withOR. This is more flexible but makes the expression harder to read and test.
Prefer the first approach unless regions change often.
Duplicate rows
Duplicate rows in Security are harmless for the IN expression because VALUES returns distinct values. They can cause trouble if you later relate the table and filter through it, because duplicates affect how filters propagate. Keep the table to unique combinations of user and value.
Build it step by step
- In Power Query, load or create the
Securitytable withUserEmailandRegion. Trim and standardize the text. - Confirm that
Regionvalues inSecurityexactly match theRegiondimension, including spelling. - Do not create a relationship from
Securityto other tables for theIN CALCULATETABLEpattern. - On the Modeling tab, choose Manage roles and select New. Name the role
RegionAccess. - Select the
Regiontable and paste the expression into its table filter box. Save the role. - Add the
Current Usermeasure to a card on a report page. - Choose View as, tick
RegionAccess, and choose Other user to enter a UPN from yourSecuritytable. Check that the card and the visuals change to match that user's regions.
Only one role is needed whether you have ten users or ten thousand. Membership in the role is still assigned in the Power BI service, and that step is covered in a later chapter. Note that the role applies to nobody until you add members there.
Exercise
A role on the Region dimension uses [Region] IN CALCULATETABLE ( VALUES ( Security[Region] ), Security[UserEmail] = USERPRINCIPALNAME () ). A new employee, [email protected], is added to the role in the service but has no row in the Security table. What does Dana see, and why? Then name one change that grants her the West region without editing the role.
Show answer
Dana sees no data. For her, the CALCULATETABLE filter leaves no Security rows, so VALUES returns an empty list, and IN of an empty list is false for every Region row, so the Sales table is filtered to nothing. To grant West, add a row with her UPN (exactly as USERPRINCIPALNAME() returns it) and Region West to the Security table and refresh the semantic model. The role expression stays the same.
Exercise
Your mapping table stores [email protected], but Ana is a guest user and sees an empty report. How can you find the cause, and what do you change?
Show answer
Add a measure Current User = USERPRINCIPALNAME() to a card and check it while testing as her in the service. For a guest it will likely return a UPN in the form ana_partner.com#EXT#@contoso.onmicrosoft.com. Replace the value in the Security table with that exact string, then refresh the model.