Set Default Slicer Value to Current Month in Power BI

You have a report and want to show a default slicer value that moves with time. When you arrive at the next month, you want the slicer to move to the next month too. How is that possible? Reports often have slicers that filter a report to show the desired period. In financial reports you often want to see the last month that completely finished. Let’s say you want to see the month September. You select september as slicer value and publish your Power BI report. Then time passes and you arrive in the next month. The slicer still shows the month September. Yet you now want to see the next month, October. Ideally you would always see the last completely finished month, without making any manual changes to the report. So how can you achieve that?

There are two ways you can look into.

1. Relative Date Filters

The first approach makes use of the built in filters types in Power BI. In any report when you open the report canvas, you can find the filter pane on the right hand. This filter pane allows you to apply filters to your report. You can choose to filter all pages, this page or this visual. When you then move a date field into any of these filters, you can make use of the relative date and relative time filter types.

1.1. Configure Filters

You can do that by:

  1. Move a date field into one of the filters (all pages, page or visual filter)
  2. Select the Filter type drop-down -> select Relative date.
Relative date filter

After selecting relative date you can choose to show items in different timeframes.

Filter last calendar month

Any of the above mentioned options looks at the current date and from there shifts the period . When you select input is in the last 1 calendar months, you achieve your goal.

1.2. Disadvantages

This approach has several downsides compared to using a regular slicer panel:

  1. you are limited to the options provided by the relative date filters. When you have more specific wishes to filter a period, you will have to look for alternatives.
  2. it is inconvenient to selectively apply filters. You either have to apply the filters to all pages, configure it for each page or for each visual. To apply the filters to all pages except one would involve opening each page and to manually configure the filters. Imagine doing this for 20 pages. Yet, creating a filter pane with slicers allows you to easily sync your filters across multiple pages.
  3. the filter pane is a hidden and arduous way to adjust periods. Imagine your slicer has a default value, but you want to change to a different month. You would have to open the filter pane, remove the date filter, find a filter for the relevant month and apply it. And reverse these steps to get back to the default view.
  4. it looks unattractive when the filter pane contains many filters. And there are no visual options to adjust the layout of the filter pane. A more attractive option is to have slicers available in a slicer pane. Where you can decide on the right format of the slicer. You may choose a timeline, dropdown, search bar and can select a vertical or horizontal orientation.
  5. you are unable to apply a measure filter on the values in the filter panel. Yet pane with slicers allows you to pre-filter using a measure value.

As you can imagine, my preferred method is to use the regular slicers in Power BI. Which is what comes next.

2. Set Default Slicer Value

A more flexible way to set a default slicer value in Power BI is by using slicers. Any slicer value that you save in a slicer, remains the same. Putting in a month like September will therefore always remain the same. Yet as you have seen in approach 1, inputting something dynamic like last x months makes the naming dynamic. The challenge is, how can you create that for a regular slicer? The answer is by adjusting the calendar in Power Query. How? Buckle your seatbelts, we’ll dive right in.

We will use below calendar for the example. The calendar only shows the first 2 days of each months to make it fit in a single picture.

Calendar table

2.1. Create Adjusted Month Column

Your requirement is to have a month slicer with a default value that moves with the months. And you want this month to be the last completely finished month. You will achieve this by adding a column named Month Name Default. This column contains the regular Month Names, the only difference being that the last completely finished month will be replaced by the value Default. With each passing of the months, this value moves along.

You will need to create logic to check each month in your calendar against the latest completed month. To get there I strongly recommend you create a separate column. You may still need the regular Month Name column for other purposes in your report. For example to display which month name is displayed. You can then reference the original column, instead of the adjusted one.

So what steps do you go through to get there?

You first retrieve the current date on your system. Power Query has a function to retrieve the current datetime value on your system, which is DateTime.LocalNow. First add the current datetime to a calculate column by writing:

= DateTime.LocalNow()

This returns a DateTime value. For date comparisons, you want this value to be in a date format. Adjust the previous formula by using either the Date.From function or the DateTime.Date function:

