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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
Anonymous
Not applicable

Dynamically scan for a single value in different path items of a hierarchy

Hello there, 

To implement role based security, I am in need of a variable which checks for a Division ID in all of its path items of Division ID & Parent Division ID hierarchy. So, the current code I have used is as below assuming the path length will not exceed 15.



DivisionIDparentDivisionidPath_from_DAX
Div0Div0Div0
Div1Div0Div0|Div1
Div10Div5Div0|Div1|Div3|Div5|Div10
Div11Div5Div0|Div1|Div3|Div5|Div11
Div12Div7Div0|Div1|Div2|Div7|Div12
Div13Div7Div0|Div1|Div2|Div7|Div13
Div14Div10Div0|Div1|Div3|Div5|Div10|Div14
Div15Div10Div0|Div1|Div3|Div5|Div10|Div15
Div16Div14Div0|Div1|Div3|Div5|Div10|Div14|Div16
Div17Div14Div0|Div1|Div3|Div5|Div10|Div14|Div17
Div18Div17Div0|Div1|Div3|Div5|Div10|Div14|Div17|Div18
Div19Div17Div0|Div1|Div3|Div5|Div10|Div14|Div17|Div19
Div2Div1Div0|Div1|Div2
Div20Div17Div0|Div1|Div3|Div5|Div10|Div14|Div17|Div20
Div3Div1Div0|Div1|Div3
Div4Div3Div0|Div1|Div3|Div4
Div5Div3Div0|Div1|Div3|Div5
Div6Div3Div0|Div1|Div3|Div6
Div7Div2Div0|Div1|Div2|Div7
Div8Div2Div0|Div1|Div2|Div8
Div9Div2Div0|Div1|Div2|Div9

 

// the below variable selects the Division ID that is assigned to be given access to, to the logged in report user.

var vDivIDs=
SELECTCOLUMNS(
FILTER('Security_Org',[Employee ID]= username())
,"DivID",[Division ID]
)

 

// the below variable searches the division IDs returned in the above step in all the path items & returns all the division IDs it is related to. 

var vDivIDSets=
SELECTCOLUMNS(
FILTER('Division',
PATHITEM(Division[Path_from_DAX],1) in vDivIDs ||
PATHITEM(Division[Path_from_DAX],2) in vDivIDs ||
PATHITEM(Division[Path_from_DAX],3) in vDivIDs ||
PATHITEM(Division[Path_from_DAX],4) in vDivIDs ||
PATHITEM(Division[Path_from_DAX],5) in vDivIDs ||
PATHITEM(Division[Path_from_DAX],6) in vDivIDs ||
PATHITEM(Division[Path_from_DAX],7) in vDivIDs ||
PATHITEM(Division[Path_from_DAX],8) in vDivIDs ||
PATHITEM(Division[Path_from_DAX],9) in vDivIDs ||
PATHITEM(Division[Path_from_DAX],10) in vDivIDs ||
PATHITEM(Division[Path_from_DAX],11) in vDivIDs ||
PATHITEM(Division[Path_from_DAX],12) in vDivIDs ||
PATHITEM(Division[Path_from_DAX],13) in vDivIDs ||
PATHITEM(Division[Path_from_DAX],14) in vDivIDs ||
PATHITEM(Division[Path_from_DAX],15) in vDivIDs
)
,"AllDivs",[DivisionID])

 

I then use the Division IDs from vDivIDSets to filter my fact table & achieve the required row level security.

 

Just wondering if in future, an extra level comes up in data & without the need of going back to the code to add another condition, is there a way to optimise it to work for any number of levels?

Something in the lines of - 

var x = MAX(PATHLENGTH(Path_from_DAX))

var n = GENERATESERIES(1,x) 

var vDivIDSets =  FILTER('Division', PATHITEM(Division[Path_from_DAX],1) in vDivIDs || FILTER('Division', PATHITEM(Division[Path_from_DAX],1) in vDivIDs  || ......... FILTER('Division', PATHITEM(Division[Path_from_DAX],x) in vDivIDs 

 

Cheers,

Pravallika

1 ACCEPTED SOLUTION

@Anonymous Ah, then how about this?

VAR __Path = Division[Path_from_DAX]
VAR __PathLength = PATHLENGTH(__Path)
VAR __PathTable = 
  ADDCOLUMNS(
    GENERATESERIES(1,__PathLength,1),
    "Division ID",PATHITEM(__Path,[Value])
  )
RETURN
  INTERSECT(vDivIDs,__PathTable)

Basically, turn the path into a table and find the division ids that match.



Follow on LinkedIn
@ 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!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

4 REPLIES 4
Anonymous
Not applicable

@Greg_Deckler  Thanks a ton!

Greg_Deckler
Community Champion
Community Champion

@Anonymous Use PATHCONTAINS instead: PATHCONTAINS function (DAX) - DAX | Microsoft Docs



Follow on LinkedIn
@ 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!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...
Anonymous
Not applicable

Hi @Greg_Deckler , thanks for your response, but how can we pass multiple values to pathcontains function, as it expects only one value.

 

For example,  we are to pass pathcontains(path, vDivIDs) .. if vDivIDs return just one division, say D2 this will work straight, vDivIDs can also return multiple divisions.

 

PATHCONTAINS and Multiple selected value - Microsoft Power BI Community

Have gone through this, once the columns are created, it looks like the equivalent of above code. How to then search for the division IDs in those columns created for each path item.. also, there is no aggregation in need, just a search through all the individual columns generated for each path item..

 

Any inputs would be of great help!

 

Cheers,

Pravallika

@Anonymous Ah, then how about this?

VAR __Path = Division[Path_from_DAX]
VAR __PathLength = PATHLENGTH(__Path)
VAR __PathTable = 
  ADDCOLUMNS(
    GENERATESERIES(1,__PathLength,1),
    "Division ID",PATHITEM(__Path,[Value])
  )
RETURN
  INTERSECT(vDivIDs,__PathTable)

Basically, turn the path into a table and find the division ids that match.



Follow on LinkedIn
@ 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!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

Check out the August 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

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