Power BI is turning 10! Tune in for a special live episode on July 24 with behind-the-scenes stories, product evolution highlights, and a sneak peek at what’s in store for the future.
Save the dateEnhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.
Hi everyone,
I need help to achieve my request please
I have a visual matrix in Power BI that contain Client and coutry in row and number for period and last period and gap between thses periods. I want to create 1 slicer with 4 choice (3 months, 2 months, month and custom). I use measure for every choice. I need when I clic on button 1, 2 or 3 months, it select the right measure and if I select custom then the user select date in the filter Date slicer. Thank you in advance.
Example below
Solved! Go to Solution.
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
We are following up to see if you have had the chance to review the information provided. If you have any further questions, please do not hesitate to contact us. Could you confirm whether your query has been resolved? If so, kindly mark the helpful response and accept it as the solution to assist other community members in resolving similar issues more efficiently. If not, please provide detailed information so we can better assist you.
Thank You.
Hi @Yacine2025
I wanted to check if you had the opportunity to review the information provided. Please feel free to contact us if you have any further questions. If my response has addressed your query, please accept it as a solution and give a 'Kudos' so other members can easily find it.
Thank you.
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.
Hi @Yacine2025 ,
The solution above is pretty solid for dynamic period selection, but just to make things easier, here’s a ready-to-use DAX example that calculates days since each customer’s last purchase. You can drop this into your model and adjust table/column names as needed:
Days Since Last Purchase = VAR LastPurchaseDate = CALCULATE( MAX('Sales'[OrderDate]), ALLEXCEPT('Sales', 'Sales'[Customer]) ) RETURN DATEDIFF(LastPurchaseDate, TODAY(), DAY)
You can put this measure into a table visual with customers to see the exact number of days since each last transaction.
If you want to expand it for dynamic date periods (like “Last Month”, “Last 3 Months”, etc), you can use a disconnected table for the period selection and a SWITCH statement in DAX, something like this:
SelectedPeriod = SWITCH( SELECTEDVALUE('PeriodSelect'[Period]), "Last Month", DATESINPERIOD('Date'[Date], TODAY(), -1, MONTH), "Last 3 Months", DATESINPERIOD('Date'[Date], TODAY(), -3, MONTH), "Last 6 Months", DATESINPERIOD('Date'[Date], TODAY(), -6, MONTH), "Custom", // add your own logic for custom periods BLANK() )
If you want, you can mix both approaches—showing days since last purchase, but filter it by the period the user selects.
If you run into any issues, just let me know what your current model looks like or share your DAX, and I can help tweak it for you.
If my response resolved your query, kindly mark it as the Accepted Solution to assist others. Additionally, I would be grateful for a 'Kudos' if you found my response helpful.
translation and formatting supported by AI
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,
Check out the July 2025 Power BI update to learn about new features.
This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.
User | Count |
---|---|
63 | |
62 | |
52 | |
39 | |
24 |
User | Count |
---|---|
84 | |
57 | |
45 | |
42 | |
37 |