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
leilei787
Helper II
Helper II

Rolling average for the past 3 months ( have to be full month)

Hello:

 

i am trying to figure out how to calculate rolling past 3 months average. but they need to be full month....for example, today is May 12th...so the calculation should do an average of April, March and Feb ( excluding May because it is not completed yet)

 

Below is my data sample....let's say i want to know rolling average for the past 3 months for Pencil.

 

thank you in advance!!!! 

OrderDateItemUnits
1/1/2022Pencil 
1/2/2022Binder50
1/3/2022Pencil36
1/4/2022Pen27
1/5/2022Pencil56
1/6/2022Binder60
1/7/2022Pencil75
1/8/2022Pencil90
1/9/2022Pencil32
1/10/2022Binder60
1/11/2022Pencil90
1/12/2022Binder29
1/13/2022Binder81
1/14/2022Pencil35
1/15/2022Desk 
1/16/2022Pen Set16
1/17/2022Binder28
1/18/2022Pen64
1/19/2022Pen15
1/20/2022Pen Set 
1/21/2022Pencil67
1/22/2022Pen Set74
1/23/2022Binder46
1/24/2022Binder87
1/25/2022Binder 
1/26/2022Binder 
1/27/2022Pen Set50
1/28/2022Pencil66
1/29/2022Pen 
1/30/2022Pencil53
1/31/2022Binder80
2/1/2022Desk 
2/2/2022Pen Set62
2/3/2022Pen Set55
2/4/2022Pen Set42
2/5/2022Desk 
2/6/2022Pencil 
2/7/2022Pen76
2/8/2022Binder57
2/9/2022Pencil14
2/10/2022Binder11
2/11/2022Binder 
2/12/2022Binder28
2/13/2022Pencil 
2/14/2022Binder 
2/15/2022Pencil22
2/16/2022Pen 
2/17/2022Pencil44
2/18/2022Binder 
2/19/2022Pencil52
2/20/2022Pencil 
2/21/2022Pencil 
2/22/2022Binder53
2/23/2022Pencil 
2/24/2022Binder25
2/25/2022Binder 
2/26/2022Pencil52
2/27/2022Desk55
2/28/2022Pen Set3
3/1/2022Binder5
3/2/2022Pen8
3/3/2022Pen6
3/4/2022Pen Set 
3/5/2022Pencil85
3/6/2022Pen Set47
3/7/2022Binder64
3/8/2022Binder 
3/9/2022Binder77
3/10/2022Binder 
3/11/2022Pen Set 
3/12/2022Pencil 
3/13/2022Pen47
3/14/2022Pencil 
3/15/2022Binder 
3/16/2022Desk47
3/17/2022Pen Set99
3/18/2022Pen Set5
3/19/2022Pen Set 
3/20/2022Desk5
3/21/2022Pencil 
3/22/2022Pen7
3/23/2022Binder74
3/24/2022Pencil 
3/25/2022Binder 
3/26/2022Binder50
3/27/2022Binder36
3/28/2022Pencil27
3/29/2022Binder56
3/30/2022Pencil60
3/31/2022Pen75
4/1/2022Pencil90
4/2/2022Binder32
4/3/2022Pencil60
4/4/2022Pencil90
4/5/2022Pencil29
4/6/2022Binder81
4/7/2022Pencil35
4/8/2022Binder 
4/9/2022Binder16
4/10/2022Pencil28
4/11/2022Desk64
4/12/2022Pen Set15
4/13/2022Binder 
4/14/2022Pen90
4/15/2022Pen29
4/16/2022Pen Set90
4/17/2022Pencil29
4/18/2022Pen Set 
4/19/2022Binder29
4/20/2022Pen Set 
4/21/2022Pencil 
4/22/2022Pen Set29
4/23/2022Binder90
4/24/2022Pen Set29
4/25/2022Pencil 
4/26/2022Pen Set29
4/27/2022Binder 
4/28/2022Pen Set 
4/29/2022Pencil29
4/30/2022Pen Set90
5/1/2022Binder29
5/2/2022Pen Set 
5/3/2022Pencil29
5/4/2022Pen Set 
5/5/2022Binder22
5/6/2022Pen Set33

 

