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

Display selected values of hierarchy slicer on a card

Hi all,

 

I have a 2-level hierarchy slicer. 

Kristi_0-1602507946647.png

I want to display the selected values. In order to achieve this, I use a measure that I found here on another thread :

 

 

Selected Slicer = 
SWITCH (
    TRUE (),
    ISFILTERED ( 'Table'[Level 2 ] ), CONCATENATEX ( VALUES ( 'Table'[Level 2 ] ), [Level 2 ], "," ),
    ISFILTERED ( 'Table'[Level 1] ), CONCATENATEX ( VALUES ( 'Table'[Level 1] ), [Level 1], "," )
)

 

 

This works great, instead of one case.

My issue is that when I choose a combination of a complete high level AND some lower level values, I get a display of all selected lower values. 

 

To demonstrate it in a clear manner:

 

This is the desired result

Kristi_1-1602508264135.png

 

This is the desired result

Kristi_2-1602508352576.png

 

This is NOT the desired result. Instead, I want the card to display "1,e".

Kristi_3-1602508400538.png

Here are some sample data:

Level 1Level 2
1a
1b
1c
2d
2e
3f
3g

 

I hope this makes sense, it is my first attempt to ask for help so if I need to provide anything more pls let me know!

 

Thank you,

Kristina

 

1 ACCEPTED SOLUTION

Ok, this version seems to work for me. Please validate:

 

 

Selected Slicer = 
var s = SELECTCOLUMNS('Table',"Lev1",'Table'[Level 1],"Lev2",'Table'[Level 2])
var a = ADDCOLUMNS(s,"l2a",CONCATENATEX(filter(ALL('Table'),'Table'[Level 1]=[Lev1]),'Table'[Level 2],",")
                    ,"l2s",CONCATENATEX(filter(ALLSELECTED('Table'),'Table'[Level 1]=[Lev1]),'Table'[Level 2],","))
var b = ADDCOLUMNS(a,"res",if([l2a]=[l2s],[Lev1],[Lev2]))
var c = SUMMARIZE(b,[res])
return CONCATENATEX(c,[res],",")

 

Here's another version, slightly more concise, but same idea.

ss2 :=
VAR s = SELECTCOLUMNS ( 'Table', "Lev1", 'Table'[Level 1], "Lev2", 'Table'[Level 2] )
VAR b = ADDCOLUMNS (s,"res",
            IF ( COUNTROWS (
                    EXCEPT (
                        FILTER ( ALL ( 'Table' ), 'Table'[Level 1] = [Lev1] ),
                        FILTER ( ALLSELECTED ( 'Table' ), 'Table'[Level 1] = [Lev1] )
                    )
                ) = 0,[Lev1],[Lev2] )
    )
RETURN CONCATENATEX ( SUMMARIZE ( b, [res] ), [res], "," )

 

View solution in original post

19 REPLIES 19
smodsy
New Member

Hi there,

 

Thank you for this awesome solution... I've managed to adapt it to the three levels I have within my hierachy but for some reason it only ever displays the office level (level 3) for me?  Have I adapted it incorrectly?  

 

Location Slicer = 
VAR s = SELECTCOLUMNS ( 'Data', "Lev1", 'Data'[User Office Region], "Lev2", 'Data'[User Country], "Lev3", 'Data'[User Office] )
VAR b = ADDCOLUMNS (s,"res",
            IF ( COUNTROWS (
                    EXCEPT (
                        FILTER ( ALL ( 'Data' ), 'Data'[User Office Region] = [Lev1] ),
                        FILTER ( ALLSELECTED ( 'Data' ), 'Data'[User Office Region] = [Lev1] )
                    )
                ) = 0,[Lev1],IF ( COUNTROWS (
                    EXCEPT (
                        FILTER ( ALL ( 'Data' ), 'Data'[User Country] = [Lev2] ),
                        FILTER ( ALLSELECTED ( 'Data' ), 'Data'[User Country] = [Lev2] )
)
                                    ) = 0,[Lev2] ,[Lev3]))
    )
