Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Rolling Average previous 12 months then 1 month rolling average

Posted on 2014-04-28
3
Medium Priority
?
229 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 49

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 49

Accepted Solution

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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.
If you’re using QODBC to update QuickBooks data from Microsoft® Access but Access is not showing the updated data, you could have set up QODBC incorrectly.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

971 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