Forum Discussion

Uhrond's avatar
Uhrond
Frequent Visitor
4 years ago
Solved

DAX For RLS

Hello everyone, I need an expert in DAX because I have trouble understanding the logic of it.   I'm trying to implement the RLS in my Power BI report. I have a nested set model as follow :  ...
  • Anonymous's avatar
    Anonymous
    4 years ago

    Hi Uhrond ,

     

    According to your screenshot, I think the hierachy level in your data model will make calculations difficult.

    We can see that C is the parent in level 2 and C is also the child in level 3. This will confuse Power BI. Secondly, there are multiple ranges in C, I think it is better for you to expand the numeric range by create a new calculated table.

    Basic User Table:

    Basic Data Table:

    Now, let's create new User Table and Data table by Dax.

    Expand User = 
    GENERATE (
        SUMMARIZECOLUMNS (
            'Basic User'[Name],
            'Basic User'[UserName],
            'Basic User'[Left],
            'Basic User'[Right]
        ),
        GENERATESERIES (
            CALCULATE ( DISTINCT ( 'Basic User'[Left] ) ),
            CALCULATE ( DISTINCT ( 'Basic User'[Right] ) )
        )
    )
    Expand Data = 
    GENERATE (
        SUMMARIZECOLUMNS ( 'Basic Data'[Data], 'Basic Data'[Left], 'Basic Data'[Right] ),
        GENERATESERIES (
            CALCULATE ( DISTINCT ( 'Basic Data'[Left] ) ),
            CALCULATE ( DISTINCT ( 'Basic Data'[Right] ) )
        )
    )

    Build a relationship between [Value] column in two tables. Trun on "Apply security filter in both directions" due to many to mant relationship.

    We can get the user who login Power BI by USERNAME() or USERPRINCIPALNAME(). Here I suggest you use USERPRINCIPALNAME(), it will get email format username in both Power BI Desktop and Service. 

    Mange Roles:

    Now let's see the report as [email protected]. Result is as below.

     

    Best Regards,
    Rico Zhou

     

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.