Short answer
Alternative 1
- Use the system 2 for week numbering.
Alternative 2
- Use MMULT instead of SUM to do running total operations, but be careful on using it with open-ended references as they could make that spreadsheet recalculation takes a lot of time or even to break the spreadsheet.
Explanation
ARRAYFORMULA usually is used to get multiple values (an array), but not all the functions works with ARRAYFORMULA. This is the case of SUM as it returns the same value for all the rows of the resulting array.
Tweek of the second OP's formula
The following formula returns 0 for rows below the Table
=ArrayFormula(IF( EQ(weekday(A2:A),7), SUMIF(WEEKNUM(A2:A),WEEKNUM(A2:A),B2:B), ""))
As the OP mentioned, the above formula doesn't work when the table has data along more than one year. This occurs because the week numbers repeats each year and the last days of the year and the first year of the next year could belong to the same week. Fortunately WEEKNUM has two systems for week numbering. The System 2:
The first week of the year is considered to be the week containing the first Thursday of the year, which is numbered as week 1. System 2 is the approach specified in ISO 8601, also known as the European system for numbering weeks.
By the other hand, by adding FILTER we could avoid the 0
; also ""
could be removed to get a blank cell instead of a cell with 0 length string.
The tweaked formula is the following:
=ArrayFormula(FILTER(IF( EQ(weekday(A2:A),7), SUMIF(WEEKNUM(A2:A,21),WEEKNUM(A2:A,21),
B2:B), ),LEN(A2:A)))
Use of MMULT
MMULT could be used to do running total operations. For details on this, please checkout MMULT Usage, a spreadsheet by Adam Lusk.
Assuming that
- the Table is a continuous range
- there is no other data below the Table
- the Table headers are on Row 1
- the Table values start on Row 2
- the dates are continuous, start on Sunday and end on Saturday
- the data on Column A are values of type date, not strings
Adding following formula to C2, it will calculate automatically the weekly total the row corresponding to each Saturday.
=ARRAYFORMULA(IF(FILTER(MOD(ROW(A2:A)-1,7),LEN(A2:A))<>0,,
MMULT(ARRAY_CONSTRAIN(IF(N(MOD(ROW(A2:A)-1,7)=0)*TRANSPOSE(N(MOD(ROW(A2:A)-1,7)>=0))*
N(ROW(A2:A)-1>=TRANSPOSE(ROW(A2:A)-1))-(N(ROW(A2:A)-1>=TRANSPOSE(ROW(A2:A)-1+7)))>0,1,0),
COUNT(A2:A),COUNT(A2:A)),FILTER(IF(B2:B="",0,B2:B),LEN(A2:A)))))
To avoid that the use of open-ended references make troubles, FILTER and ARRAY_CONSTRAIN were included in the formula.
MOD, ROW, TRANSPOSE, compare operators (=
,<>
,>=
,>
), multiply (*
) and subtraction (-
) operators are used make several matrixes which are used to identify which values will be include in the weekly total calculations and the row to put each one.
Multiplication is used instead of AND because it doesn't work on array formulas.