Solved

Rolling Average previous 12 months then 1 month rolling average

Posted on 2014-04-28
3
224 Views
Last Modified: 2014-05-09
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.

Please help.

Thank you.
0
Comment
Question by:Queennie L
  • 2
3 Comments
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 40029122
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 Comment

by:Queennie L
ID: 40029616
Please find attaché excel file the two queries output.

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?

Thank you for your help.
Output.xlsx
0
 
LVL 47

Accepted Solution

by:
Dale Fye (Access MVP) earned 500 total points
ID: 40029641
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

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question