# Average the last N values in a Sheets column using Arrayformula

I'm looking to create a formula (not necessarily an arrayformula), that will automatically calculate an average. This sounds easy, but surprisingly, it isn't.

Let me explain...

I have a list of values in column G of a google sheet. Once each week, a script adds a new row at the bottom of the sheet and a new value in column G of the new row. Each week, after the script runs, I need to get the average of the last N cells and paste the result in columns N, O,and P. (The average of 2 values pastes to column N, the average of 6 values pastes to column O, and the average of 13 values pastes to column P.)

Calculating an average of values in a column is simple. Calculating the average of the last N values in a column is simple. Calculating an average automatically for the last N values in a column is difficult. I don't open the sheet every day, and even if I did, I usually don't have the time (or desire) to update every time. I want this to be automatic. (If what I want to do is impossible with a formula, a script solution is acceptable.)

Here's how I need it to work. The source values are in column G. (Row 1 is always the column label.) The values to be averaged start in cell G2 and continue to the end of column G.

I need to average G2 and G3, and paste the result in cell N3. Then, average G3 and G4, and paste the result in cell N4. Then, average G4 and G5, and paste the result in cell N5. And so on, to the bottom of column G.

Then, I need to duplicate this, but with 6 values. I need to average G2:G7, then paste the result in cell O7. Then, average G3:G8, then paste the result in cell O8. Then, average G4:G9, then paste the result in cell O9. Again, all the way to the bottom of column G.

Finally, I need to do all of this again, only this time with 13 values.

(If someone can tell me how, I can share a sheet.)

Would anyone care to give me some insight?

• Welcome. Would you summarise your research before you asked this question (as discussed in How do I ask a good question?). You said that Calculating an average automatically for the last N values in a column is difficult - there are many precedents for doing this - it is worthwhile that we can understand why they don't satisfy your scenario. Jul 9, 2020 at 11:13
• Would you please clarify something. Are you trying to maintain a record of the running average(s), or simply display the current value of the relevant average(s)? Jul 12, 2020 at 4:37

``````=ARRAYFORMULA(