Forum Discussion

Sanskruti's avatar
Sanskruti
Helper II
2 years ago

Custom year slicer

Hello,
I have a year slicer in which there are 4 years.

For this current year i have used dax which is:

YearSlicer = if([Year]=year(today()),"Current Year",CONVERT([Year],STRING)).

But the requirement is instead of current year it should dispaly "2023" and by default the current year should be selected. 
And only last 4 years should be visible. For example: 
If the current year is 2024 the years visible should be:
2024, 2023, 2022 and 2021. 

Thank you in advance.

14 Replies

  • Hi Sanskruti 

    You can try the Preselected Slicer custom visual, assuming you are happy with the visual's appearance.

    (Similar functionality should be coming to the core slicer visual at some stage but not yet.)

     

    The sample workbook provided by the developer for this particular visual contains instructions, but I have attached a small example similar to what you have described, using a 'Date' table.

     

    Components of this example:

    1. A measure Current Year Flag, used in the Pre Selection field of the visual:

    Current Year Flag = 
    MAX ( 'Date'[Year] ) = YEAR ( TODAY () )

    2. A measure Last 4 Years Flag used as a visual-level filter:

    Last 4 Years Flag = 
    VAR MaxYear = MAX ( 'Date'[Year] )
    VAR CurrentYear =
        YEAR ( TODAY () )
    RETURN
        INT (
            AND (
                MaxYear <= CurrentYear,
                MaxYear >= CurrentYear - 3
            )
        )

    3. Once the visual is set up it looks like this:

     

    When the report page is opened, the current year will be selected (as long as it exists in the table).

     

    Does this work for you?

     

    Regards

     

      • OwenAuger's avatar
        OwenAuger
        Super User

        Understood 🙂
        Would you be willing to use the "Slicer (new)" visual?

        It would require the items to appear as buttons.

         

        If you have a "Year Offset" column, you could place a "Year Offset" field on the slicer, filtered to zero (i.e. current year)

        Then switch off Values (-3, -2, -1, 0) and instead show a Label defined by a measure returning the year (2020, 2021, 2022, 2023).

         

        Example attached.

         

  • AliceW's avatar
    AliceW
    Power Participant

    Why don't you just select the current year in the default published report? And set a hidden page filter with the last three years plus the current one? 

    And just change this setting once per year, on January 1st?

    • Sanskruti's avatar
      Sanskruti
      Helper II

      thanks for replying AliceW ,
      We tried this method but this will not be a good practice.

  • Yes, as long as the dataset is refreshed so that Year Offset column = 0 for the current year.

     

    The slicer selection is actually Year Offset = 0. It just displays the appropriate year as a label in place of 0, and the label will update to whichever year corresponds Year Offset =0.

    • Sanskruti's avatar
      Sanskruti
      Helper II

      okay. And is it possible if we do this in dropdown?
      And in the year offset column we have defined the end date. Will it dynamically change if the year changes? 

      • OwenAuger's avatar
        OwenAuger
        Super User

        Right now, dropdown is unfortunately not possible with this new Slicer visual. And the original Slicer has no option for displaying labels and hiding values 😞

         

        Yes, the "Year Offset" column will dynamically change on refresh as long as it is defined in a calculated column or table relative to TODAY() (using DAX) or similarly in Power Query.

         

        My example version was defined in a calculated table using the TODAY() function to determine the current year.