Sub topic 2
Roles and table filter expressions
This chapter assumes you can load data in Power BI Desktop, you know a star schema (fact tables, dimension tables, one-to-many relationships), and you can read simple DAX comparisons. You also need a work or school account to publish later. Here you build the working model: a role is a named set of DAX filters on tables.
What a role is
A role is a named rule set stored in the semantic model. Each role holds zero or more table filter expressions. A table filter expression is a DAX formula that returns TRUE or FALSE for each row of one table. Rows that return TRUE stay visible to members of the role. Rows that return FALSE are hidden from every visual, measure, and query that member runs.
A role has no effect until someone is a member of it. You assign members in the Power BI service, which a later chapter covers. In Desktop you only define the role and its filters.
Create a role in Desktop
- Open the Modeling tab and select Manage roles.
- Select New, then give the role a name such as
West Sales. - Select the table you want to filter, for example
Region. - In the filter box, enter a DAX expression that returns TRUE or FALSE.
- Select Save.
The expression is evaluated one row at a time. Inside it, a column reference such as [Region] means "this column in the current row".
[Region] = "West"Members of this role see only rows of the Region table whose Region column is West. The filter is written on the dimension table, not on the fact table. It still limits the fact table, because filters travel through relationships. The chapter on filter propagation covers that in detail.
Rules for filter expressions
- The expression must return a Boolean. A bare column or a number is an error.
- It is evaluated in row context, so you can compare columns of the same table without an aggregator.
- Each table can have at most one filter expression per role. Combine conditions with
&&(and) or||(or). - A table with no expression in a role is not filtered directly by that role. It can still be filtered indirectly through relationships.
- You can use functions that depend on the signed-in user, such as
USERPRINCIPALNAME(). That is what makes a role dynamic.
For example, this expression keeps only West rows from the current year:
[Region] = "West" && [Year] = YEAR ( TODAY () )Inspect the identity the model sees
Before writing user-based filters, check which identity the model sees. Add a measure and put it in a card visual:
Current User = USERPRINCIPALNAME()In Desktop with no role applied, this shows your own sign-in name. When you view the report as a role, or after publishing, it shows the identity of the person viewing.
How a role changes what the model returns
The diagram shows the order of events for a member of a role.
Common mistakes
- Filtering the fact table instead of a dimension. It works, but you must repeat it on every fact table. Filter a shared dimension once.
- Expecting a role to hide a column. A role limits rows only. Every column of a visible row stays visible.
- Testing only as the author. The model owner is not filtered in Desktop until you use View as. See the testing chapter.
Exercise
A role has the expression [Region] = "West" on the Region table. Region has a one-to-many relationship to Sales. What does a member of the role see in a Sales table visual, and why?
Show answer
Only the Sales rows that belong to West. The role removes all non-West rows from Region, and the relationship passes that filter to Sales, so Sales rows linked to other regions are hidden.
Exercise
Why does the expression [Region] alone fail as a table filter expression?
Show answer
A table filter expression must return TRUE or FALSE for each row. A bare column returns a value of the column's type, not a Boolean, so it needs a comparison such as [Region] = "West".