Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the range of the last X rows in Google Sheets

Say I have a spreadsheet with 18 rows, and I can hard code the range of the last 7 values in column D and average their values like so:

=AVERAGE(D12:D18)

How then could I do the same without hard coding them, so it'll work as I add more rows?

like image 373
Ashley Williams Avatar asked Dec 09 '22 09:12

Ashley Williams


1 Answers

If you want to calculate the average or sum, there's no need for a script. You can accomplish this with an array filter as well. The following formula calculates the average over the last 7 rows in the A column:

=AVERAGE(FILTER(A:A;ARRAYFORMULA(ROW(A:A)>COUNT(A:A)-7+1)))

This assumes that the data starts at row 1. Otherwise you have to change the latter constant in the formula to the row number where the data starts.

like image 72
Bram Schoenmakers Avatar answered Jan 13 '23 13:01

Bram Schoenmakers