Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
yj1111
Helper I
Helper I

Moving Average Calculation for Forecasting

Dear all,

 

I would like to use Moving Average of actuals in the same period last year to predict my actuals in the rest of the year. 

I use line and stacked column chart to show the values. 

X-axis: Month Nr of year 2024 (only 2024 is selected)

Y-axis: 1) Actual costs from month 1-6 (column), 2) Forecast costs from 7-12 (line)

 

The Dax function I use for moving average calculation: 

Actuals MA = AVERAGEX(DATESINPERIOD(Dates[Date],LASTDATE(Dates[Date]),-30,DAY],[ActualCosts])

Actuals Forecast = IF(ISBLANK([ActualCosts], CALCULATE([Actuals MA], SAMEPERIODLASTYEAR(Dates[Date])))

 

If I use Dates[Date] as X-axis, everything will work, however, the Actuals Forecast does not sum up on month level. How can I adjust my dax functions? (MovingAverage Dax Function is not availble in my PBI version)

 

Thanks in advance!

 

 

1 ACCEPTED SOLUTION
v-jtian-msft
Community Support
Community Support

Hi,@yj1111 .I am glad to help you.
According to your description, you have correctly calculated the correct result from the measure, but it is not displaying correctly on the line/bar chart in months.
If my understanding is correct, you can refer to my test below
Here is my test data:

vjtianmsft_0-1721019081729.pngvjtianmsft_1-1721019088358.png

Here are two of your original measures

Actuals Forecast = 
IF(
    ISBLANK([ActualCosts]), 
    CALCULATE(
        [Actuals MA], 
        SAMEPERIODLASTYEAR(Dates[Date])
    )
)
Actuals MA = 
AVERAGEX (
    DATESINPERIOD ( Dates[Date], LASTDATE ( Dates[Date] ), -30, DAY ),
    [ActualCosts]
)

To accommodate the fact that the Actuals MA uses a measure [ActualCosts], you don't give the exact code in your question.
So I modeled a simple measure (the column values in the table is [ActualCost])

vjtianmsft_2-1721019184948.png
Here is my solution idea:
I recreated a new column YearMonth
and used that column to replace the Date column that was originally displayed in visual
Wrote a new measure.
Aggregate your original end result for each day into months by month.
like this:

M_finalresult = 
VAR _yearMonth=MAX('Dates'[YearMonth])
RETURN
SUMX(
        FILTER(
            Dates, 
            Dates[YearMonth] = _yearMonth
        ), 
        [Actuals Forecast]
    )

vjtianmsft_3-1721019248324.png

vjtianmsft_4-1721019263265.png

The fields displayed on the x-axis and y-axis of the discounted graph are as follows.

vjtianmsft_5-1721019282565.png

Note that my test results may not be suitable for your real data, so you need to modify the code I provided or create a new code according to your actual situation, because due to the different computing environment will affect the final results of the measure.
I hope my test results can bring you good ideas.

If you can provide a pbix file that doesn't contain sensitive data (which you can share with everyone via OneDrive), that will help solve your problem.

I hope my suggestions give you good ideas, if you have any more questions, please clarify in a follow-up reply.

Best Regards,

Carson Jian,

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



View solution in original post

1 REPLY 1
v-jtian-msft
Community Support
Community Support

Hi,@yj1111 .I am glad to help you.
According to your description, you have correctly calculated the correct result from the measure, but it is not displaying correctly on the line/bar chart in months.
If my understanding is correct, you can refer to my test below
Here is my test data:

vjtianmsft_0-1721019081729.pngvjtianmsft_1-1721019088358.png

Here are two of your original measures

Actuals Forecast = 
IF(
    ISBLANK([ActualCosts]), 
    CALCULATE(
        [Actuals MA], 
        SAMEPERIODLASTYEAR(Dates[Date])
    )
)
Actuals MA = 
AVERAGEX (
    DATESINPERIOD ( Dates[Date], LASTDATE ( Dates[Date] ), -30, DAY ),
    [ActualCosts]
)

To accommodate the fact that the Actuals MA uses a measure [ActualCosts], you don't give the exact code in your question.
So I modeled a simple measure (the column values in the table is [ActualCost])

vjtianmsft_2-1721019184948.png
Here is my solution idea:
I recreated a new column YearMonth
and used that column to replace the Date column that was originally displayed in visual
Wrote a new measure.
Aggregate your original end result for each day into months by month.
like this:

M_finalresult = 
VAR _yearMonth=MAX('Dates'[YearMonth])
RETURN
SUMX(
        FILTER(
            Dates, 
            Dates[YearMonth] = _yearMonth
        ), 
        [Actuals Forecast]
    )

vjtianmsft_3-1721019248324.png

vjtianmsft_4-1721019263265.png

The fields displayed on the x-axis and y-axis of the discounted graph are as follows.

vjtianmsft_5-1721019282565.png

Note that my test results may not be suitable for your real data, so you need to modify the code I provided or create a new code according to your actual situation, because due to the different computing environment will affect the final results of the measure.
I hope my test results can bring you good ideas.

If you can provide a pbix file that doesn't contain sensitive data (which you can share with everyone via OneDrive), that will help solve your problem.

I hope my suggestions give you good ideas, if you have any more questions, please clarify in a follow-up reply.

Best Regards,

Carson Jian,

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



Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.