Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

DAX Code for Dynamic RLS

I am new to DAX and am trying to implement some dynamic RLS. The current table of users details looks like below:

User EmailUser NameAgent IDLevel of Access RequiredRegionCompany
[email protected]Test123123IndividualUSACompany1
[email protected]Test456456RegionalUKCompany2
[email protected]Test789789NationalAUSCompany3

 

What I want to do is give a user access at a level defined in 'Level of Access Required'.

This has 3 options:

1) National which will show all data for the specific company

2) Regional which will show all data for the specific company and the region

3) Individual which will show all data for user with the Agent ID

 

This table mentioned above will then be joined on to the other datasets within the report using a many to many join relationship I imagine?

 

Let me know if any further information is required.

  • sturlaws's avatar
    sturlaws
    6 years ago

    sorry, was a bit quick there, you need to write it like this

    VAR _t =
        CALCULATETABLE (
            VALUES ( 'Access_Table'[Access_Required] ),
            FILTER ( 'Access_Table', 'Access_Table'[User_Email] = USERPRINCIPALNAME () )
        )
    RETURN
        IF (
            CONTAINS ( _t, 'Access Table'[Access_Required], "National" )
                || CONTAINS ( _t, 'Access Table'[Access_Required], "Regional" ),
            'dimCompany'[Company]
                IN CALCULATETABLE (
                    VALUES ( 'Access_Table'[Company] ),
                    FILTER ( 'Access_Table', 'Access_Table'[User_Email] = USERPRINCIPALNAME () )
                ),
            TRUE ()
        )

18 Replies

  • sturlaws's avatar
    sturlaws
    Resident Rockstar

    Hi Anonymous,

    with your requirements, I don't think many-to-many relationships will be sufficient.

     

    You could try to use the "Table filter DAX expression" feature in the "Manage roles" menu. Here is an example of how you can write the DAX:

    'Sales'[Company]
        IN CALCULATETABLE (
            VALUES ( 'Sheet2'[Company] ),
            FILTER ( 'Sheet2', 'Sheet2'[userId] = USERNAME () )
        )

    If you provide a sample report(upload to onedrive/dropbox/other and share the link), it will be easier to guide you through how to set it up.

    regards,
    S

      • sturlaws's avatar
        sturlaws
        Resident Rockstar

        The sample you provided is a bit short on data, so it is not really suited to see the full effect of RLS. But this how I would set it up

        1. Change up the model to look more like a star model:


        2. Click on the manage roles-button, and create a role called e.g. all. Then on the tables which are prefixed with dim, add the dax statement which will filter the table according to the access level they have according to Access Table:

         

        3. You can then test your role like this, add the e-mail of the user you want to test for, and click 'OK'

        When you publish it to power bi services, you need to go to the Dataset section in the left menu, and for the dataset of your report, click the three dots and find Security. You should have a ad/security group called something like Everyone or All, add this group to the role you have created.

         

        pbi desktop file