Arrayformula sum one column until this row - google-sheets

I'm trying to make an array formula which sums up all the rows until this row.
For clarification column a will be the input and column b would be the output. I'm looking for a way to do this with an arrayformula.
a1:1 b1:1a2:2 b2:3a3:5 b3:8a4:3 b4:11
I tried to use =ARRAYFORMULA(SUM(INDIRECT("F1:"&ADDRESS(ROW(),COLUMN(F2:F))))) but this doesn't work.

How about
=arrayformula(sumif(row(A1:A4),"<="&row(A1:A4),A1:A4))
The sumif is evaluated separately for each value in the criteria part so:
In the first row of the output array you have
=sumif(row(A1:A4),"<=1",A1:A4)
giving you just the first row of column A.
In the second row of the output array you have
=sumif(row(A1:A4),"<=2",A1:A4)
giving you the sum of the first 2 rows and so on.

Since OP changed the question with a clarification, A different answer is submitted below:
B1:
=ARRAYFORMULA(MMULT(transpose(A1:A5)*--IF(row(1:5),COLUMN(A:E)<=row(1:5)),ROW(1:5)^0))

Related

Sum every n columns starting at the 1st column in the range

In Excel/Google Sheets I have found how to sum every N columns on websites such as https://exceljet.net/formula/fixed-value-every-n-columns, but the problem is, from what I can see is that it starts at N column each time. I need something that starts from column 1 and then counts every N columns. like the following:
I need to do this with a formula and not a script.
With Google-Sheets, try:
Formula in M2:
=SUM(QUERY(TRANSPOSE(A2:J2),"Skipping "&L2))
Or, a single dynamic array formula (without dragging):
=INDEX(MMULT(A2:J4*(MOD(COLUMN(A2:J4),L2:L4)=1),SEQUENCE(10,1,1,0)))
Or, more dynamic:
=INDEX(MMULT(A2:J4*(MOD(COLUMN(A2:J4),L2:L4)=1),SEQUENCE(COLUMNS(A2:J4),1,1,0)))
Note: The latter would also work in Excel with slight modifications.
Google sheets formula:
=SUM(FILTER(A2:J2, MOD(A2:J2, L2)=1))
then drag to other cells
or use this array version:
Array version:
=INDEX(TRANSPOSE(MMULT(A2:J4,TRANSPOSE(COLUMN(A2:J4)^0 *
N(MOD(COLUMN(A2:J4), L2:L4)=1)))), ,1)
If you want the cells that were added to be automatically highlighted.
Conditional formatting used on A2:J:
=MOD(COLUMN(), $L2)=1
In M2:
=SUMPRODUCT(A2:J2,N(MOD(SEQUENCE(,COLUMNS(A2:J2),0),L2)=0))
and copied down.
Try this formula on column M:
=SUM((sumif(ArrayFormula(mod((COLUMN(B2:J2)-COLUMN(B2)+1),L2)),0,B2:J2))+A2)
Here's the result on Column M.
Just to break down the code sumif(ArrayFormula(mod((COLUMN(B2:J2)-COLUMN(B2)+1),L2)),0,B2:J2) does the actual calculation with the number of intervals set on Column L but take note that I started at the 2nd column so the range here does not include the first column. The result from this is at the Column O highlighted red as you can see in the screenshot.
At the Column M is the actual solution where I only added the first column by using SUM on top of the previous formula.
I hope my explanation is clear.
Just copy/drag the formula down to each row and it should work.
Reference: How to Sum Every Nth Row or Column in Google Sheets Using SUMIF

Is there a way to use an array formula to keep my formula (to add specific columns) when rows are inserted?

I'm setting up a spreadsheet that has specific columns summed in each row, but I need the formula to be included when a row is inserted.
The current formula also includes a statement to make a 0 value, if a check box is checked in the last column:
=IF(T2=FALSE, SUM(I2,K2,L2,M2,N2,O2), 0)
Is there a way I can do this using an array formula?
Here is a formula which will give a sum for columns I to O in each row, ignoring column J:
=ArrayFormula(if(I2:I="","",if(T2:T<>FALSE,0,I2:I+sumif(row(K2:O)+0*column(K2:O),row(K2:O),K2:O))))
but this assumes all rows that have data will have a number in column I.
If this isn't the case, you could go on to test columns individually like this:
=ArrayFormula(if((I2:I="")*(K2:K=""),"",if(T2:T<>FALSE,0,I2:I+sumif(row(K2:O)+0*column(K2:O),row(K2:O),K2:O))))
and so on up to column O if necessary, or maybe column T is always completed and you could test that - it depends how your data actually looks.
Note 1
row(K2:O)+0*column(K2:O)
is necessary to generate an array which is has the same dimensions as K2:O as required by SUMIF.
Note 2
There's also the MMULT approach to getting the row sums as demonstrated here
={"AAA"; ARRAYFORMULA(IF(LEN(T2:T), IF(T2:T=FALSE, I2:I+K2:K+L2:L+M2:M+N2:N+O2:O, 0), ))}

