I have a column D with numerical values and on an adjacent column, I want to display the sum for the last 7 rows relative to the current row.
I've tried a couple of variants, most of them inspired by StackOverflow and other sites but they haven't worked as I need them.
I figured that since Offset() creates a range it would be simple to just "slice" the range I need and feed it to sum()
ArrayFormula( Sum(Offset(D7:D500,-6,1,7,1)) )
However, it seems that it only calculates the cell I place this formula in.
I adapted a potential solution I found online to my situation
ArrayFormula(SUMIF(ROW(D7:D500),"<="&ROW(D7:D500),D7:D500))
This one calculates the sum for all the previous rows, so I figured using SUMIFS() would get me what I need, therefore
=ArrayFormula(SUMIFS(D7:D500, ROW(D7:D500),"<="&ROW(D7:D500), ROW(D7:D500),">"&ROW(D7:D500)-7))
But this one just doesn't work. I've also tried creating the range with Address() and Interpret() but it seems they don't work properly inside ArrayFormula()
I'm fairly new at it so it might just be that I'm not using them properly. What am I doing wrong here? Is there a better way of doing this? I would rather use ArrayFormula() if possible since the formula will live in a single cell.
You can accomplish this via a custom function created in Google Apps Script. To achieve this, follow these steps:
function SUM7ROWS(input) {
var output = [];
var sum;
for (var row = 0; row < input.length; row++) {
sum = 0;
for (var i = 6; i >= 0; i--) {
if (row - i >= 0) {
sum = sum + Number(input[row - i]);
}
}
output.push(sum);
}
return output;
}
D7:D500
), as you can see here:I hope this is of any help.