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

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

jirineoral

Different approach to Dynamic Row Level Security

There were already written few blog posts both about Row Level Security. I would like to add one more about this topic. I use this pattern for several years in SQL Server Analysis Services. Biggest advantage of this approach is, that you don’t have to fight with table relationships, which can be sometimes tricky to make work correctly.
To follow with steps, you can download sample file, which uses Adventure Works Sample Data.

File can be downloaded here: https://drive.google.com/file/d/0B9ZohZ1CALKZOFZBRG9YZERWakk/view?usp=sharing

Introduction

Let’s have a look at the report and scenario. Adventure Works sales products, that are grouped to categories. Common requirement is to limit data in report, so category manager would see just his/her category. So if I want to grant access just to “Bikes” I could write DAX row filter

DAX formula:

[Category] = “Bikes”

This could work for few categories. But if you have 40. Role membership would be hard to maintain. Same thing can be handled by one role using configuration table.

01.jpg

Solution

For purpose of blog post, I will enter data manually using “Enter Data” option. You can replace data in table with real user names and email addresses. In real life I use for this configuration SQL Server Master Data Services, but any table would do.

02.jpg 

Table security can stay unrelated

03.jpg

 

What I’ll need is to create calculated column to DimProduct table. Let’s call it Security_Category using function lookupvalue searching in security configuration table.

DAX formula

Security_Category = LOOKUPVALUE(security_mapping[CATEGORY],security_mapping[CATEGORY],[Category],security_mapping[USER],"domain\username")

first column is the value I want to return, second is searched column, third value I want to find, fourth second column to be searched, fifth value.

Arguments 4,5 are here to restrict access to particular user if we had multiple users in same configuration table.

04.jpg

DAX filter needs to be evaluated as true value. I can check if anything was found by function ISBLANK function

DAX formula

Security_Category = ISBLANK(LOOKUPVALUE(security_mapping[CATEGORY],security_mapping[CATEGORY],[Category],security_mapping[USER],"domain\username"))

05.jpg

Formula returns now true, where records weren’t found. I need it oppositely and can switch logical value using function NOT

DAX formula

Security_Category = NOT(ISBLANK(LOOKUPVALUE(security_mapping[CATEGORY],security_mapping[CATEGORY],[Category],security_mapping[USER],"domain\username")))

06.jpg

As next step I can create new role Dynamic Security in Power BI desktop and apply Row Filter on DimProduct table referencing calculated column with formula from previous step.

07.jpg

I can check what if visible to role members using View as role Dynamic Security.

08.jpg

09.jpg

To make it really dynamic, I will replace static value “domain\username” with function USERNAME() in formula.

DAX formula:

Security_Category = NOT(ISBLANK(LOOKUPVALUE(security_mapping[CATEGORY],security_mapping[CATEGORY],[Category],security_mapping[USER],USERNAME())))

Function USERNAME behaves differently in desktop where it returns username in format “domain\username” and when published to powerbi.com where it returns email address username@domain.com

Therefore before publihing replace column USER with column EMAIL in DAX formula

Security_Category = NOT(ISBLANK(LOOKUPVALUE(security_mapping[CATEGORY],security_mapping[CATEGORY],[Category],security_mapping[EMAIL],USERNAME())))

Conclusion

In this blog post I covered how to implement dynamic security in Power BI without relationships in data model using DAX formulas. Let me know if you have any questions or comments

Jiri

Comments

@Anonymous

As @jirineoral already stated you are most probably the admin / publisher of your data set. Therefore, RLS settings won't have any effect on your user. I tried and tested the above mentioned procedure with another user. And it worked just fine.

 

Anonymous

@jirineoral@Markus_Hanisch thank you guys! It worked!!
The rls don't apply for the "owner", I tried with other user and it worked.

Hi Jiri,

 

Very interesting approach! I haven't had a chance to test yet, but will this model work if the same person belongs to multiple categories AND if multiple people belong to the same category?

 

Example:

 

Example Table.JPG

 

Hi @Brysonds,

yes it works, that's how I use it

 

Jiri

I understand the logic but when I try to use the USERNAME() I get an error because it says the function can't be used in a calculated column. Any help would be greatly appreciated.

 

ERROR:

CUSTOMDATA, USERNAME and USERPRINCIPALNAME functions are not supported in calculated tables/columns. These functions may only be used in Measures or in the AllowedRowsExpression.

hi @S-F

this was already addressed on ‎11-15-2016 08:32 AM. Please read previous comments


Jiri

Hi Jiri,

 

This is nice post!!

 

I have tables users,roles,application, each application having different roles ,each user havinf different roles here

i want to display if user is Admin role for that Particular application he has to see all others data also.if is not Admin he can see only his data .

 

I am taking all tables into my model and creating relation after that how i have to map in Manage roles Dynamically

Can you please help me on this.

 

Regards

Mallikarjun

Hi Mallikarjun,

There is no DAX function to check role membership as far as I'm aware

Maybe use some PowerShell script to list members?

 

Jiri

Understanding the Power BI admin role this link helps you to understand the admin role and the PowerShell script to get the object ID of the role as well as assign it to a user.

Then have a closer look at this PowerShell script:

Get-​Msol​Role​Member

Get-​Msol​Role​Member

 

I hope this helps.

Very helpful post thanks for sharing a knowledge with us

HI Jiri,

 

Thanks a lot for this post and it is very useful and interesting.

I tried the solution and worked , but I got error when the user has access to more than one category due to the lookup function returns multiple values, above there is some one asked about the same situation and you answer with that it will work, so can you tell me how to solve this problem because Iam stucking in this problem .

 

Regards

Mahmoud

HI Jiri,

 

Thanks, I did it , I had a mistake in one of the steps.

 

Thaaaaaaanks

Hi @mahmoud_sameer

you're wellcome 🙂 I love problems that are resolved before I can respond to them 😄

 

Jiri

Anonymous

I have implemented dynamic row level security and added table filters in manage roles as below. the below is created in Employee table. 

 

CONTAINS(
Users,
Users[Email ID],
USERNAME(),
Users[state],
[state]
)

 

Users Table:

 

Account ID,       Email ID,                           State

123                    krishan.1@gmail.com       MD,PA,OH

144                    k.2@gmail.com                 

 

Employee Table:

 

Account ID,    Employee phone number, Employee state, Employee City

123                 1234567890                       MD                     Glenbernie

144                  4567777774                      NY                       Jefferson

 

If i use above DAX expression when i logged in as krishan.1@gmail.com it is restricting but when i logged in with k.2@gmail.com the report is showing blank because there is no data in users table. 

 

If there is no records for state in users table then it has to show everything for that user not other user data.

 

How can i do this.

 

Please help.

 

Thanks,

Paruchuri

Hi @Anonymous

add unrestricted users to unrestricted roles

 

Jiri