Forum Discussion

MadBern85's avatar
MadBern85
Helper I
1 year ago
Solved

Line chart, two slicers - Hide legend values when none selected

Hi I have a line chart with two y-axes. Each axis is controlled by a slicer, and each slicer has it's own Field Parameter. By default (it seems), slicers function such that if no values are select...
  • OwenAuger's avatar
    OwenAuger
    1 year ago

    Hi MadBern85 

    One solution I can suggest is:

    1. Add a "No selection" option to each field parameter (a dummy item corresponding to no valid field).
    2. Create a measure that is used to filter both field parameters at the visual level via an additional table, such that when no selection is made on a particular field parameter, the "No selection" option is forced.

    I have attached a sample PBIX using a Contoso model.

     

    I will say that this might feels like overkill to achieve a seemingly simple outcome, but I don't think there's any other way to do this, at least until slicers allow for "no selection" as distinct from "all selected". (Perhaps a custom slicer visual could be created to do this.)

     

    Detailed steps:

    1. For each of the field parameter tables:

    (a) Add a "No selection" option which contains an invalid (but not blank) field reference. I used the value "Dummy".

    (b) Add a "Type" column which is equal to either "No selection" for the "No selection" row, otherwise "Selection".

    Here is the DAX code and table view for one of my field parameter tables Parameter 1:

    Parameter 1 = 
    ADDCOLUMNS (
        {
            ( "No selection", "Dummy", -1 ), -- No selection
            ( "Sales Amount", NAMEOF ( [Sales Amount] ), 0 ),
            ( "Total Cost", NAMEOF ( [Total Cost] ), 1 ),
            ( "Margin", NAMEOF ( [Margin] ), 2 )
        },
        "Parameter 1 Type", IF ( [Value1] = "No selection", "No selection", "Selection" )
    )

     

    Parameter 2 is set up similarly.

     

    2. Create single-column tables Parameter 1 Type and Parameter 2 Type:

    Parameter 1 Type = 
    ALLNOBLANKROW ( 'Parameter 1'[Parameter 1 Type] )

    These are related one-to-many with the corresponding columns of Parameter 1 / Parameter 2.

     

    3. Create Parameter Types which is a crossjoin of Parameter 1 Type and Parameter 2 Type with a key column added. This is related many-to-one (bidirectional) with each of Parameter 1 Type and Parameter 2 Type.

    Parameter Types = 
    ADDCOLUMNS (
        CROSSJOIN (
            ALLNOBLANKROW ( 'Parameter 1'[Parameter 1 Type] ),
            ALLNOBLANKROW ( 'Parameter 2'[Parameter 2 Type] )
        ),
        "Key",
            INT (
                ( 'Parameter 1'[Parameter 1 Type] = "Selection" )
                    + ( 'Parameter 2'[Parameter 2 Type] = "Selection" ) * 2
            )
    )

    4. The relationship diagram then looks like below. The idea is that Parameter Types will be filtered via a measure at the line chart visual level, which in turn will filter each of the field parameter tables appropriately.

    We could have built this without the intermediate tables Parameter 1 Type & Parameter 2 Type, but this would require many-to-many relationships which I prefer to avoid.

    5. Create slicers for Parameter 1 and Parameter 2, but hide "No selection" on each using visual level filters.

    6. Set up the line chart with Parameter 1 on Y-axis and Parameter 2 on Secondary Y-axis:

    7. Create a measure Parameter Type Flag. This will be used to filter 'Parameter Types'[Key]:

    Parameter Type Flag = 
    VAR Parameter1_Type =
        IF ( ISFILTERED ( 'Parameter 1' ), "Selection", "No Selection" )
    VAR Parameter2_Type =
        IF ( ISFILTERED ( 'Parameter 2' ), "Selection", "No Selection" )
    VAR Current_Parameter1_Type =
        SELECTEDVALUE ( 'Parameter Types'[Parameter 1 Type] )
    VAR Current_Parameter2_Type =
        SELECTEDVALUE ( 'Parameter Types'[Parameter 2 Type] )
    VAR FlagValue =
        INT ( AND ( Parameter1_Type = Current_Parameter1_Type, Parameter2_Type = Current_Parameter2_Type ) )
    RETURN
        FlagValue

    8. Add a "Top N" visual level filter to the line chart as shown:

    9. Now the line chart includes only the measures explicitly selected on the slicers. If no selections are made on a slicer, no measures are included on that axis.

     

    You could also do something similar with a hidden Preselected Slicer custom visual controlling the filtering rather than the Top N visual filter.

     

    Is this something you can adapt to your model?