Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
racope
Frequent Visitor

Defaulting date range with date slicer

Is there a way in DAX to specify the start and ending date used in a date range slicer? I need the default start date to be the first day of the month from 2 years ago and the default end date to be the last day of the previous month. So when opening the report today the start date should be 11/1/2019 and the ending date should be 10/31/2021. Both dates should automatically update to the next month as a new month starts.  Is this possible?  

1 ACCEPTED SOLUTION
v-cazheng-msft
Community Support
Community Support

Hi @racope 

 

You may try this Measure.

IsOrNot =

VAR Today_ =

    TODAY ()

VAR Year_ =

    YEAR ( Today_ )

VAR Month_ =

    MONTH ( Today_ )

VAR StartDate =

    DATE ( Year_ - 2, Month_, 1 )

VAR EndDate =

    DATE ( Year_, Month_, 1 ) - 1

RETURN

    COUNTX (

        FILTER (

            'calendar',

            'calendar'[Date] >= StartDate

                && 'calendar'[Date] <= EndDate

        ),

        'calendar'[Date]

    )

 

And then, use this Measure to filter your date Slicer.

vcazhengmsft_0-1638515701621.png

 

 

Then, the result should look like this:

vcazhengmsft_1-1638515701623.png

 

For more details, please refer to the attached pbix file.

 

Best Regards,

Community Support Team _ Caiyun

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly. If you still have problems on it or I misunderstand your needs, please feel free to let us know. Thanks a lot!

View solution in original post

3 REPLIES 3
v-cazheng-msft
Community Support
Community Support

Hi @racope 

 

You may try this Measure.

IsOrNot =

VAR Today_ =

    TODAY ()

VAR Year_ =

    YEAR ( Today_ )

VAR Month_ =

    MONTH ( Today_ )

VAR StartDate =

    DATE ( Year_ - 2, Month_, 1 )

VAR EndDate =

    DATE ( Year_, Month_, 1 ) - 1

RETURN

    COUNTX (

        FILTER (

            'calendar',

            'calendar'[Date] >= StartDate

                && 'calendar'[Date] <= EndDate

        ),

        'calendar'[Date]

    )

 

And then, use this Measure to filter your date Slicer.

vcazhengmsft_0-1638515701621.png

 

 

Then, the result should look like this:

vcazhengmsft_1-1638515701623.png

 

For more details, please refer to the attached pbix file.

 

Best Regards,

Community Support Team _ Caiyun

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly. If you still have problems on it or I misunderstand your needs, please feel free to let us know. Thanks a lot!

rbriga
Impactful Individual
Impactful Individual

I've tested this solution, but it misses out on the desired effect.Date Filter.JPG

  1. Create a measure that will help us filter the days:

Available Dates =
VAR _LastDate =
    DATE ( YEAR ( TODAY () ), MONTH ( TODAY () ), 1 ) - 1
VAR _FirstDate =
    DATE ( YEAR ( TODAY () ) - 2, 1, 1 )
RETURN
    COUNTROWS (
        FILTER (
            'Calendar',
            'Calendar'[Date] >= _FirstDate
                && 'Calendar'[Date] <= _LastDate
        )
    )

 

2. Select the date slicer and open the filter pane.

Under "Filters on this visual", drag our new Available Dates measure.

Select "is not blank", then apply. I suggest that you hide and lock this filter.

 

This limits the slicer the way you wanted, but until you move the sliders\ values from their start\end (default) positions, PowerBI assumes you haven't filtered anything, so it shows dates outside that range until you move the slider.

 

However, the answer may lie in the date table itself.

1. Use the query editor to add a column which flags (Yes\ No) the dates in the range you've defined. Let's call it Available Date.

2. In the filter pane, add a page\report-wide filter: "Available Date is Yes".

-------------------------
Data analyst by day, hockey goalie by night.
Did I help? Then please hit that "kudos" or "accept as a solution" button!
amitchandak
Super User
Super User

@racope , no there is no option. You can log an idea for that https://ideas.powerbi.com/ideas/

 

There is an work around

Default Date Today/ This Month / This Year: https://www.youtube.com/watch?v=hfn05preQYA

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors