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.

Reply
Uhrond
Frequent Visitor

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 : 

Uhrond_4-1646143697172.png

 

Which is stored in a first table :

Uhrond_5-1646143745897.png

 

Then my data table that I want to filter also containing left and right values :

Uhrond_6-1646143802606.png

 

So when "C" connects to the report ("C" comes from USERNAME() ), I want my data table to be filtered like this :

Uhrond_7-1646143963635.png

 

Because "C" has access to all data that has a left greater or equal than 3 and a right less or equal than 4
AND the data that has a left greater or equal than 8 and a right less or equal than 13.

This is what I want to convert to DAX.

 

In this example I only have two lines for "C" but I can have more.

 

Thanks for your help, hope this is clear enough

 

1 ACCEPTED SOLUTION

Hi @Uhrond ,

 

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

1.png

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:

1.png

Basic Data Table:

2.png

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.

1.png

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:

1.png

Now let's see the report as C@xxx.com. Result is as below.

1.png

 

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.

 

View solution in original post

4 REPLIES 4
Uhrond
Frequent Visitor

Thanks for spending time on my problem.

Greg_Deckler
Super User
Super User

@Uhrond I don't believe you have enough information in your tables to dynamically create/enforce the rules you have laid out. For example, there is nothing that indicates that the C rows should be less than versus greater than. Also, your comment about more than two rows is concerning because not sure how that would work.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Thank you for your answer.

I'm a developer and used to algorithm logic. What I want could be done with this simple code :

foreach(dataRow in Data)
{
	foreach(struc in StructureTable)
	{
		if(dataRow.Left >= struc.Left && dataRow.Right <= struc.Right)
			return true;
		else
			return false;
	}
}


I know DAX doesn't work the same at all. But I thought there had to be a way to do it. 😕

 

Hi @Uhrond ,

 

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

1.png

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:

1.png

Basic Data Table:

2.png

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.

1.png

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:

1.png

Now let's see the report as C@xxx.com. Result is as below.

1.png

 

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.

 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors