Forum Discussion
How to calculate Future Sales
- 2 years ago
Hi Mike,
Ah, I see you're also filtering by product and your fact table doesn't have the date, only the YearWeek.
In that case, the below should work for your Forecast measure instead. (Mainly I've added a variable to capture the max date of the row in the visual.)
Forecast = VAR MaxDate = CALCULATE ( MAX ( 'Date'[Date] ), REMOVEFILTERS ( Preorder ) ) VAR AvgVariation = [Avg Variation] VAR FirstActualSalesDate = CALCULATE ( FIRSTNONBLANK ( 'Date'[Date], [preorder] ), REMOVEFILTERS ( 'Date' ) ) VAR LastActualSalesDate = CALCULATE ( LASTNONBLANK ( 'Date'[Date], [preorder] ), REMOVEFILTERS ( 'Date' ) ) VAR LastActualSales = CALCULATE ( [preorder], REMOVEFILTERS ( 'Date' ), 'Date'[Date] = LastActualSalesDate ) VAR NumWeeksAfterLastActualSalesDate = DATEDIFF ( LastActualSalesDate, MaxDate, WEEK ) VAR Result = COALESCE ( [preorder], LastActualSales * (1 + [Avg Variation] ) ^ NumWeeksAfterLastActualSalesDate) RETURN IF ( MaxDate >= FirstActualSalesDate, Result )Something to consider in the future:
While the above measure works for your data model, I strongly encourage you to learn more about data modeling, so you can avoid many-to-many relationships in the future (and also write measures more easily!). For example, in your scenario where the preorders are captured at a weekly level, instead of capturing the WeekYear in the Preorder table, I would suggest having a WeekStart column instead for example. That way, you can have a one-to-many relationship instead.
----------------------------------
If this post helps, please consider accepting it as the solution to help other members find it quickly. Also, don't forget to hit that thumbs up and subscribe! (Oh, uh, wrong platform?) - 2 years ago
Mike,
Apologies, I made two subtle but important changes early on that I forgot to mention.
- The Catalog to Preorder relationship should not be bi-directional. In general, relationships should only filter from dimensions (ie: Catalog) to facts (ie: Preorder).
- Replace Preorder[Product] with Catalog[Product] in your product slicer. Again, in general, you should default to using columns from dimension tables in your slicers.
When you do, you should see the below:
This free course on data modeling from SQLBI should be tremendously helpful for you if you will be working with Power BI and DAX (and data analysis in general!) often. I hope it is as beneficial for you as it was for me! 🙂
----------------------------------
If this post helps, please consider accepting it as the solution to help other members find it quickly. Also, don't forget to hit that thumbs up and subscribe! (Oh, uh, wrong platform?)
Hi Wilson, thx a lot i try to apply your solution to my original pbix and came back to you with a feedback. in the future if i need any advice can text you directly?
Mike,
----------------------------------
If this post helps, please consider accepting it as the solution to help other members find it quickly. Also, don't forget to hit that thumbs up and subscribe! (Oh, uh, wrong platform?)
- Mike9212 years agoNew Member
Thx a lot for the help you are a master of Bi, i expanded the calendar to reach end of year 2024, but forecast the data from the last preorder to max calendar date, i need to stop the forecast when the date reach the relase date. how can i do? update. i edit the sample i think i'm near the solution but something not work properly. sample.pbix
- Wilson_2 years ago
Memorable Member
Mike,
Try this updated forecast measure:
Forecast = VAR MaxDate = CALCULATE ( MAX ( 'Date'[Date] ), REMOVEFILTERS ( Preorder ) ) VAR MaxWeekYear = CALCULATE ( MAX ( 'Date'[YearWeek] ), REMOVEFILTERS ( Preorder ) ) VAR ReleaseWeek = INT ( [release week] ) VAR AvgVariation = [Avg Variation] VAR FirstActualSalesDate = CALCULATE ( FIRSTNONBLANK ( 'Date'[Date], [preorder] ), REMOVEFILTERS ( 'Date' ) ) VAR LastActualSalesDate = CALCULATE ( LASTNONBLANK ( 'Date'[Date], [preorder] ), REMOVEFILTERS ( 'Date' ) ) VAR LastActualSales = CALCULATE ( [preorder], REMOVEFILTERS ( 'Date' ), 'Date'[Date] = LastActualSalesDate ) VAR NumWeeksAfterLastActualSalesDate = DATEDIFF ( LastActualSalesDate, MaxDate, WEEK ) VAR Result = COALESCE ( [preorder], LastActualSales * (1 + [Avg Variation] ) ^ NumWeeksAfterLastActualSalesDate) VAR Display = IF ( MaxDate >= FirstActualSalesDate && MaxWeekYear <= ReleaseWeek, Result ) RETURN DisplayAdded another variable to calculate the selected WeekYear and a variable to calculate the release week. Note: I also had to convert release week to an integer because Catalog[Release Week] is a text field. For multiple reasons, this should probably be an integer field.
----------------------------------
If this post helps, please consider accepting it as the solution to help other members find it quickly. Also, don't forget to hit that thumbs up and subscribe! (Oh, uh, wrong platform?)- Mike9212 years agoNew Member
Hi wilson, i apply your dax but get stuck, forecast 2 it's your old dax, forecast is the new one.
Sorry Wilson, please don't hate me.... follow the pbix. sample.pbix