Forum Discussion
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.
Any guidance is greatly appreciated.
Thank you.
- Anonymous1 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
- bhanu_gautamSuper User
honeybee84 , Try using
Cumulative Prior Month Forecast =
VAR SelectedReportingPeriod = MAX(FACT_CombinedData[Reporting Period])
VAR PriorReportingPeriod = EDATE(SelectedReportingPeriod, -1) // Get the prior reporting periodRETURN
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
)
)- honeybee84Frequent Visitor
Hi bhanu_gautam
Unfortunately it's still giving monthly values. Thanks for replying though 🙂
- johnt75Super User
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 Resultwhere DIM_Date[Year month] is the date column linked to your fact reporting period column.
- honeybee84Frequent 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 🙂
- johnt75Super 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.
- AnonymousNot 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
- AnonymousNot 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
- AnonymousNot 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