@tamerj1 @Greg_Deckler 

2 ACCEPTED SOLUTIONS

You are welcome.  If my original reply helped, please mark it as Answer.  ABCD is just the title of the new column which will get created in the virtual table (via the SUMMARIZE() function). 


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

Hi @leilei787 
I'm using DropBox on different machines. The file was open and unsaved on this machine. Sory for that. Here is a lnik on WeTransfer just to make sure you recieve the correct one.

https://we.tl/t-nsW7l0Jekq

View solution in original post

14 REPLIES 14
tamerj1
Super User
Super User

HI @leilei787 
Here is a sample file with the solution https://www.dropbox.com/t/23vxVrFExwxu0xUw

Rolling Average = 
VAR CurrentDate = 
    MAX ( 'Date'[Date] )
VAR StartofPeriod =
    EOMONTH ( CurrentDate, -4 ) + 1
VAR EndOfPeriod =
    EOMONTH ( CurrentDate, -1 )
VAR Result =
    CALCULATE ( 
        AVERAGE ( Sales[Units] ),
        'Date'[Date] >= StartofPeriod,
         'Date'[Date] <= EndOfPeriod
    )
RETURN
    Result

1.png2.png

Ashish_Mathur
Super User
Super User

Hi,

You may download my PBI file from here.

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Thank you Ashish!

i review the PBI you sent...It works except i changed the formula a bit so that it reflects past 3 completed month, not 2. 

 

Question, in your formula, what is that "ABCD" represent?

 

Average units sold in past 3 completed months = averagex(SUMMARIZE(CALCULATETABLE('Calendar',DATESBETWEEN('Calendar'[Date],edate(EOMONTH(today(),-1)+1,-3),EOMONTH(today(),-1))),'Calendar'[Year],'Calendar'[Month name],"ABCD",[Units sold]),[ABCD])

 

 

thanks for your time

Apologies for misunderstanding your requierement. Here is the modified version of my solution https://www.dropbox.com/t/sqjAtA5drCIicKZk

 

Rolling Average = 
VAR CurrentDate = 
    MAX ( 'Date'[Date] )
VAR StartofPeriod =
    EOMONTH ( CurrentDate, -4 ) + 1
VAR EndOfPeriod =
    EOMONTH ( CurrentDate, -1 )
VAR Result =
    CALCULATE ( 
        AVERAGEX (
            VALUES ( 'Date'[Month] ),
            CALCULATE ( SUM ( Sales[Units] ) )
        ),
        'Date'[Date] >= StartofPeriod,
        'Date'[Date] <= EndOfPeriod
    )
RETURN
    Result

1.png

 

@tamerj1  hi Tamerj1, thank you again for your time!

 

I open up the PBI you attached....somehow i am still seeing the same result. (see screenshot)....is it the right file? thanks

 

PBI.JPG

@leilei787 

maybe was no saved when uploaded. I'm sending this from the phone. I hope it is the correct one. 
https://www.dropbox.com/transfer/cap_pid_ft:AAAAABKFFQJMawa4rlv6E90_JwaV6XsJl7LAFMJZeqiKUOApi3ZVcn0

@tamerj1 hi Tamerj1, still not quite....i swear this is the one you just sent...still not the right one 

 

PBI.JPG

Hi @leilei787 
I'm using DropBox on different machines. The file was open and unsaved on this machine. Sory for that. Here is a lnik on WeTransfer just to make sure you recieve the correct one.

https://we.tl/t-nsW7l0Jekq

@tamerj1thank you! this file works. two question:

 

i see you added "Date" table, basically a time table...is it necessary? can i just use the order date in my original table? ( i tried not to add additional time table to my real dataset)

if additional time table is needed....is your time based on excel file or using some kind of formula? i want to be able to replicate the table if need to

 

2nd question, i need to do 3 months moving average for the past 2 years...so let's say Apr 2022, i need to average Apr, Mar and Feb; Then Mar 2022, i need to average Mar, Feb and Jan; for Feb 2022, i need to average Feb, Jan and Dec 2021....does your solution work cross years? ( no partial month)

 

