Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

v-jingzhan-msft

How to perform calculations involving overall values under RLS constraints

Scenario: 

In some scenarios, we need to use RLS to restrict user’s access to certain data, but this can conflict with certain calculations involving overall data, such as rankings. In this article, I will show you how to achieve this.

 

Sample Data:

In the sample data, there are two tables.

Table 1 is the score table, including employee and channel.

vjingzhanmsft_0-1708492191446.png

Table 2 is the employee table, including the email address table of employee.

vjingzhanmsft_1-1708492208399.png

 

How:

If we use RLS directly on tables that involve the calculation of overall values, the result is often wrong.

 

For example: We ranked the scores of our employees and the results are as follows.

 

Here is the Rank measure:

 

Rank = 
RANKX (
    FILTER ( ALL ( 'Score' ), 'Score'[Channel] = MAX ( 'Score'[Channel] ) ),
    'Score'[Score],
    MAX ( 'Score'[Score] ),
    DESC,
    DENSE
)

 

vjingzhanmsft_2-1708492331743.png

If we use RLS directly, we are constrained by RLS and we cannot access all the values. Therefore the result is not correct.

The following are the steps to set up RLS:

1.From the Modeling tab, select Manage Roles.

vjingzhanmsft_3-1708492353075.png

2.From the Manage roles window, select Create.

vjingzhanmsft_4-1708492374170.png

3.Under Roles, provide a name for the role.

4.Under Tables, select the table to which you want to apply a DAX (Data Analysis Expression) rule.

5.In the Table filter DAX expression box, enter the DAX expressions.

vjingzhanmsft_5-1708492396522.png

After you've created your roles, test the results of the roles within Power BI Desktop.

1.From the Modeling tab, select View as.

vjingzhanmsft_6-1708492414317.png

2.The View as roles window appears, where you can see the roles you've created. Select a role you created. Then choose OK to apply that role.

vjingzhanmsft_7-1708492433852.png

3.Select OK.

vjingzhanmsft_8-1708492451271.png

This is our expected result:

vjingzhanmsft_9-1708492494395.png

According to our description, here are my steps we can follow as a solution.

1.Create a new table to have the overall ranking data:

 

Summarize Score = SUMMARIZE('Score',Score[Score],Score[Channel])

 

vjingzhanmsft_10-1708492543673.png

Note: Please avoid using this table in any other place since it could expose RLS data.

2.Since the summary table is only used for DAX calculations and not for creating any visuals, we hide it:

vjingzhanmsft_12-1708492592815.png

 

Note: There is no relationship between the summary table and other tables.

vjingzhanmsft_13-1708492639907.png

 

3.Create RLS in Table 2(Table Employee)

vjingzhanmsft_14-1708492655937.png

 

4.Since there is no relationship between the Summary table and other tables, it will not be affected by the RLS, while the RLS of the Employee table will be passed to the Score table, which will cause the Score to retain the data after being restricted by the RLS.

 

Now you can rank the scores of the employees, here is the measure:

 

Rank Employee = 
VAR _a =
    FILTER ( ALL ( 'Summarize Score' ), [Channel] = MAX ( 'Score'[Channel] ) )
VAR _b =
    ADDCOLUMNS ( _a, "Rank", RANKX ( _a, [Score] ) )
RETURN
    MAXX ( FILTER ( _b, [Score] = MAX ( 'Score'[Score] ) ), [Rank] )

 

 

Summary:

In some cases that involve the calculation of overall values but require the use of RLS, you can use a method like this to perform the calculation .

 

Hope this article will help people with similar questions.   

 

 

Author: Neeko Tang

Reviewer: Ula and Kerry