Home » excel » Excel: GroupBy Person, rolling average from last N occurences

Excel: GroupBy Person, rolling average from last N occurences

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have some data I would like to apply a rolling average to. I would like to do a group by person for the last 3 days and average GA for the last 3 times they have worked based off the date.

Today: D1 = 9/19/2018 or =TODAY()

person  work_date   GA
JOHN    10/7/2017   2
MIKE    10/7/2017   2
JAKE    10/7/2017   2
JOHN    10/6/2017   3
MIKE    10/6/2017   3
JAKE    10/6/2017   3
JOHN    10/5/2017   2
MIKE    10/5/2017   2
JAKE    10/5/2017   2
JOHN    10/4/2017   20
MIKE    10/4/2017   20
JAKE    10/4/2017   20
JOE     9/30/2017   3
JOE     9/23/2017   2
JOE     5/22/2017   2
JOE     4/22/2017   20
AVA     3/22/2017   10

Wondering if a cell in the sheet should be =TODAY() so the formula has a date to use to check the last 3 days closest to TODAY.

Result:

person  GA
JOHN    2.33
MIKE    2.33
JAKE    2.33
JOE     2.33
AVA      10

I know you can use a pivot table but some people are in the data more than others therefore that would throw off my average of the last three times that person has worked. And if I only use data from the past x number of days people like Joe and his third day of work would not be in the data at all and move his average from 2.33 to 2.50. If a person is in the data less than twice than they should still appear just like AVA with an average of 10. I found some rolling averages with AVERAGEIF but not quite like this I feel.

How to&Answers:

AVERAGEIFS with some criteria trickery is all that is needed.

=AVERAGEIFS(C:C, A:A, E2, B:B, "<="&TODAY(), B:B, ">="&AGGREGATE(14, 7, B:B/((A$1:A$18=E2)*(B$1:B$18<=TODAY())), MIN(3, COUNTIFS(A:A, E2, B:B, "<="&TODAY()))))

enter image description here