i appreciate for your time!

@leilei787 

Yes it works accross years. This is why I added a date table. This is a date table not time table. It is created using dax code. Just click on New table snd paste the code. It is there in pbi file. 

@tamerj1 thank you Tamerj1, you have taught me quite a few things. appreciate!

 

After some reviews, your formula works! The only limitation is that if the unit is 0 for that month, the formula skip the month and divide the total by 2 instead of 3. for example, if Pen sold 10 unit in Jan, 0 unit in Feb and 20 unit in Mar, the result become (10+20)/2=15. it should be (10+20)/3=10. 

 

i am using the data below...could you please see if we can capture the month even though 0 unit sold? appreciate again!

 

OrderDateItemUnits
1/4/2021Pen27
1/15/2021Desk 
1/18/2021Pen64
1/19/2021Pen15
1/29/2021Pen 
2/1/2021Desk 
2/5/2021Desk 
2/7/2021Pen76
2/16/2021Pen 
2/27/2021Desk55
3/2/2021Pen8
3/3/2021Pen6
3/13/2021Pen47
3/16/2021Desk47
3/20/2021Desk5
3/22/2021Pen7
3/31/2021Pen75
4/11/2021Desk64
4/14/2021Pen90
4/15/2021Pen29
5/8/2021Pen 
5/11/2021Desk 
5/15/2021Desk 
5/17/2021Pen76
5/26/2021Pen 
6/6/2021Desk55
6/9/2021Pen8
6/10/2021Pen6
6/20/2021Pen47
6/23/2021Desk47
6/27/2021Desk5
6/29/2021Pen7
7/8/2021Pen75
7/19/2021Desk64
7/22/2021Pen90
7/23/2021Pen29
8/15/2021Pen 
8/18/2021Desk 
8/22/2021Desk 
8/24/2021Pen76
9/2/2021Pen 
9/13/2021Desk55
9/16/2021Pen8
9/17/2021Pen6
9/27/2021Pen47
9/30/2021Desk47
10/4/2021Desk5
10/6/2021Pen7
10/15/2021Pen75
10/26/2021Desk64
10/29/2021Pen90
10/30/2021Pen29
11/22/2021Pen 
11/25/2021Desk 
11/29/2021Desk 
12/1/2021Pen76
12/10/2021Pen 
12/21/2021Desk55
12/24/2021Pen8
12/25/2021Pen6
1/4/2022Pen47
1/7/2022Desk47
1/11/2022Desk5
1/13/2022Pen7
1/22/2022Pen75
2/2/2022Desk64
2/5/2022Pen90
2/6/2022Pen29
3/1/2022Pen 
3/4/2022Desk 
3/8/2022Desk 
3/10/2022Pen76
3/19/2022Pen 
3/30/2022Desk55
4/2/2022Pen8
4/3/2022Pen6
4/13/2022Pen47
4/16/2022Desk47
4/20/2022Desk5
4/22/2022Pen7
5/1/2022Pen75

 

 

Hi @leilei787 

this how AVERAGE behaves in case od blanks. Please try (in case you have 3 consecutive months with no sales use DIVIDE function instead of "/" operator. 

Rolling Average = 
VAR CurrentDate = 
    MAX ( 'Date'[Date] )
VAR StartofPeriod =
    EOMONTH ( CurrentDate, -4 ) + 1
VAR EndOfPeriod =
    EOMONTH ( CurrentDate, -1 )
VAR Result =
    CALCULATE ( 
        SUMX (
            VALUES ( 'Date'[Month] ),
            CALCULATE ( SUM ( Sales[Units] ) )
        ),
        'Date'[Date] >= StartofPeriod,
        'Date'[Date] <= EndOfPeriod
    )/3
RETURN
    Result

 

@tamerj1 

totally awesome! thank you!

You are welcome.  If my original reply helped, please mark it as Answer.  ABCD is just the title of the new column which will get created in the virtual table (via the SUMMARIZE() function). 


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

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!

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.