Forum Discussion

honeybee84's avatar
honeybee84
Frequent Visitor
1 year ago
Solved

Need help with DAX code for cumulative values

Hi 

 

I have fact table with [reporting period] and [month]. I have a date table which has an active relationship to [reporting period] and inactive relationship to [month]. 

 

My reporting period has Dec-24, Jan-25, Feb-25, Mar-25, etc.

 

My goal is to have measure to show value as [Prior Month Forecast]. If I filter by period, let's say Apr-25, it will filter by reporting period Mar-25 (M-1) and it will give the forecast values each month as CUMULATIVE. 

 

So far all the codes I have tried give me MONTHLY values, as opposed to CUMULATIVE. Any idea how to fix this?

 

This is the code that shows incorrect monthly values as opposed to cumulative.

 

Cumulative Prior Month Forecast =
VAR SelectedMonthDate = MAX(DIM_Date[FullDateAlternateKey])
VAR PriorReportingPeriodDate = DATE(YEAR(SelectedMonthDate), MONTH(SelectedMonthDate) - 1, 1)  // First day of prior month

RETURN
CALCULATE(
    SUMX(
        FILTER(
            ALL(DIM_Date),
            DIM_Date[Year] = YEAR(PriorReportingPeriodDate) &&
            DIM_Date[Month] <= MONTH(SelectedMonthDate)  // Ensures cumulative roll-up
        ),
        [Forecast]  // Accumulates forecast values month by month
    ),
    TREATAS(
        {PriorReportingPeriodDate}, FACT_CombinedData[Reporting Period]  // Passes filter without needing an active relationship
    )
)

 

Any guidance is greatly appreciated.

Thank you. 

  • Anonymous's avatar
    Anonymous
    1 year ago

    Hi honeybee84 ,

     

    Thank you for reaching out to Microsoft Fabric Community Forum.

    johnt75 bhanu_gautam Thank you for your quick inputs.

     

    honeybee84 Could you please try the below DAX measure.:

    Cumulative Prior Month Forecast =
    VAR SelectedReportingPeriod = MAX(DIM_Date[Reporting Period])
    VAR PriorReportingPeriod =
    CALCULATE(
    MAX(DIM_Date[Reporting Period]),
    DATEADD(DIM_Date[Reporting Period], -1, MONTH)
    )
    RETURN
    CALCULATE(
    [Forecast],
    FILTER(
    ALL(DIM_Date),
    DIM_Date[Month] <= MAX(DIM_Date[Month]) 
    ),
    TREATAS({PriorReportingPeriod}, FACT_CombinedData[Reporting Period]),
    USERELATIONSHIP(DIM_Date[Month], FACT_CombinedData[Month]) 
    )

     

    If this post helps, then please consider Accepting as solution to help the other members find it more quickly, don't forget to give a "Kudos" – I’d truly appreciate it!

     

    Regards,

    B Manikanteswara Reddy

     

9 Replies

  • honeybee84 , Try using

    Cumulative Prior Month Forecast =
    VAR SelectedReportingPeriod = MAX(FACT_CombinedData[Reporting Period])
    VAR PriorReportingPeriod = EDATE(SelectedReportingPeriod, -1) // Get the prior reporting period

    RETURN
    CALCULATE(
    SUMX(
    FILTER(
    ALL(DIM_Date),
    DIM_Date[FullDateAlternateKey] <= SelectedReportingPeriod &&
    DIM_Date[FullDateAlternateKey] >= PriorReportingPeriod // Ensures cumulative roll-up from prior reporting period
    ),
    [Forecast] // Accumulates forecast values month by month
    ),
    TREATAS(
    {PriorReportingPeriod}, FACT_CombinedData[Reporting Period] // Passes filter without needing an active relationship
    )
    )

    • honeybee84's avatar
      honeybee84
      Frequent Visitor

      Hi bhanu_gautam 

      Unfortunately it's still giving monthly values. Thanks for replying though 🙂 

  • You can try

    Cumulative Prior Month Forecast =
    VAR SelectedMonthDate =
        MAX ( DIM_Date[FullDateAlternateKey] )
    VAR DatesToUse =
        CALCULATETABLE (
            DATESYTD ( DIM_Date[Year Month] ),
            TREATAS (
                { EOMONTH ( SelectedMonthDate, -2 ) + 1 },
                DIM_Date[FullDateAlternateKey]
            )
        )
    VAR Result =
        SUMX ( DatesToUse, [Forecast] )
    RETURN
        Result
    

    where DIM_Date[Year month] is the date column linked to your fact reporting period column.

    • honeybee84's avatar
      honeybee84
      Frequent Visitor

      Hi johnt75

      Unfortunately it's still giving monthly values and values are also including prior reporting months as opposed to 1 month prior only. Thanks for replying though 🙂 

      • johnt75's avatar
        johnt75
        Super User

        I'm not entirely sure what you're trying to get. Can you give a list of months along with the months which should be included in the calculation. e.g. March 2025 should include Jan 2025 and Feb 2025.

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi honeybee84 ,

     

    Thank you for reaching out to Microsoft Fabric Community Forum.

    johnt75 bhanu_gautam Thank you for your quick inputs.

     

    honeybee84 Could you please try the below DAX measure.:

    Cumulative Prior Month Forecast =
    VAR SelectedReportingPeriod = MAX(DIM_Date[Reporting Period])
    VAR PriorReportingPeriod =
    CALCULATE(
    MAX(DIM_Date[Reporting Period]),
    DATEADD(DIM_Date[Reporting Period], -1, MONTH)
    )
    RETURN
    CALCULATE(
    [Forecast],
    FILTER(
    ALL(DIM_Date),
    DIM_Date[Month] <= MAX(DIM_Date[Month]) 
    ),
    TREATAS({PriorReportingPeriod}, FACT_CombinedData[Reporting Period]),
    USERELATIONSHIP(DIM_Date[Month], FACT_CombinedData[Month]) 
    )

     

    If this post helps, then please consider Accepting as solution to help the other members find it more quickly, don't forget to give a "Kudos" – I’d truly appreciate it!

     

    Regards,

    B Manikanteswara Reddy

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi honeybee84 ,

       

      We wanted to kindly follow up to check if the solution provided for the issue worked? or Let us know if you need any further assistance?

       

      If our response addressed, please mark it as Accept as solution and click Yes if you found it helpful.

       

      Please don't forget to give a "Kudos " – I’d truly appreciate it!

       

      Regards,

      B Manikanteswara Reddy

      • Anonymous's avatar
        Anonymous
        Not applicable

        Hi honeybee84 ,

         

        As we haven’t heard back from you, we wanted to kindly follow up to check if the solution provided for the issue worked? or Let us know if you need any further assistance?

        If our response addressed, please mark it as Accept as solution and click Yes if you found it helpful.

         

        Please don't forget to give a "Kudos " – I’d truly appreciate it!

         

        Regards,

        B Manikanteswara Reddy