Sunday, March 31, 2024

Develop and take a look at RLS Guidelines in Energy BI | by Salvatore Cagliari | Jun, 2023

Must read


Fairly often, not all Customers ought to have permission to entry all information in a Report. Right here I’ll clarify learn how to develop RLS Guidelines in Energy BI to configure entry and learn how to take a look at them.

Towards Data Science
Picture by FLY:D on Unsplash

Lots of my shoppers wish to limit entry to the information of their stories primarily based on particular guidelines.

Entry to information known as Row Degree Safety (RLS briefly).

You will discover many articles about RLS in Energy BI on Medium.

I added two of them within the References part under.

Whereas all of the articles do a great job of explaining the fundamentals, I at all times miss a proof on learn how to develop extra complicated guidelines and learn how to take a look at them simply.

On this article, I’ll clarify the fundamentals of RLS and add complexity step-by-step.

As well as, I’ll present you learn how to use DAX Studio to construct queries to check RLS guidelines earlier than including them to the information mannequin.

So, right here we’re.

I take advantage of the Situation the place Customers get entry to Retail Gross sales information primarily based on Shops or the geographic places of Shops inside the firm, together with a mix of each.

Within the Contoso information mannequin, I take advantage of the next tables:

Tables involved in my scenario (Figure by the Author)
Determine 1 — Tables concerned in my state of affairs (Determine by the Writer)

I create the next report to check my outcomes:

Starting report (Figure by the Author)
Determine 2 — Beginning report (Determine by the Writer)

To create an RLS Rule, you want to open the Safety position editor:

Open the Security role editor (Figure by the Author)
Determine 3 — Open the Safety position editor (Determine by the Writer)

Subsequent, you may create a brand new Function and set the identify for this Function:

Create a Role and rename it (Figure by the Author)
Determine 4 — Create a Function and rename it (Determine by the Writer)

In my case, I put the identify to “StorePermissions”.

Now, I can begin including an expression to regulate entry to the Retailer desk:

Add a DAX expression to the new Role (Figure by the Author)
Determine 5 — Add a DAX expression to the brand new Function (Determine by the Writer)

We’ve had a brand new, easier editor for RLS guidelines for a couple of months.

In my case, I wish to add a DAX expression. So, I click on on the “Swap to DAX editor” button.

At first, I add the only potential expression: TRUE()

Simplest possible RLS rule (Figure by the Author)
Determine 6 — Easiest potential RLS rule (Determine by the Writer)

To grasp RLS Guidelines, you need to know that entry is managed by the output of the expression within the RLS rule editor.

The person will get entry if the output of the expression is just not empty or FALSE().

In precept, any expression within the RLS rule editor is added as a filter to any question.
Let’s take a look at the impact of this primary expression earlier than I clarify this in additional element.

To check the Rule, I save the expression and shut the Editor.

Now I can View the report with the brand new Rule:

Test the RLS rule (Figure by the Author)
Determine 7 — Take a look at the RLS rule (Determine by the Writer)

On prime of the report web page, you will note a yellow banner exhibiting that you’re trying on the report utilizing the StorePermission Rule.

Because the StorePermission rule doesn’t limit entry, you’ll not see any distinction.

Let’s attempt one thing completely different.

Now I modify the Expression within the RLS rule to FALSE().

Once I take a look at the Rule, I can’t see any information:

Test the Rule with FALSE() (Figure by the Author)
Determine 8 — Take a look at the Rule with FALSE() (Determine by the Writer)

This proves that information is accessible if the expression doesn’t return FALSE().

To grasp this impact intimately, let me present a DAX question to get the end result with none restrictions:

EVALUATE
SUMMARIZECOLUMNS(
Retailer[Store]
,"Retail_Sales", 'All Measures'[Retail Sales]
)
ORDER BY Retailer[Store]

Once I add an RLS rule with TRUE(), as proven above, the question adjustments to a question much like this:

EVALUATE
FILTER(
SUMMARIZECOLUMNS(
Retailer[Store]
,"Retail_Sales", 'All Measures'[Retail Sales]
)
,TRUE()
)
ORDER BY Retailer[Store]

I enclosed the question inside a FILTER() perform and added TRUE() because the filter expression.

Within the following examples, I’ll use CALCULATETABLE(), as writing the code is extra environment friendly and versatile.

Extra on this in a bit.

Subsequent, I wish to limit entry to all Shops containing the “Contoso T” String.

For this, I modify the Expression within the Rule editor to the next:

CONTAINSSTRING('Retailer'[Store], "Contoso T")

When testing the rule, I get the next end result:

Result for restricting access to “Contoso T” Stores (Figure by the Author)
Determine 9 — Consequence for proscribing entry to “Contoso T” Shops (Determine by the Writer)

It might be good to check the result of such a rule with a DAX Question.

On this case, I take advantage of the next Question in DAX Studio to test the Consequence:

