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.
https://www.youtube.com/watch?v=5XNcGL0g-sE&t=2s&ab_channel=NickPowerBi
maybe check out this video that shows step-by-step instructions on how to make a date filter to select Last 90 days, last week, current month, or select any specific date ranges.