Search code examples
google-sheetsgoogle-sheets-formula

Select range of cells from last filled cell of a column in google sheets


I'm calculating the average value of the last 1 year. But if I add new rows I need to adapt the function to the new cells of 1 year back. I would like to automate it instead.

How can I select a range of 12 cells from the last filled cell?

There are already many posts that search for the last cell. But it returns the value instead of just use it for selecting the range.

An example sheet below:

Month & Year | amount

average 1y   | =AVERAGE(filter(B$5:B$17,B$5:B$17<>0))

2022/10      | 123
2022/11      | 234
2022/12      | 345
2023/1       | 456 
2023/2       | 567
2023/3       | 678
2023/4       | 789
2023/5       | 890
2023/6       | 901
2023/7       | 234
2023/8       | 345
2023/9       | 456
2023/10      | 567
2023/11      | 678
2023/12      | 789

Solution

  • This will auto-pick the last 12 cell values; in this case B4:B15 of the sample dataset in the screenshot:

    =let(Σ,chooserows(tocol(B:B,1),sequence(12,1,-1,-1)),
           average(filter(Σ,Σ<>0)))
    

    enter image description here