Forum Discussion

VannurVali's avatar
VannurVali
Icon for Helper I rankHelper I
1 year ago
Solved

Dynamic Column Names and Measures in Power BI Matrix Based on Selected Year and Month

Hi Everyone, I am trying to create a Power BI matrix visual to display sales measure for the Current Month Sales (CM), Previous Month Sales (CM-1), Last 2 Months Sales (CM-2), and Last 3 Months Sale...
  • Anonymous's avatar
    Anonymous
    1 year ago

    Hi VannurVali ,

     

    I think you can add an unrelated year and month selection table to help calculation.

    My Sample is as below.

    Calendar = 
    ADDCOLUMNS (
        CALENDARAUTO (),
        "Year", YEAR ( [Date] ),
        "Month", FORMAT ( [Date], "MMM" ),
        "MonthSort", MONTH ( [Date] ),
        "YearMonth", FORMAT ( [Date], "MMM-YY" ),
        "YearMonthSort",
            YEAR ( [Date] ) * 100
                + MONTH ( [Date] )
    )
    Selection = SUMMARIZE('Calendar','Calendar'[Year],'Calendar'[Month],'Calendar'[MonthSort])

    Measure:

    Measure = 
    VAR _SELECTYEAR = SELECTEDVALUE(Selection[Year])
    VAR _SELECTMONTH = SELECTEDVALUE(Selection[MonthSort])
    VAR _RANGEEND = EOMONTH(DATE(_SELECTYEAR,_SELECTMONTH,1),0)
    VAR _RANGESTART = EOMONTH(_RANGEEND,-3)+1
    RETURN
    CALCULATE(SUM('Table'[Sales]),FILTER('Calendar','Calendar'[Date]>=_RANGESTART && 'Calendar'[Date]<=_RANGEEND))

    Result is as below.

    Year = 2025 Month = Jan

    Year = 2024 Month = Dec

     

    Best Regards,
    Rico Zhou

     

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.