Forum Discussion

Yacine2025's avatar
Yacine2025
Frequent Visitor
1 year ago
Solved

Need Help

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 per...
  • DataNinja777's avatar
    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,