Google-sheets – How to use ARRAYFORMULA to sum every 7 rows

google sheets

I'm trying to use an arrayformula to sum weekly values. I have a table like:

     A         B        C
   date    | value | weekly total
---------------------------------
2016/08/28 |   5   |
2016/08/29 |       |
2016/08/30 |   5   |
2016/08/31 |   5   |
2016/09/01 |   5   |
2016/09/02 |       |
2016/09/03 |       | <should be 20>

I want the C column to calculate the sum of the B column for the past week each Saturday.

I have =ArrayFormula(IF( EQ(weekday(A2:A),7), SUM(B2:B), ""))
I would think there should be a simple way to tell SUM to take from 6 rows back to the current row, but I haven't found anything.

Another option is:
=ArrayFormula(IF( EQ(weekday(A2:A),7), SUMIF(WEEKNUM(A2:A),WEEKNUM(A2:A),B2:B), ""))
but that breaks when there's more than one year on the sheet.

Any ideas?

Best Answer

Phew that actually took me a while to work out. Your formula in cell C9 is:

=if(weekday(A9)=7,SUM(INDIRECT("R[-6]C[-1]",FALSE):INDIRECT("RC[-1]",FALSE)),"")

You can copy that all the way up/down column C.

You end up with this:

A          |   B   |    C
Date       | value | weekly total
---------------------------------
2016/08/28 |   5   |
2016/08/29 |       |
2016/08/30 |   5   |
2016/08/31 |   5   |
2016/09/01 |   5   |
2016/09/02 |       |
2016/09/03 |       |    20
2016/09/04 |   4   |
2016/09/05 |   3   |
2016/09/06 |   6   |
2016/09/07 |       |
2016/09/08 |       |
2016/09/09 |       |
2016/09/10 |       |    13
2016/09/11 |       |
2016/09/12 |       |
2016/09/13 |       |
2016/09/14 |   4   |
2016/09/15 |   3   |
2016/09/16 |   1   |
2016/09/17 |       |    8
...etc.