= Date.From( 
     DateTime.LocalNow() 
  )
= DateTime.Date( 
     DateTime.LocalNow() 
  )

The result of this is a date value with the date on your current system.

The next step is to find the previous month. You can do that by retrieving the current month number and subtract 1 from it. The function Date.Month returns the month number of a date. Then after retrieving this value you subtract 1 from it.

= Date.Month(
     DateTime.Date( 
          DateTime.LocalNow() 
     ) 
  )
 - 1

With these steps you arrive at the month you want to show. Even if you current date is the 30st of September, the database probably doesn’t contain all data of that day yet. In many companies the database has a day delay. So your safest bet may be to always find the previous month.

This results in below numbers.

Add Month Name Default column

You now find a regular Month column (1) in your calendar and there is a Custom column (2) with the last completely finished month. You now have all ingredients to duplicate the Month Name column, and replace the last completely finished month by the word default.

= if [Month] = 
           Date.Month( DateTime.Date(                      
           DateTime.LocalNow() ) ) - 1 then
     "Default" else
     [Month Name]

Name your column Month Name Default, and the following is the result:

Default Slicer Value column

2.2. Advantages Default Slicer Value

Your newly created column contains values identical to the Month Name column, except that the current month now has the description Default. It is good to notice a few things about this solution:

  1. The month September is replaced by the word default, not only for the current year but also for all previous years. This is intended. If you only replace the month name of the current year this could be problematic.
    Imagine using a slicer with year number. During your analysis you want to select the previous year. Unfortunately, with Default month selected, the previous year is not available in the year slicer. After all, the value Default only exists in the current year in your calendar. You would have to take off the Month filter, change the year, and then filter on the month August. And to get back to the current year, do the steps in reverse order.
  2. The used solution can be as flexible as you need it to be. If you want to select a period of 3 months ago, that is possible. Also if you want to select multiple months, you can write code for that too.

2.3. Configure Slicers

With the calendar prepared with your newly created column, we can now incorporate it in the model. Before you add your new column to a slicer, you can go to the Data View, select the column Month Name Default and sort the column by the Month. This ensures the values have the order of the corresponding month number. Your Default value is then always sorted in the position of the month it represents.

Sort by column for your default slicer value

When you add Month Name Default to the slicer the dropdown looks as follows:

Slicer containing the default month

The slicer no longer shows the name of the last completely finished month. However, for users this is clear because the sort orders implies what the selected month is. You have now configured your default slicer value to the desired month.

2.4. Create Selected Period Header

If you need more clarity in your report about the month selection, you can add a title to your report showing the selected period. You can base this title on the regular Month Name column, which still contains the month name September instead of default. Something like:

= "Period: " & 
     & SELECTEDVALUE( Calendar[Month Name] ) 
     & " " 
     & SELECTEDVALUE( Calendar[Year] )

Allowing your report to have both a dynamic slicer selection and a reference to the selected period:

2.5. Create Adjusted Year Column

Last but not least, you can apply a similar technique on the year number. If you want to show the last completely finished month, you also want to show the year of that month. Just make sure that in this new column the year number is formatted as text. After all, the column is of text type because of the Default value. You can do that by adjusting the code slightly. Instead of retrieving the previous month number, you can retrieve the previous month date first. And then transform that date into a year, using the following code:

= if [Year] = 
      Date.Year( 
           Date.AddMonths( 
                DateTime.Date( DateTime.LocalNow() ), 
               -1 )
      )  
     then "Default" 
     else Text.From( [Year] )

And that’s how you can set a default slicer value to the current month or current year in your power BI report!

About Rick de Groot

Rick is the founder and editor of BI Gorilla. He believes learning is one of the great pleasures in life and wants to share his knowledge to help you improve your skills.

Learn more about him here, connect with him on Twitter, Facebook and LinkedIn and subscribe to his YouTube Channel.

1 thought on “Set Default Slicer Value to Current Month in Power BI”

Leave a comment