Flag Non-Unique Rows (After the First Instance)

I have a Google Sheets spreadsheet where I am needing to create an array formula that will determine uniqueness and flag non-unique rows. I need it to flag non-unique rows but only the second & subsequent duplicates (the first duplicate will not be flagged and should say "Unique"). I have this formula but it includes the first duplicate.
={"Unique";
ArrayFormula(
IFS(
$C$2:$C="","",
$C$2:$C<>"", IF(COUNTIF($A$2:$A,$A$2:$A)>1,"Not Unique","Unique")
)
)
}
How can I modify this formula to not flag the first instance of a non-unique row?
Your formula looks very strange to me, perhaps try:
=ArrayFormula(IF($C$2:$C="","",IF(COUNTIF($A$2:$A$100,A2:A100)=1,"Unique","Not Unique")))
It'll need a Row number helper column:
J1 =ARRAYFORMULA(row(A:A))
Then, the magic formula, where 10 is the column ID for the helper column
=ARRAYFORMULA(if(VLOOKUP(A:A,A:J,10,false)=row(A:A),"Unique","Not Unique"))
The vlookup returns the first row number in the helper column where the value in A:A is found and compares it to the currently calculated row.

CONCATENATE cells in the column with ARRAYFORMULA

CONCATENATE cells in the column which separated by empty cells using ARRAYFORMULA. I have an idea but am not sure it is right. First of all get row number of first and last names for each day. Combine them in formula and finally put that formula to ARRAYFORMULA.
D2= =ARRAYFORMULA(IF(B2:B<>"",ROW(B2:B)+1,""))
E2= =ARRAYFORMULA(IF(C2:C<>"",IF(C3:C="",ROW(B2:B),""),""))
So the question is how to get the row number of the last name for each day in the same row as a day and how to combine this number to a formula which can be compatible with ARRAYFORMULA.
Example:
To get to the result (with steps in between) try (in a empty column in row 2)
=ArrayFormula(If(right(B2:B, 3)="day", offset(B2:B, 1,1)&", "&offset(B2:B, 2,1),))
and see if that works ?

How to use INDEX() inside ARRAYFORMULA()?

I am trying to use the INDEX() formula inside an ARRAYFORMULA(). As a simple (non-sense) example, with 4 elements in column A, I expected that the following array formula entered in B1 would display all four elements from A in column B:
=ARRAYFORMULA(INDEX($A$1:$A$4,ROW($A$1:$A$4)))
However, this only fills field B1 with a the value found in A1.
When I enter
=ARRAYFORMULA(ROW($A$1:$A$4))
in B1, then I do see all numbers 1 to 4 appear in column B. Why does my first array formula not expand similar like the second one does?
The INDEX function is one that does not support "iteration" over an array if an array is used as one of its arguments. There is no documentation of this that I know of; it simply is what it is. So the second argument will always default to the first element of the array, which is ROW(A1).
One clumsy workaround to achieve what you require relies on a second adjacent column existing next to the source data* (although it is unimportant what values are actually in that second column):
=ArrayFormula(HLOOKUP(IF(ROW($A$1:$A$4);$A$1);$A$1:$B$4;ROW($A$1:$A$4);0))
or indeed something like:
=ArrayFormula(HLOOKUP(IF({3;2;4;1};$A$1);$A$1:$B$4;{3;2;4;1};0))
edit 2015-06-09
* This is no longer a requirement in the newest version of Sheets; the second argument in the HLOOKUP can just be $A$1:$A$4.
Here is a tip for using vlookup with an array, so that even if the columns are moved later on the formula will still work correctly....
In general, configure the vlookup so that it's reading only 2 columns and returning the second. This can be done by inputting only the 2 columns required, rather than a range and column index.
Example:
Replace the following formula which would fail if columns are moved
=arrayformula( vlookup(C:C, booking!$A:$E ,5 ,false) )
with this formula which will continue to work even if columns are moved
=arrayformula( vlookup(C:C, {booking!$A:$A,booking!$E:$E} ,2 ,false) )
Note, you can also simulate the index function using vlookup.
Example:
Column R:R contains the row index numbers for looking up data in column booking!$A:$A
=arrayformula(vlookup(R:R ,arrayformula({row(booking!$A:$A), booking!$A:$A}),2 , false))
It's a nested array, so it can be helpful to test in stages, eg just the inner part for one example, eg return entry in row 10:
=vlookup(10 ,arrayformula({row(booking!$A:$A), booking!$A:$A}),2 , false)

Resources