EVALUATE
CALCULATETABLE(
SUMMARIZECOLUMNS(
Retailer[Store]
,"Retail_Sales", 'All Measures'[Retail Sales]
)
CONTAINSSTRING('Retailer'[Store], "Contoso T") = TRUE()
)
ORDER BY Retailer[Store]

The inside half, with SUMMARIZECOLUMNS(), generates the output desk.

On this case, I’m solely within the listing of the shops.

Then, I enclose the SUMMARIZECOLUMNS() name with CALCULATETABLE() so as to add a filter to the question.

On this case, I add the expression from the RLS rule, together with an “= TRUE()” test.

The result’s the next:

Result of check query (Figure by the Author)
Determine 10 — Results of test question (Determine by the Writer)

However what occurs below the hood?

Let’s take a look at the Storage Engine Question:

Result of the check Query (Figure by the Author)
Determine 11 — Results of the test Question (Determine by the Writer)

And what occurs after I apply the RLS rule to this question?

I can apply an RLS rule from DAX Studio with a couple of clicks:

Activate an RLS rule (Figure by the Author)
Determine 12 — Activate an RLS rule (Determine by the Writer)

The Storage Engine question is the next:

Query Analysis with the RLS Rule
Determine 13 — Question Evaluation with the RLS Rule

The primary question (Line 2) retrieves the listing of all Shops.

The second question consists of the RLS rule within the WHERE clause.

As a substitute of getting the listing matching Shops (In line with the Filter), we see a cryptic line, which incorporates the RLS rule.

You possibly can see that the results of the Storage Engine (SE) question nonetheless accommodates 309 Rows, like above, which is the variety of all Shops + 3 rows.
A touch why we now have the discrepancy of three rows is within the textual content under the SE question: Estimated dimension: rows = 309

The precise variety of rows returned could also be certainly 306.

However this evaluation reveals that RLS guidelines are utilized after the Storage Engine, because the question end result accommodates solely 21 rows: All Shops which begin with “Contoso T”.

That is vital, because the Formulation Engine (FE), which is able to compute the ultimate end result after the Storage Engine, is single-threaded and might use just one CPU Core.

Whereas the SE is multi-threaded and might use a number of CPU cores.

Consequently, we should chorus from writing inefficient code into the RLS rule.

Subsequent, I wish to mix two expressions:

  1. Solely Shops beginning with “Contoso T”
  2. Solely Shops in Europe

To realize this, I add a second expression to the Geography desk utilizing the straightforward editor:

Add expression to the Geography table (Figure by the Author)
Determine 14 — Add expression to the Geography desk (Determine by the Writer)

Once I change to the DAX Editor, I get the next expression:

DAX Expression from the Simple editor (Figure by the Author)
Determine 15 — DAX Expression from the Easy editor (Determine by the Writer)

Discover using the strict equal operator.

Altering to the straightforward equal operator in your expression will be vital.

That is the end result when testing the rule:

Result of the combined rule (Figure by the Author)
Determine 16 — Results of the mixed rule (Determine by the Writer)

The DAX question for this rule will seem like this:

Translation to a DAX query and results (Figure by the Author)
Determine 17 — Translation to a DAX question and outcomes (Determine by the Writer)

Now, let’s add one other stage of complexity to the RLS rule:

I wish to limit entry to the Shops which both:

  • The identify of the Shops begins with “Contoso T” and are in Europe
    or
  • The identify of the Shops begins with “Contoso S” and are in North America

This time, I start with the DAX question. That is the easier strategy to develop and take a look at the expression.

I take the primary question and enclose it with the filter expression.

As I must filter two tables (Retailer and Geography), I have to use FILTER() and RELATED():

EVALUATE
CALCULATETABLE(
ADDCOLUMNS(
SUMMARIZECOLUMNS(Retailer[Store], 'Geography'[Continent])
,"Retail_Sales", 'All Measures'[Retail Sales]
)
,FILTER(Retailer
,OR(CONTAINSSTRING('Retailer'[Store], "Contoso T") && RELATED(Geography[Continent]) = "Europe"
,CONTAINSSTRING('Retailer'[Store], "Contoso S") && RELATED(Geography[Continent]) = "North America")
)
)
ORDER BY [Retail Sales] DESC, 'Geography'[Continent], Retailer[Store]

I would like the RELATED() perform as I take advantage of FILTER() to iterate by way of the Retailer desk, and I would like the Continent column from the Geography desk.

Because the Geography desk is on the one aspect of the Relationship, I can use RELATED() to get the Continent column.

That is the end result:

Query for the combined Rule (Figure by the Author)
Determine 18 — Question for the mixed Rule (Determine by the Writer)

Subsequent, we should translate this filter to an RLS rule.

For the RLS rule, we are able to take away the FILTER() perform, because the RLS rule inherently works as a filter.

Translation to one RLS Rule (Figure by the Author)
Determine 19 — Translation to at least one RLS Rule (Determine by the Writer)

Notice that I eliminated the expression from the “Geography” desk.

Once I take a look at this rule in Energy BI, I get the next end result, which corresponds to the end result from the DAX question:

