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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
chuckbo
Frequent Visitor

How to calculate forecast based on previous 2 months with multiple filters

I'm trying to re-learn my Power BI. I know I did this two or three years ago, but I'm not at that company any more and I don't remember how I did it.

 

I have a table of Costs. The keys are Location, Fiscal month/year, and Vendor. There's also an Order# field because we can have multiple payments to the vendor each month. I have a chart that plots the running total for the Cost, and there are slicers for location, fiscal year, and vendor. (I used the quick measure to make a running total for the cost.)

 

I've been asked to create a graph that shows actual values for the past months and forecasts future costs where the future month = the average of the preceding two months. Thus July forecast = (May actual + June actual)/2. But August forecast = (June Actual + July Forecast)/2. And September forecast = (July Forecast + Aug Forecast) / 2.

 

I have dimension tables for Date, Vendor, and Location.

The complexities that I'm dealing with:

- forecast shifts from using actuals to forecasts;

- I have to sum the Orders for a month before I average the two totals); and,

- this has to work with the slicers for Location, Fiscal year, and vendor.

4 REPLIES 4
tamerj1
Super User
Super User

Sahir_Maharaj
Super User
Super User

Hello @chuckbo,

 

1. Create a measure for Actual Costs:

Actual Cost = 
CALCULATE(
    [Running Total Cost],
    NOT(ISBLANK([Running Total Cost]))
)

2. Create a measure for Forecast:

Forecast = 
VAR SelectedDate = MAX('Date'[Date])
VAR PreviousMonth = CALCULATE(MIN('Date'[Date]), 'Date'[Date] < SelectedDate, 2)
VAR PreviousTwoMonthsActuals = CALCULATE([Actual Cost], 'Date'[Date] >= PreviousMonth && 'Date'[Date] < SelectedDate)
RETURN
IF(
    ISBLANK(PreviousMonth) || ISBLANK(PreviousTwoMonthsActuals),
    BLANK(),
    DIVIDE(
        [Actual Cost] + CALCULATE([Actual Cost], 'Date'[Date] = PreviousMonth),
        2
    )
)

3. Create a measure for Forecast Shift:

Forecast Shift = 
IF(
    MAX('Date'[Date]) > MAX('Date'[Date]),
    [Forecast],
    [Actual Cost]
)

4. Create a line chart and use the "Forecast Shift" measure as the value. Add the "Date" to the axis, and use slicers for "Location," "Fiscal year," and "Vendor" to filter the data.

 

Hope this helps!


Did I answer your question? Mark my post as a solution, this will help others!

If my response(s) assisted you in any way, don't forget to drop me a "Kudos" 🙂

Kind Regards,
Sahir Maharaj
Data Scientist | Data Engineer | Data Analyst | AI Engineer
P.S. Want me to build your Power BI solution? (Yes, its FREE!)
➤ Lets connect on LinkedIn: Join my network of 15K+ professionals
➤ Join my free newsletter: Data Driven: From 0 to 100
➤ Website: https://sahirmaharaj.com
➤ Email: sahir@sahirmaharaj.com
➤ Want me to build your Power BI solution? Lets chat about how I can assist!
➤ Join my Medium community of 30K readers! Sharing my knowledge about data science and artificial intelligence
➤ Explore my latest project (350K+ views): Wordlit.net
➤ 100+ FREE Power BI Themes: Download Now
LinkedIn Top Voice in Artificial Intelligence, Data Science and Machine Learning

Sahir,

I'm trying both of the solutions that were sent to me. 
When I enter your formula for Actual Cost, I get an error:
A function 'PLACEHOLDER' has been used in a True/False expression that is used as a table filter expression. This is not allowed.

 

 

Sahir,

thank you for the reply. I can try it out on Monday.

 

something I don't think I see, though... I see where I'm taking the average of the actual cost and the previous month's cost. But will this still work when I'm two months removed from an actual cost and have to rely on the forecast for the previous two months? The problem I couldn't overcome was that I could t get past the recursive nature of the formula.

Helpful resources

Announcements
June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.