x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 234

# Rolling Average previous 12 months then 1 month rolling average

I have a query with EmployeeID, PayDate, HoursPerPeriod.

Example:

EmployeeId---PayDate-----HoursPerPeriod---RollingAverage

-----1-----------10/8/2012-------35.28
-----1-----------10/22/2012-----42.96
-----1-----------11/4/2012--------41.92
-----1-----------11/18/2012--------32.66
-----1-----------12/16/2012--------35.65
-----1-----------12/30/2012--------36.0
-----1-----------1/14/2013--------43.5
-----1-----------1/28/2013--------32.66
-----1-----------2/11/2013--------39.84
-----1-----------2/25/2013--------31.03
-----1-----------3/11/2013--------38.08
-----1-----------3/25/2013--------35.12
-----1-----------4/8/2013---------38.84
-----1-----------4/22/2013--------37.79
-----1-----------5/6/2013---------39.44
-----1-----------5/20/2013--------38.04
-----1-----------6/3/2013---------37.48
-----1-----------6/17/2013--------48.56
-----1-----------7/1/2013--------39.44
-----1-----------7/15/2013--------36.71
-----1-----------7/29/2013--------34.62
-----1-----------8/12/2013--------36.19
-----1-----------8/26/2013--------35.12
-----1-----------9/9/2013----------31.68
-----1-----------9/23/2013---------34.42
-----1-----------10/7/2013--------36.93------------38.0
-----1-----------10/21/2013-------38.06------------37.0
-----1-----------11/4/2013--------34.23------------36.0

----so on and so forth with different EmployeeID.

1) How to get the 12 month rolling average from 10/8/2012 to 9/23/2013
and results will be shown on 10/7/2013 under Rolling Average field?

2) How to get the rolling average by paydate after 10/7/2013?

This is the rolling average based on employeeID.

Thank you.
0
Queennie L
• 2
1 Solution

Commented:
it kind of depends on how you got what you have and what how your data is structured, the problem with using that query as your data source is that a two week pay cycle does not equate to a 12 month rolling average, but you could get close, with a 52 week rolling average.

You might try something like:

Select T1.EmployeeID
, T1.PayDate
, T1.HoursPerPeriod
, Count(T2.EmployeeID) as PayPeriods
, Average(T2.HoursPerPeriod) as RollingAverage
FROM yourQuery T1
LEFT JOIN yourQuery T2
ON T1.EmployeeID = T2.EmployeeID
AND T2.PayDate > DateAdd("m", -12, T1.PayDate)
GROUP BY T1.EmployeeID
, T1.PayDate
, T1.HoursPerPeriod

Because of the non-equi join on PayDate, this query will not be editable in the query grid.  Basically, what this query does is join the query to itself and link the last 26 for each employee to themselves.  It counts the records to determine how many payperiods are included and sums the hours of those pay periods.  If you then only want to display the Rolling average for those who have worked for more than a year, you could make the query above the source for another query, something like:

Select Q2.EmployeeID
, Q2.PayDate
, Q2.HoursPerPeriod
, IIF(Q2.PayPeriods < 26, NULL, Q2.RollingAverage) as RollingAverage
FROM queryAbove
Order By Q2.EmployeeID, Q2.PayDate
0

Author Commented:

Why is the Rolling Average has the same results for all paydate and employeeid in the 1st query?

In 2nd query, Rolling Average is blank?

Output.xlsx
0

Commented:
Sorry, forgot to restrict the upper end of each of the items in the first query, so it is averaging across off of the later dates as well.  Try this for query1:

Select T1.EmployeeID
, T1.PayDate
, T1.HoursPerPeriod
, Count(T2.EmployeeID) as PayPeriods
, Average(T2.HoursPerPeriod) as RollingAverage
FROM yourQuery T1
LEFT JOIN yourQuery T2
ON T1.EmployeeID = T2.EmployeeID
AND T2.PayDate > DateAdd("m", -12, T1.PayDate)
AND T2.PayDate <= T1.PayDate
GROUP BY T1.EmployeeID
, T1.PayDate
, T1.HoursPerPeriod
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.