Forum Discussion
Power BI - Custom Date Range Filter Support
Hi ShaneHi
To create a single slicer that can handle custom date ranges like "Last 7 Days," "Last 12 Weeks," "Last 12 Months," or "Custom Date Range," you can approach it by building a dynamic table with distinct ranges. Here’s how you can combine the two approaches:
1. Create a Date Range Table:
Build a separate table that defines each time period you want in the slicer (e.g., "Last 7 Days," "Last 12 Weeks," etc.).
Date Ranges =
DATATABLE(
"Range", STRING,
"SortOrder", INTEGER,
{
{"Last 7 Days", 1},
{"Last 10 Weeks", 2},
{"Last 12 Months", 3},
{"Custom", 4}
}
)
2. Modify your Date Filter Logic:
You can then use a 'SWITCH' statement or 'IF' conditions to determine which period should be applied based on the selected slicer value.
FilteredDates =
SWITCH(
SELECTEDVALUE('Date Ranges'[Range]),
"Last 7 Days", DATESINPERIOD('Calendar'[Date], MAX('Calendar'[Date]), -7, DAY),
"Last 10 Weeks", DATESINPERIOD('Calendar'[W/C Monday], MAX('Calendar'[W/C Monday]), -10, WEEK),
"Last 12 Months", DATESINPERIOD('Calendar'[Date], MAX('Calendar'[Date]), -12, MONTH),
"Custom", CALENDAR(MIN('Calendar'[Date]), MAX('Calendar'[Date])) -- Assuming custom range will be user-driven via other slicers
)
3. Create a Measure for Filtering:
Create a measure to apply the selected date range to your table visuals.
FilteredMeasure =
CALCULATE(
[Your Measure Here],
FilteredDates
)
4. Link Slicer to Date Table:
Use the 'Date Ranges' table you created as a slicer, and link it to the filtering logic above.
5. Custom Date Range Slicer:
For the "Custom Date Range," you can create a Date range slicer for the start and end date using the regular date slicer on the 'Calendar'[Date] column. You can then handle the logic in the same `SWITCH` case for "Custom"
Best Regards,
Jayleny
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Hey,
Thanks for the response! A Couple of follow up questions:
In Step 2 - "WEEK" isn't an option in DATESINPERIOD?
In Step 3 - What am I actually creating here?
I appreciate the response but I feel it's missing a couple of steps, and dare I say it's a bit like Chat GPT wrote it 😬
- Anonymous1 year agoNot applicable
Hi ShaneHi
You are right, I made a mistake here. Thank you for pointing it out. DATESINPERIOD only supports days, months, quarters, and years. Here is the changed DAX:"Last 10 Weeks", DATESINPERIOD('Calendar'[W/C Monday], MAX('Calendar'[W/C Monday]), -10*7,Day)
Since I don't know your specific data structure and don't have any sample data, I can only provide you with a potentially effective method.
If you can provide sample data that does not contain any private information, it will greatly help in solving your problem.Best Regards,
Jayleny
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
- ShaneHi1 year agoFrequent Visitor
Thanks for the response. I appreciate this is difficult without a desktop file to sample.
1) I created a table with this code
2) I created a measure with this code
3) I don't know how to create a measure with this code. What is "Your Measure Here" referring to?
I just keep hitting a wall with this code. My plan is to have a matrix table show the last 7 days of data (7 columns of previous days), 10 weeks of data (10 columns of the previous weeks) or 12 months of data (12 columns of previous months) depending what get's selected
- ShaneHi1 year agoFrequent Visitor
Hi,
I'm still stuck with this and wondering if you can help some more.
As I asked in my last response, in step 2 - was I right to create a DAX Measure with this code?
And in Step 3 - I'm assuming "YOUR MEASURE" refers to what i'm trying to output? The problem is - i'm hoping to have the slicer as a Date Column in a Matrix which contains multiple measures