Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
9 years ago
Solved

Custom Predefined Date ranges

I'm trying to create 4 custom date ranges that I can then use as a slicer in my reports.  The slicer should have the following options:   Year to Date Last Month  This Month This Week Dependi...
  • Anonymous's avatar
    Anonymous
    9 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:

    1. Period - A name column such as (Year to Date, Last Month, etc) that is used as the field on the slicer.
    2. 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).
    3. 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: