Forum Discussion
Need Help
- 1 year ago
Hi Yacine2025 ,
To create a slicer in Power BI that allows the user to choose between "Last Month," "2 Last months," "3 Last months," and "Custom," start by creating a disconnected table called Period Selector with one column called PeriodOption, using this DAX:
Period Selector = DATATABLE( "PeriodOption", STRING, { {"Last Month"}, {"2 Last months"}, {"3 Last months"}, {"Custom"} } )Add this table to a slicer visual in your report and set it to horizontal orientation to mimic button selection. Then create a measure to capture the selected value from the slicer:
Selected Period Option = SELECTEDVALUE('Period Selector'[PeriodOption])Assuming you already have base measures created for each of the period comparisons, such as [Period_LastMonth], [Period_2Months], [Period_3Months], and [Period_Custom], create a unified measure to switch between them dynamically depending on the slicer selection:
Period Dynamic = VAR SelectedOption = SELECTEDVALUE('Period Selector'[PeriodOption]) RETURN SWITCH( TRUE(), SelectedOption = "Last Month", [Period_LastMonth], SelectedOption = "2 Last months", [Period_2Months], SelectedOption = "3 Last months", [Period_3Months], SelectedOption = "Custom", [Period_Custom], BLANK() )Repeat the same structure for Last Period:
Last Period Dynamic = VAR SelectedOption = SELECTEDVALUE('Period Selector'[PeriodOption]) RETURN SWITCH( TRUE(), SelectedOption = "Last Month", [LastPeriod_LastMonth], SelectedOption = "2 Last months", [LastPeriod_2Months], SelectedOption = "3 Last months", [LastPeriod_3Months], SelectedOption = "Custom", [LastPeriod_Custom], BLANK() )Finally, calculate the GAP as the difference between these two dynamic measures:
GAP Dynamic = [Last Period Dynamic] - [Period Dynamic]Ensure that the custom period measures like [Period_Custom] and [LastPeriod_Custom] are written using the selected range from the Date slicer. You can do this with DATESBETWEEN using the MIN and MAX of the selected date from your Date table. For example:
Period_Custom = CALCULATE( SUM('YourTable'[Value]), DATESBETWEEN('DateTable'[Date], MIN('DateTable'[Date]), MAX('DateTable'[Date])) )This setup gives you a dynamic, user-controlled matrix that updates according to button-style slicer input or custom date selection.
Best regards,
Hi Yacine2025
Could you please confirm if your query have been resolved the solution provided by burakkaragoz and DataNinja777 ? If they have, kindly mark the helpful response and accept it as the solution. This will assist other community members in resolving similar issues more efficiently.
Thank you.