RETURN CONCATENATEX ( SUMMARIZE ( b, [res] ), [res], ", " )

 

Also, is there a way to adapt it so that if 'Select All' is use it displays 'All Locations' instead of the 40+ locations I have?

lbendlin
Super User
Super User

Still working on an answer, just wanted to say that you cannot use SWITCH in this scenario.  You need to calculate the result for each row of your data, and use the Level 1 value whenever the number of Level 2 filters is the same as the number of Level 2 values.

 

But that is rather tough to actually implement.  Nice challenge!

Ok, this version seems to work for me. Please validate:

 

 

Selected Slicer = 
var s = SELECTCOLUMNS('Table',"Lev1",'Table'[Level 1],"Lev2",'Table'[Level 2])
var a = ADDCOLUMNS(s,"l2a",CONCATENATEX(filter(ALL('Table'),'Table'[Level 1]=[Lev1]),'Table'[Level 2],",")
                    ,"l2s",CONCATENATEX(filter(ALLSELECTED('Table'),'Table'[Level 1]=[Lev1]),'Table'[Level 2],","))
var b = ADDCOLUMNS(a,"res",if([l2a]=[l2s],[Lev1],[Lev2]))
var c = SUMMARIZE(b,[res])
return CONCATENATEX(c,[res],",")

 

Here's another version, slightly more concise, but same idea.

ss2 :=
VAR s = SELECTCOLUMNS ( 'Table', "Lev1", 'Table'[Level 1], "Lev2", 'Table'[Level 2] )
VAR b = ADDCOLUMNS (s,"res",
            IF ( COUNTROWS (
                    EXCEPT (
                        FILTER ( ALL ( 'Table' ), 'Table'[Level 1] = [Lev1] ),
                        FILTER ( ALLSELECTED ( 'Table' ), 'Table'[Level 1] = [Lev1] )
                    )
                ) = 0,[Lev1],[Lev2] )
    )
RETURN CONCATENATEX ( SUMMARIZE ( b, [res] ), [res], "," )

 

Hi there - Thanks this was a real useful piece of Dax!

risa
Frequent Visitor

 Is there a way this same can be done for 4 level hierarchy slicer? and while using the above solution for 2 level slicer, I am getting some values when I dont select anything in the slicer...

Please provide sample data (with sensitive information removed) that covers your issue or question completely, in a usable format (not as a screenshot).
https://community.fabric.microsoft.com/t5/Community-Blog/How-to-provide-sample-data-in-the-Power-BI-...
Please show the expected outcome based on the sample data you provided.


risa
Frequent Visitor

Thanks for the quick reply. Here is the sample table:

IndexL1L2L3L4
1AA1A12A121
2BB1B12B121
3CC1C12C121
4AA2A21A212
5BB2B22B221
6CC2C21C211
7AA1A11A112
8BB1B12B121
9CC1C11C111
10AA2A21A211
11BB2B22B221
12CC2C21C212
13AA1A12A122
14BB1B11B111
15CC1C12C122
16AA2A21A211
17BB2B22B222
18CC2C22C221

 

The measures I used:

items = SWITCH (TRUE (),
    ISFILTERED ( Sheet1[L4] ), CONCATENATEX(ALLSELECTED( Sheet1[L4]), Sheet1[L4], " & ") ,
    ISFILTERED (  Sheet1[L2]), CONCATENATEX(ALLSELECTED(Sheet1[L2]), Sheet1[L2], " & ") ,
    ISFILTERED ( Sheet1[L3] ), CONCATENATEX(ALLSELECTED(Sheet1[L3]),Sheet1[L3], " & ") ,
    ISFILTERED ( Sheet1[L1]), CONCATENATEX(ALLSELECTED(Sheet1[L1]), Sheet1[L1], " & ")
)

Expected out come:

