Sub topic 8
Testing roles in Desktop and the service
A role that saves without errors can still return the wrong rows. This chapter covers how to check a role before you publish it and again after you assign members. It assumes you can already write a static role and a dynamic role, and that you know what a security mapping table is.
Why testing is its own step
A role filter is a DAX expression that runs against the model each time a visual queries it. Power BI does not check whether the expression returns the rows you meant. Three failures are common:
- The filter is valid but too broad, so a user sees data they should not.
- The filter is valid but too narrow, so the report is empty.
- The filter works for you but fails for others, because your own workspace role bypasses RLS.
Testing needs two passes. In Power BI Desktop you test the logic of the role. In the Power BI service you test the published model, with the real workspace and real user identities.
Testing in Power BI Desktop with View as
Desktop lets you impersonate a role without publishing.
- Open the Modeling tab.
- Select View as.
- Tick one or more roles. Optionally tick Other user and type a user name.
- Select OK.
A yellow banner at the top of the report shows which role is active. Select Stop viewing to return to the full model.
Static roles
For a static role such as [Region] = "West", tick only that role. Every visual should now show West rows only. Check totals on a card against a figure you know. Compare it with the unfiltered total to confirm that the filter reached the fact table through the relationship.
Dynamic roles
A dynamic role depends on who is signed in, so you must give Desktop an identity. Tick Other user and enter the sign-in name of a person who appears in your Security table, for example [email protected]. Then tick the dynamic role.
To see what the model believes your identity is, add a measure:
Current User = USERPRINCIPALNAME()Put the measure in a card. Under View as with Other user set, the card shows the name you typed. Without Other user, Desktop shows your own account name. This card is the quickest check that the value being compared in your filter is the one you expect.
What Desktop does and does not prove
| Aspect | Desktop View as | Power BI service Test as role |
|---|---|---|
| Model tested | Local file | Published semantic model |
| Identity source | Name you type | Name you type, or you, as the role |
| Workspace role effects | None | Visible, because you are a real workspace user |
| Group membership | Not tested | Not tested for individual members |
| Good for | Logic of filters and relationships | Published model behavior |
Desktop confirms the DAX and the relationships. It cannot confirm that anyone has been assigned to the role, because assignment exists only in the service.
Testing with a multiple-role selection
You can tick several roles in View as. The result is the union of the rows allowed by each role, which is how Power BI treats a user who belongs to several roles. Test this deliberately if you have users in more than one role. If one role is wider than expected, the combined result will be wide too.
Testing in the Power BI service with Test as role
After you publish, open the workspace and find the semantic model.
- Select the More options (…) menu next to the semantic model.
- Select Security.
- Select the role you want to check.
- Select the More options (…) menu next to the role, then Test as role.
The report opens with the role applied. You can also enter a user name in the Now viewing as box and choose Select a user. This lets you test a dynamic role as a specific person, for example someone in the Security table.
Use the Now viewing as control to switch between roles and users without leaving the page.
Why this matters for dynamic roles
When you test a dynamic role as yourself, USERPRINCIPALNAME() returns your own sign-in name. If you are not in the Security table, the filter finds no match and the report is empty. This is correct behavior, not a bug. Testing as a user listed in the Security table shows what that person will see.
A worked testing routine
Use a security table like this one:
| UserEmail | Region |
|---|---|
| [email protected] | West |
| [email protected] | East |
| [email protected] | North |
| [email protected] | West |
The dynamic role on the Region dimension uses:
[Region] IN CALCULATETABLE (
VALUES ( Security[Region] ),
Security[UserEmail] = USERPRINCIPALNAME ()
)Run these checks in order:
- Single region. View as
[email protected]. Only West should appear. - Multiple regions. View as
[email protected]. East and North should appear, and West should not. - Unlisted user. View as
[email protected], who is not in the table. The report should be empty. - Totals. For each user, compare the total sales card to a manual sum of that user's regions in the source data.
- Both ends of the relationship. Add a table visual with the Region column and a visual from the fact table. Confirm both show only permitted regions.
Step 3 is worth doing. An empty report for an unlisted user shows that the role denies by default. A full report there means the role is not applied.
Testing a real member
Test as role is the closest you can get as an administrator, but it still runs as you with the role applied. The strongest check is to have a real member open the report, or to sign in with a test account that is a member of the role and not a workspace Admin, Member, or Contributor.
Create a test account if your organization allows it. Give it only Viewer access to the workspace, or only access to an app, and add it to the role. Then sign in as that account in a private browser window.
Common test results and what they mean
| What you see | Likely cause |
|---|---|
| Full data when viewing as a role | Role not selected, or the filter is on a table with no relationship to the fact table |
| Empty report for a listed user | Case or spelling mismatch between the email and `USERPRINCIPALNAME()`, or a one-way relationship that does not carry the filter |
| `Current User` card shows a different name than the Security table | The table holds display names or old addresses, not sign-in names |
| Works in Desktop, empty in the service | The member is not assigned to the role, or **Test as role** was run as an account not in the Security table |
| Works for one user and not another | Missing or misspelled row in the Security table for the second user |
Check the measure first. Putting Current User next to a table showing Security[UserEmail] in a test copy of the report makes mismatches easy to spot.
Edge cases to keep in mind
- Case. DAX string comparison with
=is not case sensitive, so a case difference alone rarely causes failure. Extra spaces and different domains do. - Aliases. A user may sign in with an address that differs from their mailbox address.
USERPRINCIPALNAME()returns the sign-in name, so the Security table must use that. - Cached views. After you change a role or the Security table, refresh the model before you test again in the service. Changes to the Security table's data show up only after a data refresh.
- Tested roles are not saved as assignments. Using Test as role does not add anyone to the role.
Exercise
A dynamic role on the Region dimension filters with the Security table. You view the published report in the service as the workspace owner and see all regions. Does this show that the role is broken? What would you do next?
Show answer
No. Workspace Admin, Member, and Contributor roles bypass RLS, so the owner sees all rows regardless of the role. Use Test as role, choosing the role and a user who appears in the Security table, or sign in as a separate account that has Viewer access and is a role member. Confirm that only the permitted regions appear.
Exercise
You use View as in Desktop with the dynamic role ticked and no "Other user", and the report is empty. Why, and how do you test properly?
Show answer
With no "Other user", Desktop uses your own account, and USERPRINCIPALNAME() returns your sign-in name. If it is not in the Security table, the filter matches no rows. Tick "Other user" and enter an email that is in the Security table, then tick the role again.
Exercise
Why should you include a test for a user who is not in the Security table?
Show answer
It confirms that the role denies by default. The report should be empty for that user. If it shows data, the role is not being applied or the filter is wider than intended.