Sub topic 2
USERPRINCIPALNAME() and Dynamic RLS
Assume you know Power BI Desktop, star schemas, one-to-many relationships, and basic DAX filter expressions.
USERPRINCIPALNAME() versus USERNAME()
USERPRINCIPALNAME() returns the signed-in user's principal name, usually their work or school email address, such as [email protected]. It is useful when a security table stores email addresses.
USERNAME() returns the current user name. In Power BI Desktop, it commonly appears as DOMAIN\user. In the Power BI service, it usually returns the user's principal name. Use USERPRINCIPALNAME() when your model expects a consistent user identifier.
Current User = USERPRINCIPALNAME()Create this measure temporarily to inspect the identity that the model sees.
Dynamic RLS: one role for every user
Dynamic row-level security (RLS) uses the current user's identity to choose rows. You create one role instead of creating a separate role for every audience.
[Region] IN CALCULATETABLE (
VALUES ( Security[Region] ),
Security[UserEmail] = USERPRINCIPALNAME ()
)Apply this table filter expression to the Region table. The expression keeps only regions assigned to the signed-in user in Security.
Designing a security mapping table
A mapping table contains one row for each user-to-access assignment:
| UserEmail | Region |
|---|---|
| [email protected] | West |
Keep the mapping table at a clear grain. If Alex can access two regions, store two rows. Apply the filter to the Region dimension, and use a one-to-many relationship from Region to the Sales fact table. The filtered dimension then limits related sales rows.
Testing roles in Desktop and the service
In Desktop, open Modeling > Manage roles, create the role, and add the DAX expression. Select Modeling > View as, choose the role, and verify that visuals show only permitted rows. Add the temporary Current User measure if the identity is unclear.
After publishing, open the semantic model's Security settings in the Power BI service. Assign users or security groups to the role, then use Test as role to inspect the report. Test users with multiple assignments and users with no mapping row.
Exercise
Why might a dynamic RLS report show no rows for a user?
Show answer
The user's principal name may not match Security[UserEmail], or the filter may not reach Region and the related fact table through the model relationships.