Solved

Rolling Average previous 12 months then 1 month rolling average

Posted on 2014-04-28
3
227 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 48

Expert Comment

by:Dale Fye
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 48

Accepted Solution

by:
Dale Fye 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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

630 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