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 selected, then it's handled as if all values are selected. I've gotten around this by checking if the field parameter is filtered, and if untrue, then values for the line chart isn't calculated.
However, this doesn't apply to the legend.

The image below shows that no values have been selected for the right y-axis, and the chart does indeed not show anything, but the legend shows both values, as if they are selected.


I've been trying to figure out how to make the legend dynamic in the same way, but am stuck.

Any suggestions?

  • 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?

4 Replies

  • Uzi2019's avatar
    Uzi2019
    Community Champion

    HI MadBern85 
    Once suggestion make your selection on right side Y axis as a single select.

     

    If nothing is selected it treated as both selected like Select All functionality. so as you can see you have selected nothing but still showing Service Rate and SLA on legends. (unit on y axis is combination of both value).

     

    Solution One: Make silcer single select so by default any one value will be displayed.( Recommended)

    Solution two: At the time of pulishing make sure you select one value and publish it. so the users will able to see selected value in legends.

     

    I hope I answered your question!

     

     

     

     

    • MadBern85's avatar
      MadBern85
      Helper I

      Thanks for the suggestions.
      Making it single select isn't an option, as users have to be able to view all values against all other values (if needed).
      And option two only "works" until the users deselects all values in one of the slicers.

      I know it's possible to get a dynamic legend, like I describe, for a visual/graph with only one y-axis. You only have to use a measure with INT(ISFILTERED(Table[Column])) as a visual filter and set it to = 1, but this fails when applying two axes.


      It might be that what I want simply has no good solution ...

      • OwenAuger's avatar
        OwenAuger
        Super User

        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?