DAX Workout 009 - Displaying items that are in your filter

Hi everyone,

Most PBI reports and dashboards contain filters/slicers. It then becomes very important to ensure that your users are aware of what filters are applied. So this workout is focused on that. For those of you new to DAX, this should be a great workout as it doesn’t require alot of coding; for the experienced ones, it’ll be a great way to provide others with a technique that can be added to reports.

In this situation, there is a chart showing the number of sales by product. A slicer is also provided that allows you to select one or more countries. The problem is that the slicer is a drop down and thus it’s not readily visible as to what counties might be selected.

This workout has two parts.

The first part is around creating a DAX measure that will display all the countries that are selected (or displaying “All Countries Selected” if this is the case. As an added step, have the list be sorted alphabetically. In the attached PBI file, I have added a simple Table visual to test this out, however, other visuals (such as the Card visual, smart narrative, etc.) are also great ways to display this.

The second part is to create a second DAX measure that can be used in the “Subtitle” for the chart. However, rather than list out multiple countries (as above), some additional criteria is provided. The second and third pictures provide an example of this as well.

A sample PBI file is provided.

Workout 009_Posting.pbix (1.4 MB)



Submission
Load the supplied PBI file, create your solution, and reply to this post. Upload a screenshot of your solution (or the PBI file) along with any DAX items. Please blur any solution or place it in a hidden section.

1 Like

Hmm, I can’t seem to understand how can I maybe sort the name of countries selected. Would love to see how people solve this.
Maybe we can make another table at Data Level with Distinct Country names and use that? Not sure.

Rest is quite done.

DAX for Countries
Countries Selected = 

IF(
    ISFILTERED(Regions[Country]),
    CONCATENATEX
    (
        ALLSELECTED(Regions[Country]), Regions[Country], ", "
    ), 
    "All Countries are Selected"
)

DAX for Subtitle
Subtitle = 

    IF(
        ISFILTERED(Regions[Country]), 
        SELECTEDVALUE(Regions[Country], "Multiple Countries"), 
        "All Countries"
    )

Here’s the Screenshot of Final result

Workout 009_Posting.pbix (1.4 MB)

1 Like

@hassanashaskhan

Here is a way to address the sorting.

Summary

In your CONCATENATEX function, there is a 4th option where you can provide an ASC or DESC value and this will take care of the sorting.

1 Like

[Not a solution submission. Just a comment.]

Another possible method is the function

DAX Code

TOCSV

Country Filters Applied = 
VAR _SelectedCountries = DISTINCT ( Regions[Country] )
VAR _Result =
IF (
    COUNTROWS ( _SelectedCountries ) = COUNTROWS ( ALL ( Regions[Country] ) ),
    "All Countries Selected",
    TOCSV ( _SelectedCountries, -1, , FALSE() )
)
RETURN
    _Result

I find this function convenient when I want to display multiple columns (or quickly debug a table variable) but it has the limitation of not allowing for sorting and it always outputs a new line for each item rather than allowing for custom delimiters between table rows, so the output looks like this:

Screen snip

image

You are kidding me!
Oh my God, why did I never know that, lol!
Didn’t even notice this in the intelligense while writing the code. Thank you so much for pointing this out!

@AlexisOlson ,

I find TOCSV enormously useful for debugging - one of my favorite feature adds from last year.

I haven’t tried this, and not sure it would be worth the headache, but could you harvest the number of countries selected, build a one column TOPN table of that number of rows sorted by country name, and then send that table to TOCSV?

@tweinzapfel ,

Great, practical workout. I’ve seen @Greg use this technique very effectively in his reports with multiple slicers to mirror the slicer selections at the bottom of the page in a small row - very useful in particular when using a popout slicer panel.

Here’s my solution:

Show Countries Filtered measure

Country Filters Applied = 
VAR SelCountries =
DISTINCT( Regions[Country] )

VAR AllCountryNum = 
COUNTROWS( ALL( Regions[Country] ))

VAR Result =
IF(
    COUNTROWS(SelCountries) = AllCountryNum,
        "All Countries Selected",
    CONCATENATEX(
        SelCountries, 
        Regions[Country],
        ", ",
        Regions[Country], 
        ASC
    )
)

RETURN Result
Subtitle measure


Subtitle = 

VAR SelCountryCount =
COUNTROWS( DISTINCT( Regions[Country] ))

VAR AllCountryCount = 
COUNTROWS( ALL(Regions[Country] ))

VAR Result =
SWITCH(TRUE(),
    SelCountryCount = 1, SELECTEDVALUE( Regions[Country] ) & " Selected",
    SelCountryCount = AllCountryCount, "All Countries Selected",
    "Multiple Countries Selected"
)

RETURN Result

1 Like

I considered using TOPN but, while it allows for an Order By expression for choosing which rows to return, the documentation states that “TOPN does not guarantee any sort order for the results.”

@AlexisOlson ,

Have you seen this video? Would be an insane level of effort to implement this as an alternative to CONCATENATEX, but it does seem possible to guarantee the sort order of the TOPN table feeding into TOCSV:

  • Brian

This appears to depend on CONCATENATEX. If the dependency can’t be removed, then it’s not really an alternative.

Here’s a different attempt. I’m not sure this guarantees sort order but it seems to work for me:

DAX Code
Country Filters Applied = 
VAR _Countries = DISTINCT ( Regions[Country] )
VAR _Ranked =
    ADDCOLUMNS (
        _Countries,
        "Value", RANKX ( _Countries, [Country], , ASC )
    )
VAR _Series = GENERATESERIES ( 1, COUNTROWS ( _Countries ) )
VAR _SortRanked = NATURALLEFTOUTERJOIN ( _Series , _Ranked )
VAR _CountryCol = SELECTCOLUMNS ( _SortRanked, [Country] )
VAR _Result = TOCSV ( _CountryCol, -1, , FALSE() )
RETURN
    _Result
1 Like

Hi Tim,
Thanks for the workout. They always make me think!

Here is my solution:

Dax:

Summary


image

1 Like

Summary
Country Filters Applied = 
VAR __f = FILTERS ( Regions[Country] ) 
VAR __r = COUNTROWS ( __f ) 
VAR __N = CALCULATE ( 
    DISTINCTCOUNT ( Regions[Country] )
    , ALL ( Regions[Country] )
)
RETURN
IF(
    __r = __N
    , "All Countries Selected"
    , CONCATENATEX ( 
        __f
        , Regions[Country]
        , ", " 
        , Regions[Country]
        , ASC
    )
)
filtered country subtitle = 
VAR __f = FILTERS ( Regions[Country] ) 
VAR __r = COUNTROWS ( __f ) 
VAR __N = CALCULATE(DISTINCTCOUNT(Regions[Country]),ALL(Regions[Country]))
RETURN
IF(
    __r = __N
    , "All Countries"
    , IF( __r = 1, __f, "Multiple Countries")
)
1 Like

@tweinzapfel thanks for the workout, here is my submission.

Selected Region(s) DAX

Summary

Workout009_SelectedRegionSoln

Subtitle DAX

Summary

1 Like

Apr 18, 2023
Donn Clark

My Entry with the filter info as the subtitle also …
thanks for a fun workout.

Countries Selected =
IF(
ISFILTERED(Regions[Country]),
CONCATENATEX(
ALLSELECTED(
Regions[Country]
),
Regions[Country], ", ", Regions[Country], ASC
),
“All Countries are Selected”
)

Subtitle 1 =
IF(
ISFILTERED(Regions[Country]),
SELECTEDVALUE(Regions[Country],
“Multiple Countries”
),
“All Countries”
)

Subtitle 2 =
IF(
ISFILTERED(Regions[Country]),
SELECTEDVALUE(Regions[Country],
"Countries: " & " " & [Countries Selected]
),
“All Countries”
)

DEC

1 Like

Hi all

Here is my solution:

Selected Countries measure:

Selected Countries V1 = 
VAR SelectedCountries = DISTINCT(Regions[Country]) 
// Distinct list of all countries

VAR _Result = 
    IF(
        COUNTROWS(SelectedCountries) = COUNTROWS(ALL(Regions[Country])),
        "All Countries Selected",
        // If the number of rows n SelectedCountries is equal to all rows in Regions[Country] then All Countries Selected
        CONCATENATEX(SelectedCountries, Regions[Country], " | ", Regions[Country], ASC)
        // If not concatenate the names of the selected countries in a string and sort ascending based on their names
    )
RETURN _Result      

Subtitle measure:

Subtitle = 
IF(
    HASONEVALUE(Regions[Country]),
    // Checks if only one country is selected 
    VALUES(Regions[Country]),
    // If it is it returns the name of the country using VALUES
    IF(
        ISFILTERED(Regions[Country]), 
        "Multiple Countries", 
        "All Countries"
    ) 
    
)

A merged option which will actually show the Selected countries as a subtitle:

Merged = 
VAR SelectedCountries = DISTINCT(Regions[Country]) 
VAR _Result = 
    IF(
        COUNTROWS(SelectedCountries) = COUNTROWS(ALL(Regions[Country])),
        "All Countries Selected",
        CONCATENATEX(SelectedCountries, Regions[Country], " | ", Regions[Country], ASC)
    )
VAR _Subtitle = 
    IF(
        HASONEVALUE(Regions[Country]),
        VALUES(Regions[Country]),
        IF(
            ISFILTERED(Regions[Country]), 
            _Result, 
            "All Countries"
        ) 
    )
RETURN _Subtitle

This task looks like a technique that will come in handy very often, yet simple and straightforward.

Here’s my code:

Country filters applied
Country filters applied = 
CONCATENATEX(ALLSELECTED(Regions[Country]), 
                Regions[Country], ", ", 
                Regions[Country], ASC)

Then I used a second measure for the subtitle

Subtitle measure
Which country = 
VAR countcountries = COUNTROWS(ALLSELECTED(Regions[Country]))
RETURN
SWITCH(TRUE(),
    countcountries = 1, SELECTEDVALUE(Regions[Country]),
    countcountries = COUNTROWS(ALL(Regions[Country])), "All countries",
    countcountries < COUNTROWS(ALL(Regions[Country])), "Multiple countries"
    )

I agree with MubarakBabs, this technique will definitely help in future projects. Thanks for this workout. Here is my submission
image

DAX Measures

This text will be hidden

Country Selected =
IF(
    ISFILTERED( Regions[Country] ),
    CONCATENATEX(
        ALLSELECTED( Regions[Country] ),
        Regions[Country],
        " , ", Regions[Country],
        ASC
    ),
    "All Countries are selected"
)

Subtitle =
SWITCH(
    TRUE(),
    COUNTROWS( ALLSELECTED( Regions[Country] ) ) = 1,"Single Country", 
    COUNTROWS( ALL( Regions[Country] ) ) =COUNTROWS( ALLSELECTED( Regions[Country] ) ),
    "All Countries", "Multiple Countries")
)

Hi ,

I know I am late but still posting my entry for the workout :
image

DAX CODE
Selection = 
VAR _totcount= CALCULATE(DISTINCTCOUNT(Regions[Country]),ALL(Regions[Country]))
VAR _selcountry= VALUES(Regions[Country])
return
if (_totcount=COUNTROWS(_selcountry),"All countries is selected",
CONCATENATEX(_selcountry,[Country],",",[Country],ASC))
Subtitle = 
VAR _totcount= CALCULATE(DISTINCTCOUNT(Regions[Country]),ALL(Regions[Country]))
VAR _selcountry= SELECTEDVALUE(Regions[Country],
                                    if (DISTINCTCOUNT(Regions[Country])=_totcount,"All countries "
                                                                   ,"Multiple Countries" ))
return
_selcountry

Workout 009_Posting.pbix (1.4 MB)

Thanks

answer:
solution:


DAX Workout No9 MB.pbix (1.4 MB)