risa_0-1689132457569.png

 


Expected outcome:

risa_0-1689131834682.png



Below was not the expected outcome

risa_1-1689131924717.png

Expexted outcome: A & B111.


How do I get the expected outcome here?

Same formula.

 

 

Measure = 
VAR s = SELECTCOLUMNS ( 'Sheet1', "Lev1", 'Sheet1'[L1], "Lev2", 'Sheet1'[L2], "Lev3", 'Sheet1'[L3], "Lev4", 'Sheet1'[L4] )
VAR b = ADDCOLUMNS (s,"res",
            IF ( COUNTROWS (
                    EXCEPT (
                        FILTER ( ALL ( 'Sheet1' ), 'Sheet1'[L1] = [Lev1] ),
                        FILTER ( ALLSELECTED ( 'Sheet1' ), 'Sheet1'[L1] = [Lev1] )
                    )
                ) = 0,[Lev1],IF ( COUNTROWS (
                    EXCEPT (
                        FILTER ( ALL ( 'Sheet1' ), 'Sheet1'[L2] = [Lev2] ),
                        FILTER ( ALLSELECTED ( 'Sheet1' ), 'Sheet1'[L2] = [Lev2] )
                    )
                ) = 0,[Lev2],IF ( COUNTROWS (
                    EXCEPT (
                        FILTER ( ALL ( 'Sheet1' ), 'Sheet1'[L3] = [Lev3] ),
                        FILTER ( ALLSELECTED ( 'Sheet1' ), 'Sheet1'[L3] = [Lev3] )
                    )
                ) = 0,[Lev3] ,[Lev4])))
    )
RETURN CONCATENATEX ( SUMMARIZE ( b, [res] ), [res], " & " )

 

lbendlin_0-1689203765889.png

Your problem is that B11 only has one child. Therefore B111 is not honored.  Adding another value gives the desired result.

 

lbendlin_1-1689203940029.png

 

risa
Frequent Visitor

I have 2 tables like the above, and are connected. Can I use 2 slicers(from both tables) and get the above desired output on a single card?

i cannot help you if you cannot provide appropriate sample data.

risa
Frequent Visitor

Hi Ibendin, Thank you for the continued replies. I have included the pbix file link below. I used your measure here and having some bugs here. can you fix the measure for the card here to only display all the values selected in the the slicer1 and slicer2.

pbix:
https://drive.google.com/file/d/11oU0u6MmHFpmZiYbntMkYMQfRze4Stpn/view?usp=drivesdk

Your bugs are in your data and data model. Your formula works fine.

Anonymous
Not applicable

Is there a way to implement this with a hierarchy with 10 levels?

You can use the same methodology but I would seriously challenge the usefulness for your users.

 

Please provide sanitized sample data that fully covers your issue.

Please show the expected outcome based on the sample data you provided.

Anonymous
Not applicable

Thank you for the quick reply. I actually don't need every node selected to display, I would just need the highest level in the hierarchy selected. For example if node e. Division level and f. Manger level are selected I would just need e. Division level to display. Does that make sense?

savi_1-1649083180759.png

 

You can use FILTERS() for the levels you are interested in, but that will not give you the hierarchy.

Thanks for this sollution, for me it works almowst perfectly except the fact that I have 8 levels into my hierarchy. How can I do this but with multiple nodes?
Thanks a lot 😉

Anonymous
Not applicable

Ibendlin this is great, works perfectly! I knew that switch would not do the trick and had tried so many things that didn't work. I had lost hope 😅. Thank you so much!

It was really tough for me as well. As the Italians say : " DAX is simple, it is not easy."  I am pretty sure there are more accurate and more performant solutions.  But for this scope (single slicer) it might be ok. 

What helps me is to break the problem down into baby steps and to use CONCATENATEX along the way to verify that I get the expected intermediate results. And I also learned that you should design a measure to work for the Totals, rather than for the individual "rows".

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

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