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
MagikJukas
Resolver III
Resolver III

forecasting sales based on history

Hello,

I have a table cotaning daily orders for the last three years.

I want to create a measure that predicts how many orders we are going to receive by the end of the month, based on the last two years sales.

I then want to see all this in a line chart so I can see the three years MTD sales trajectories.

 

Any tips?

 

 

I just created a random dataset for the sake of the exercise:

 

DayYearMonthSales
120201246
220201226
320201217
420201225
520201244
620201225
720201219
820201250
920201210
1020201214
1120201211
1220201219
1320201243
1420201234
1520201211
1620201235
1720201246
1820201243
1920201219
2020201237
2120201221
2220201215
2320201231
2420201225
2520201248
2620201224
2720201248
2820201217
2920201235
3020201216
120211217
220211217
320211244
420211238
520211250
620211229
720211224
820211250
920211227
1020211244
1120211247
1220211242
1320211231
1420211221
1520211223
1620211220
1720211223
1820211243
1920211225
2020211236
2120211225
2220211218
2320211220
2420211215
2520211224
2620211225
2720211219
2820211233
2920211248
3020211215
120221246
220221245
320221247
420221229
520221249
620221230
720221247
820221249
920221223
1020221213
1 ACCEPTED SOLUTION
PaulOlding
Solution Sage
Solution Sage

Hi @MagikJukas 

First of all I used a Date table for these measures and converted your Day, Month & Year columns in Sales table to a single date field.  So, the model looks like this:

PaulOlding_0-1670938375449.png

Now, here's a basic Sales amount measure and a SalesMTD measure to use in a line chart:

Sales Amount = SUM(Sales[Sales])

Sales MTD = 
VAR _MaxDateInData = MAXX(ALL(Sales), Sales[Date])
VAR _Result = IF(MAX('Date'[Date]) <= _MaxDateInData, TOTALMTD([Sales Amount], 'Date'[Date]) )
RETURN
    _Result

 I can set up a line chart like this to track MTD sales trajectories across years.  Note, the Year, Month & Day columns are from the date table:

PaulOlding_1-1670938564199.png

 Lastly, the forecast measure.  There are many different rules/logic you could use to forecast sales.  This one carries on from whatever the actual sales are this month and adds the average of the same day in previous years, and returns a cumulative amount like the Sales MTD measure

Sales Forecast MTD = 
VAR _MaxDateInData = MAXX(ALL(Sales), Sales[Date])
VAR _CurrentDate = MAX('Date'[Date])
VAR _SalesMTD = TOTALMTD([Sales Amount], 'Date'[Date])
VAR _ForecastMTD = 
SUMX(
    DATESBETWEEN('Date'[Date], _MaxDateInData + 1, _CurrentDate),
    VAR _ThisDate = CALCULATE(MAX('Date'[Date]))
    RETURN
    AVERAGEX(
        FILTER(ALL('Date'), 'Date'[Month Num] = MONTH(_ThisDate) && 'Date'[Day] = DAY(_ThisDate) && 'Date'[Year] < YEAR(_ThisDate)), 
        [Sales Amount]
    )
)
VAR _Result = IF(MAX('Date'[Date]) >= _MaxDateInData, _SalesMTD + _ForecastMTD )
RETURN
    _Result

 Here's what that can look like on a line chart, along with Sales MTD.

PaulOlding_2-1670938952569.png

Frustratingly it doesn't look like you can combine the line charts.  ie have Sales MTD split by Year, plus the Sales Forecast MTD all in one line chart.  Perhaps you can achieve that by layering 2 line charts on top of each other or delve into creating a custom chart with Deneb if you want that.

View solution in original post

4 REPLIES 4
PaulOlding
Solution Sage
Solution Sage

Hi @MagikJukas 

First of all I used a Date table for these measures and converted your Day, Month & Year columns in Sales table to a single date field.  So, the model looks like this:

PaulOlding_0-1670938375449.png

Now, here's a basic Sales amount measure and a SalesMTD measure to use in a line chart:

Sales Amount = SUM(Sales[Sales])

Sales MTD = 
VAR _MaxDateInData = MAXX(ALL(Sales), Sales[Date])
VAR _Result = IF(MAX('Date'[Date]) <= _MaxDateInData, TOTALMTD([Sales Amount], 'Date'[Date]) )
RETURN
    _Result

 I can set up a line chart like this to track MTD sales trajectories across years.  Note, the Year, Month & Day columns are from the date table:

PaulOlding_1-1670938564199.png

 Lastly, the forecast measure.  There are many different rules/logic you could use to forecast sales.  This one carries on from whatever the actual sales are this month and adds the average of the same day in previous years, and returns a cumulative amount like the Sales MTD measure

Sales Forecast MTD = 
VAR _MaxDateInData = MAXX(ALL(Sales), Sales[Date])
VAR _CurrentDate = MAX('Date'[Date])
VAR _SalesMTD = TOTALMTD([Sales Amount], 'Date'[Date])
VAR _ForecastMTD = 
SUMX(
    DATESBETWEEN('Date'[Date], _MaxDateInData + 1, _CurrentDate),
    VAR _ThisDate = CALCULATE(MAX('Date'[Date]))
    RETURN
    AVERAGEX(
        FILTER(ALL('Date'), 'Date'[Month Num] = MONTH(_ThisDate) && 'Date'[Day] = DAY(_ThisDate) && 'Date'[Year] < YEAR(_ThisDate)), 
        [Sales Amount]
    )
)
VAR _Result = IF(MAX('Date'[Date]) >= _MaxDateInData, _SalesMTD + _ForecastMTD )
RETURN
    _Result

 Here's what that can look like on a line chart, along with Sales MTD.

PaulOlding_2-1670938952569.png

Frustratingly it doesn't look like you can combine the line charts.  ie have Sales MTD split by Year, plus the Sales Forecast MTD all in one line chart.  Perhaps you can achieve that by layering 2 line charts on top of each other or delve into creating a custom chart with Deneb if you want that.

Hello PO,

thanks. yesterday I worked on a solution very similar to yours. the key point was creating a new table contaning all the available days and then using formulas similar to yours.

 

thanks 

FreemanZ
Super User
Super User

Thanks I will consider that.
But I would like to create something using formulas.

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

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.