Solved

Rolling Average previous 12 months then 1 month rolling average

Posted on 2014-04-28
3
222 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now