I have a query that calculates a 12 Month Rolling SUM.
First, I create this query, called QrySub. Essentially, this query creates a filed to determine where the year start date is, giving the record date.
Then, from here, I create another Query to actually run the Rolling 12 Sums:SELECT ValueT.ValueID, ValueT.OE, ValueT.MetricID, ValueT.Month_Year, ValueT.Months, DateAdd("yyyy",-1,[Month_Year])+1 AS YearStartFROM ValueT;
This calculates the Rolling 12 Sum just fine, but there's a catch to the result that I want to capture.SELECT QrySub.ValueID, QrySub.OE, QrySub.MetricID, DatePart("yyyy",[qrysub]![Month_Year]) AS YearSort, DatePart("m",[QrySub]![Month_Year]) AS MonthSort, MonthName(DatePart("m",[QrySub]![Month_Year])) AS MonthDesc, QrySub.Month_Year, QrySub.Months, Sum(IIf([ValueT]![Month_Year] Between [QrySub]![YearStart] And [QrySub]![Month_Year],[ValueT]![Values],0)) AS Rolling12Sum, Sum(IIf([ValueT]![Month_Year] Between [QrySub]![YearStart] And [QrySub]![Month_Year],1,0)) AS Rolling12CountFROM QrySub LEFT JOIN ValueT ON (QrySub.OE = ValueT.OE) AND (QrySub.MetricID = ValueT.MetricID)
GROUP BY QrySub.ValueID, QrySub.OE, QrySub.MetricID, DatePart("yyyy",[qrysub]![Month_Year]), DatePart("m",[QrySub]![Month_Year]), MonthName(DatePart("m",[QrySub]![Month_Year])), QrySub.Month_Year, QrySub.Months;
Say my dataset looks like this (note that my query considers more fields than just what is shown):
MetricID Month_Year Values A1 1/1/2014 3289239 A1 2/1/2014 1324089 A1 3/1/2014 5646545 A1 4/1/2014 4584899 A1 5/1/2014 4983409 A1 6/1/2014 9089834 A1 7/1/2014 3920480 A1 8/1/2014 9032840 A1 9/1/2014 2093480 A1 10/1/2014 2342343 A1 11/1/2014 3294898 A1 12/1/2014 3425234 A1 1/1/2015 2460958 A1 2/1/2015 2463293 A1 3/1/2015 2463428 A1 4/1/2015 2406541 A1 5/1/2015 2408239 A1 6/1/2015
A1 7/1/2015
A1 8/1/2015
A1 9/1/2015
A1 10/1/2015
A1 11/1/2015
A1 12/1/2015
Now, my Rolling-12 calculation will display this:
MetricID Month_Year Rolling12Sum A1 1/1/2014 3289239 A1 2/1/2014 4613328 A1 3/1/2014 10259873 A1 4/1/2014 14844772 A1 5/1/2014 19828181 A1 6/1/2014 28918015 A1 7/1/2014 32838495 A1 8/1/2014 41871335 A1 9/1/2014 43964815 A1 10/1/2014 46307158 A1 11/1/2014 49602056 A1 12/1/2014 53027290 A1 1/1/2015 52199009 A1 2/1/2015 53338213 A1 3/1/2015 50155096 A1 4/1/2015 47976738 A1 5/1/2015 45401568 A1 6/1/2015 36311734 A1 7/1/2015 32391254 A1 8/1/2015 23358414 A1 9/1/2015 21264934 A1 10/1/2015 18922591 A1 11/1/2015 15627693 A1 12/1/2015 12202459
I do a count to determine if there are INDEED 12 months to roll on. Which is a step in the right direction as you can see from the 2nd query with the field Rolling12Count. HOWEVER,
Note that in the first table I listed that there are values up until 5/1/2015. I want the Query to ONLY SHOW Rolling12Sum's IF and ONLY IF there are indeed 12 months to roll on (which I capture), but that those 12 months also have values attached to them!
Any help is greatly appreciated.