Forum Discussion
6 Month Moving Sum
New member and first time poster.
After a lot of research and reading, I am unable to resolve my issue. I can easily create a moving sum of my volume, but I need months 1 - 5 to remain blank and then months 6 - n to populate with my moving sum values as illustrated below:
| Year | Month | Volume | MA-6M | Month # |
| 1996 | Jul | 27,862 | 1 | |
| 1996 | Aug | 25,485 | 2 | |
| 1996 | Sep | 26,381 | 3 | |
| 1996 | Oct | 37,516 | 4 | |
| 1996 | Nov | 45,600 | 5 | |
| 1996 | Dec | 45,240 | 208,084 | 6 |
| 1997 | Jan | 61,258 | 241,480 | 7 |
| 1997 | Feb | 38,484 | 254,479 | 8 |
Months 1 - 5 need to remain blank for subsequent calculations where I use SAMEPERIODLASTYEAR.
My introduction to DAX has been via the deep-end of the pool the past 5-wks, and I just haven't been able to put all the pieces together. Additionally, I do have a calendar linking my data together. Any guidance would be appreciated. Thanks.
Hi Medic7653,
Believe that you are using a filter or something similar to get the previous 6 months however taking into account the data the first rows also are consider in the calculations althoung for those specific rows it's not getting the full 6 months since they don't have enough rows. You need to do something like this:
Moving 6 Months Sum = VAR Month_selected = MAX ( 'Fact'[Month #] ) RETURN IF ( Month_selected < 6; BLANK (); CALCULATE ( SUM ( 'Fact'[Volume] ); FILTER ( ALL ( 'Fact'[Month #]; 'Fact'[Year]; 'Fact'[Month] ); 'Fact'[Month #] >= Month_selected - 5 && 'Fact'[Month #] <= Month_selected ) ) )Regards
MFelix
9 Replies
- Medic7653Regular Visitor
Apologies. Here's my base formula for my measure:
Moving 6 Month Sum :=
CALCULATE(
SUM(Order_Details[Tot Sales]),
DATESINPERIOD(DimDate[Date],
LASTDATE(DimDate[Date]),
-6,MONTH)
)
- MFelixSuper User
Hi Medic7653,
Believe that you are using a filter or something similar to get the previous 6 months however taking into account the data the first rows also are consider in the calculations althoung for those specific rows it's not getting the full 6 months since they don't have enough rows. You need to do something like this:
Moving 6 Months Sum = VAR Month_selected = MAX ( 'Fact'[Month #] ) RETURN IF ( Month_selected < 6; BLANK (); CALCULATE ( SUM ( 'Fact'[Volume] ); FILTER ( ALL ( 'Fact'[Month #]; 'Fact'[Year]; 'Fact'[Month] ); 'Fact'[Month #] >= Month_selected - 5 && 'Fact'[Month #] <= Month_selected ) ) )Regards
MFelix
- Medic7653Regular Visitor
MFelix- Thanks for your reply. Forgive me, but Month # was added to illustratewhen I wanted the mvoing sum to begin. It is not part of the data set. My apologies as this is my first post. The only data fields in my data set are: Year, Month, Volume and MA-6M.
I was, however, able to learn quite a lot from your example.
- tinkertrustFrequent Visitor
Hi, I had a similar scenario and found this to work well:
6-Mo Rolling Profit:= CALCULATE([Profit], DATESINPERIOD(Calendar_Lookup[date], MAX(Calendar_Lookup[date]), -6, MONTH))
6-Mo Rolling Avg Profit = [6-Month Rolling Profit] /
CALCULATE(DISTINCTCOUNT(Calendar_Lookup[Year_Month]), DATESINPERIOD(Calendar_Lookup[date], LASTDATE(Calendar_Lookup[date]), -6, MONTH))This calcualtes a 6 month rolling sum first and then worksout the average. The second formula should returns the average of the first month alone, then the first two months, and finally the 3-month period for the remaining months.
Hope it helps.