Forum Discussion
Custom Predefined Date ranges
- Anonymous9 years ago
Hi v-huizhn-msft,
Thank you for the suggestion. Unfortunately it didn't work for me in my scenario as it only seemed to work with a single measure and in a model that only has data for the current year. I needed it to work across all measures with data more than 1 year. Although your solution didn't work, I was able to find one that worked with minimal modification to the model and zero dax code on my end. I have to give credit to Chris Webb as I stumbled across his blog post on the topic HERE.
Given that I had a fact table with measures and a date table with dates, I only had to add one more table that I called Date Range that would handle all of the magic.
Here is a look at my model (Only the Date Range table was added):
The Date range table has 3 columns:
- Period - A name column such as (Year to Date, Last Month, etc) that is used as the field on the slicer.
- Date - Contains date values that represent all dates with the specified "Period" column. For example, for the Period Named "Year to Date" currently contains all dates from the 1st of the year to yesterday. Last Month contains all date values for last month. This is important to note as when i drag this field to the Date table the relationship should be Many-to-One (for me it chose one-to-one by default for some reason).
- Sort. This is an arbitray column that allows me to sort the periods in a meaninful way withinin the slicer. (1 for year to date, 2 for last month, etc)
Here is a look at the Date Range table. You can see the duplicate date values for "This Month" and "This Week", hence the many to one relationship with the Date table.
Then, there is just one final piece in order to get this to work. The glorious bi-directional cross filter. In the relationship between date and the Date Range tables, simply change the Cross filter direction from "single" to "Both".
Now, you can enjoy your custom date range slicer inside of your reports:
Can't think of way to do this using single slicer since dates will overlap between each range. Basically you'll need column for each range and toggle "Yes"/"No". Each column needing it's own slicer. But this isn't clean way of doing things.
Instead, why not use Timeline slicer. It can easily toggle between Week, Month, Year.
If your date dimension table contains future dates (ex. dates to end of year), you may need to create another table that shrinks down date range from start of year to current date.