Testing the combined RLS rule (Figure by the Author)
Determine 20 — Testing the mixed RLS rule (Determine by the Writer)

For testing the RLS rule, for instance, whenever you need solely to get the listing of filtered shops, you may write a easy question with simply the FILTER() perform:

Executing the FILTER() only (Figure by the Author)
Determine 21 — Executing the FILTER() solely (Determine by the Writer)

Till now, we checked out static RLS guidelines.

However more often than not, we want guidelines primarily based on the Person-Login.

To realize this, we want a desk that maps the person to the rows the person wants entry to.

For instance, a desk like this:

User List with assigned Geographies (Figure by the Author)
Determine 22 — Person Listing with assigned Geographies (Determine by the Writer)

After including the desk to the information mannequin, we have to add a Relationship between the brand new desk and the “Geography” desk:

Expanded data model (Figure by the Author)
Determine 23 — Expanded information mannequin (Determine by the Writer)

The connection between the brand new “Geography Entry” desk and the “Geography” desk have to be configured appropriately.

After including the Relationship, Energy BI configures it as a 1:n Relationship, with the “Geography” desk on the one aspect and the Filter flowing from the “Geography” desk to “Geography Entry”.

However we wish to filter the “Geography” desk primarily based on an RLS rule (a filter) on “Geography Entry”.
Because of this, we should change the cross-filter course to each:

Settings of the Relationship (Figure by the Author)
Determine 24 — Settings of the Relationship (Determine by the Writer)

As well as, we should set the flag on “Apply safety filter in each instructions,” as Energy BI ignores the cross-filter course setting when making use of RLS guidelines.

Now we are able to add the RLS rule:

Configure the RLS Rule (Figure by the Author)
Determine 25 — Configure the RLS Rule (Determine by the Writer)

Keep in mind to take away any filter expression on the Retailer desk earlier than including this rule.

When testing the RLS rule, I get this:

Empty result (Figure by the Author)
Determine 26 — Empty end result (Determine by the Writer)

To seek out out what occurs, let’s return to the RLS rule editor and alter the view for the Rule to DAX:

Wrong RLS rule (Figure by the Author)
Determine 27 — Fallacious RLS rule (Determine by the Writer)

The straightforward RLS rule editor doesn’t acknowledge DAX features and provides them as Textual content to filter.

We should change the expression to this:

Correct DAX rule (Figure by the Author)
Determine 28 — Right DAX rule (Determine by the Writer)

Now the result’s as anticipated:

Testing the RLS rule with my user and the correct RLS expression (Figure by the Author)
Determine 29 — Testing the RLS rule with my person and the right RLS expression (Determine by the Writer)

The Card on the top-left nook of the report web page accommodates a Measure with the USERPRINCIPALNAME() perform to make sure that the right person is lively in the course of the take a look at.

I may even take a look at an RLS rule utilizing one other person:

Test the RLS rule with another user (Figure by the Author)
Determine 30 — Take a look at the RLS rule with one other person (Determine by the Writer)

It’s humorous that this person doesn’t must exist. It solely must be contained within the “Geography Entry” listing.

Right here is the results of the take a look at:

Test-Result with test user (Figure by the Author)
Determine 31 — Take a look at-Consequence with take a look at person (Determine by the Writer)

Within the yellow line on prime, you may see the lively person in the course of the take a look at.

I confirmed you learn how to create elementary RLS guidelines and learn how to take a look at them.

Then I added extra complexity and analyzed the consequences of RLS guidelines on the underlying Storage engine.

We’ve seen that the Formulation Engine processes a part of the RLS rule. Subsequently, we should write environment friendly code within the RLS guidelines.

Figuring out learn how to take a look at RLS guidelines earlier than implementing them within the information mannequin is essential.

It’s a lot simpler to know incorrect outcomes by understanding how the rule is utilized to the information mannequin.

Lastly, I added dynamic user-based RLS guidelines to the mannequin.

These guidelines are tougher to check in a DAX question, as you need to know which information every person can entry to write down the right take a look at question to validate the end result.

I hope I’ve given you some hints on simplifying your life with the RLS function in Energy BI.

Picture by Andrew George on Unsplash

You will discover a listing of Safety features in Energy BI on this Article:

You will discover a easy rationalization about Row Degree Safety in Energy BI on the Energy BI (Now Material) Group web page: Row-level safety (RLS) with Energy BI — Energy BI | Microsoft Be taught.

I can advocate this text by Nikola Ilic, the place you will get a place to begin about RLS:

One other good introductory article on Row-Degree-Safety in Energy BI by Elias Nordlinder:

Go to my Tales Listing for extra details about the FILTER() perform and learn how to analyze DAX Question with DAX Studio.

I take advantage of the Contoso pattern dataset, like in my earlier articles. You possibly can obtain the ContosoRetailDW Dataset free of charge from Microsoft right here.

The Contoso Information will be freely used below the MIT License, as described right here.



Supply hyperlink

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest article