Arrayformula calculation with row() - google-sheets

=OFFSET(INDIRECT($A$4),5+row()-2,4,1,1)
I'm looking to repeat the above formula in all row within the same column, so the calculation will change based on the row(). I tried to put it like below, column H is where the result will be going. But the row() is not following, all result are just using the formula row instead. The $A$4 is a where I'm using to input the range name.
=ARRAYFORMULA(D3:D&OFFSET(INDIRECT($A$4),5+row()-3,4,1,1))
I know I can drag it down, but automatic is always better. Before I'm over complicating the formula, is there a simple solution to make this work?
Updates:
Sample sheet link
https://docs.google.com/spreadsheets/d/1di8a9I0Fv-vuQFDNfV3sPsnWbDuBWTNDYlvUAI5pOCk/edit?usp=sharing

I've added a new sheet ("Erik Help") with two formulas, each of which will return the same results:
=IFERROR(OFFSET(INDIRECT("CLASS_"&$A$4),5+row()-3,4,COUNT(INDEX(INDIRECT("CLASS_"&$A$4))),1),"NO DATA")
-or-
=IFERROR(QUERY({INDEX(INDIRECT("CLASS_"&$A$4),,5)},"Select * WHERE Col1 Is Not Null",0),"NO DATA")
The D3 formula is better, because it is not dependent on the row where the formula is found (i.e., it can be placed anywhere and get the same result). It is "cleaner" (i.e., it has no redundancy of the INDIRECT clause). It's also easier to read and edit if necessary.

Related

Google sheets: How to get the row number of a cell in a different sheet?

Basically, in the expression I'm writing, it searches for a certain text in an interval in a different sheet using VLOOKUP, and if it finds it, it must get the number of the row the text is. I can't find a simple way of doing the second step, since as far as I know, VLOOKUP can only return the searched value, and ROW can only be used to return the line number of the current sheet.
try:
=MATCH(A1; Sheet1!A:A; 0)
where A1 is somewhere in Sheet1 A column
Use this formula
=ArrayFormula(QUERY(IF(Sheet2!A:A=A2,ROW(Sheet2!A:A),"")," Where Col1 is not null",0))

Google Sheet: How to use arrayformula to copy data from one sheet to another?

In a Google spreadsheet, I want to sync A2:G500 in sheet1 to sheet2, I've been aware of the following two methods:
use IMPORTRANGE: put the following formula in A1 of sheet2:
=IMPORTRANGE("spreadsheet_url",sheet1!A2:G500)
It works but it feels like I am overdoing it, besides there seem to be a performance issue
In A2 of sheet2, put formula =sheet1!A2, then drag the formula to G500 in sheet2. This one is intuitive and simple to do. However, it doesn't work if sheet1 is a form response sheet - when new response is added, sheet2 won't automatically get it.
For learning purpose, I'm wondering if there is a way to do this using Arrayformula. Besides, I want to find a way to make this sync more care-free, meaning if there are indefinite rows of data I won't have to go back to this sheet every now and then and change the formula or manually drag the formula. Is this possible? And is Arrayformula the right way to go for this purpose?
I would recommend an { array expression }, like this:
={ Sheet1!A2:G }
This is more or less the same as
=arrayformula(Sheet1!A2:G)
...but I prefer the {} syntax because it allows you to specify non-adjacent columns. For example, you can skip columns D and F like this:
={ Sheet1!A2:C, Sheet1!E2:E, Sheet1!G2:G }
In spreadsheets where the locale uses the comma as decimal mark instead of the period, use a backslash \ instead of comma as horizontal separator.
To skip rows, use the semicolon ; as vertical separator. For example, you can skip rows 2:9 like this:
={ Sheet1!A1:G1; Sheet1!A10:G }
The open-ended range reference A10:G means "columns A to G starting in row 2 and extending all the way to the bottom of the sheet."
You can also leave out the row number to get an open-ended range reference like A:G which means "columns A to G from the very top to the bottom of the sheet." This reference will behave the same as A1:G in almost all situations. I have made it a habit to always include the start row in the reference because that way the formula will automatically adjust in the event a row is inserted above row 1.
When the source sheet is a form responses sheet, another tactic is needed. Form responses are always inserted in newly created rows that cannot be referenced directly in advance.
To avoid the range reference from adjusting when you dynamically copy form responses to another sheet, start the copy from row 1, like this:
={ 'Form Responses 1'!A1:A }
Alternatively, use an array formula, like this:
=arrayformula( 
  if( 
    row('Form Responses 1'!A1:A) = 1,
"Enter column header here", 
    'Form Responses 1'!A1:A
  ) 
)
An even better way to deal with form responses is to aggregate the data directly to whatever reports you need with the query() function.
It's either:
ArrayFormula(Sheet1!A2:G500) for the 499 lines, or
ArrayFormula(Sheet!A2:G) if you wanto sync everything from line 2 down
=ARRAYFORMULA(Sheet1!A:G)
Does this not work?
try in row 1:
={""; INDEX(sheet1!A2:A)}
this will solve your form issues when you use it in 1st row. if you already have something in your row 1 you can add it into double quotes like this:
={"header"; INDEX(sheet1!A2:A)}
in case of multiple columns its like this:
={"","","","","","",""; INDEX(sheet1!A2:G)}

Add title row with ARRAYFORMULA in Google Sheets

I watched a tutorial where the author uses an IF statement along with the ARRAYFORMULA function to add a title row to a column of data. Links are given to the docs; however, for an example of how to use ARRAYFORMULA see this answer.
An example can be seen below:
I was able to populate the C column by placing the following formula in C1:
=ARRAYFORMULA(if(row(A:A) = 1, "spent", B:B - A:A))
I'm confused about the syntax. I understand that X:X references the entire X column but I don't understand how it's being used to check if we're at cell A1 in one context and then being used to apply mass formulas in another context.
How does the above line work?
Can you illustrate with some examples?
It sounds to me that the information you learned led you to expect that row(A:A)=1 translates to row A1?
It works a little different than that, the syntax as your using it now, is basically saying if any row in A:A has a value of 1, then write "spent" else subtract B-A
My suggestion:
use a literal array to make your header, then use the if(arrayformula) to only populate rows with values, for aesthetics:
Example:
={"Spent";arrayformula(if(isnumber(A2:A),B2:B-A2:A,))}
Explanation:
The {} allow you to build a literal array, and using a semicolon instead of a comma allows you to stack your cells vertically, following that we check if there is a value in column A, if so, subtract A from B, else leave it blank.
why not just put the column title directly on the first row cell, and start the array formula from the 2nd row, using the A2:A, B2:B syntax?
If something does not have to be in a formula, better put it directly on the cell - simpler for others to understand what's going on, and the formula will be simpler.
If you put the array formula in line 2, and someone sorts the data, then the arrayformula will move. If it is in the header line, this is less likely to happen.
You can also use the IFS function to achieve a similar effect to the array,
=arrayformula(ifs(row(A1:A)=1,"Spent",A1:A="",,True,B1:B-A1:A)
Here the first condition checks the row number, and if it is row ONE, then inserts a Column Header.
The Second condition - A1:A="",, - ensures that blank lines are ignored.
The Third condition True (ELSE) performs the calculation.
This method also allows for different calculations to performed on different rows depending on requirements.

Google Sheets Auto Repeat Using arrayformula()

Based on this sample data, I have this formula which gives me the accurate results with one major problem... It does not auto-populate to new rows
=arrayformula(if(countif(filter(G$2:G,A$2:A=A2,B$2:B=B2),">"&G2)>0,countif(filter(G$2:G,A$2:A=A2,B$2:B=B2),">"&G2),COUNTIF(filter(E$2:E,A$2:A=A2,B$2:B=B2),"Finished")))
I have tried this formula to see if it will auto-populate to the new rows...
=arrayformula(IF(ISNA(A2:A),,if(countif(filter(G$2:G,A$2:A=A2,B$2:B=B2),">"&G2)>0,countif(filter(G$2:G,A$2:A=A2,B$2:B=B2),">"&G2),COUNTIF(filter(E$2:E,A$2:A=A2,B$2:B=B2),"Finished"))))
...the above formula does auto-populate; however, every value is 1 instead of the correct value.
I tried a simple formula which does not do everything the above does but might help troubleshoot. I was under the suspicion that the above formula was only displaying results of the first row over and over. To test I tried this formula...
=arrayformula(IF(ISNA(A2:A),,indirect("g"&ROW(indirect("g2:g"&counta(G2:G))))))
...it turns out that the above formula does display the results from G2 into each row. If I could figure out the reason why, I am sure I could take the concept from the solution to this simple formula and add it to the above more complex one.
Please, try this formula:
=ArrayFormula(if(
mmult(
--(A2:A=TRANSPOSE(A2:A))*
--(B2:B=TRANSPOSE(B2:B))*
--(G2:G<TRANSPOSE(G2:G)),
row(A2:A)^0)>0,
mmult(
--(A2:A=TRANSPOSE(A2:A))*
--(B2:B=TRANSPOSE(B2:B))*
--(G2:G<TRANSPOSE(G2:G)),
row(A2:A)^0),
mmult(
--(A2:A=TRANSPOSE(A2:A))*
--(B2:B=TRANSPOSE(B2:B)),
--(E2:E="Finished"))
)
)
Caution! It works slow, so it's better to delete blank rows in the worksheet. Even better to use this formula in 2 steps. Step 1 formula:
=ArrayFormula(mmult(--(A2:A=TRANSPOSE(A2:A))*
--(B2:B=TRANSPOSE(B2:B))*
--(G2:G<TRANSPOSE(G2:G)),
row(A2:A)^0))
And step 2 formula:
=ArrayFormula(mmult(--(A2:A=TRANSPOSE(A2:A))
*--(B2:B=TRANSPOSE(B2:B)),
--(E2:E="Finished")))
Open ranges overload this formula. It also could work faster if you use:
offset(E2,,,counta(E2:E)) instead of E2:E

Google Spreadsheet sum which always ends on the cell above

How to create a Google Spreadsheet sum() which always ends on the cell above, even when new cells are added? I have several such calculations to make on each single column so solutions like this won't help.
Example:
On column B, I have several dynamic ranges which has to be summed. B1..B9 should be summed on B10, and B11..B19 should be summed on B20. I have tens such calculations to make. Every now and then, I add rows below the last summed row , and I want them to be added to the sum. I add a new row (call it 9.1) before row 10, and a new raw (let's call it 19.1) before row 20. I want B10 to contain the sum of B1 through B9.1 and B20 to contain the sum of B11:B19.1.
On excel, I have the offset function which does it like charm. But how to do it with google spreadsheet? I tried to use formulas like this:
=SUM(B1:INDIRECT(address(row()-1,column(),false))) # Formula on B10
=SUM(B11:INDIRECT(address(row()-1,column(),false))) # Formula on B20
But on Google Spreadsheet, all it gives is a #name error.
I wasted hours trying to find a solution, maybe someone can calp?
Please advise
Amnon
You are probably looking for formula like:
=SUM(INDIRECT("B1:"&ADDRESS(ROW()-1,COLUMN(),4)))
Google Spreadsheet INDIRECT returns reference to a cell or area, while - from what I recall - Excel INDIRECT returns always reference to a cell.
Given Google's INDIRECT indeed has some hard time when you try to use it inside SUM as cell reference, what you want is to feed SUM with whole range to be summed up in e.g. a1 notation: "B1:BX".
You get the address you want in the same way as in EXCEL (note "4" here for row/column relative, by default Google INDIRECT returns absolute):
ADDRESS(ROW()-1,COLUMN(),4)
and than use it to prepare range string for SUM function by concatenating with starting cell.
"B1:"&
and wrap it up with INDIRECT, which will return area to be sum up.
REFERRING TO BELOW ANSWER from Druvision (I cant comment yet, I didn't want to multiply answers)
Instead of time consuming formulas corrections each time row is inserted/deleted to make all look like:
=SUM(INDIRECT(ADDRESS(ROW()-9,COLUMN(),4)&":"&ADDRESS(ROW()-1,COLUMN(),4)))
You can spare one column in separate sheet for holding variables (let's name it "def"), let's say Z, to define starting points e.g.
in Z1 write "B1"
in Z2 write "B11"
etc.
and than use it as variable in your sum by using INDEX:
SUM(INDIRECT(INDEX(def!Z:Z,1,1)&":"&ADDRESS(ROW()-1,COLUMN(),4))) - sums from B1 to calculated row, since in Z1 we have "B1" ( the 1,1 in INDEX(...,1,1) )
SUM(INDIRECT(INDEX(def!Z:Z,2,1)&":"&ADDRESS(ROW()-1,COLUMN(),4))) - sums from B11 to calculated row, since in Z2 we have "B11" ( the 2,1 in INDEX(...,2,1) )
please note:
Separate sheet named 'def' - you don't want row insert/delete influence that data, thus keep it on side. Useful for adding some validation lists, other stuff you need in your formulas.
"Z:Z" notation - whole column. You said you had a lot of such formulas ;)
Thus you preserve flexibility of defining starting cell for each of your formulas, which is not influenced by calculation sheet changes.
By the way, wouldn't it be easier to write custom function/script summing up all rows above cell? If you feel like javascripting, from what I recall, google spreadsheet has now nice script editor. You can make a function called e.g. sumRowsAboveMe() and than just use it in your sheet like =sumRowsAboveMe() in sheet cell.
Note: you might have to replace commas by semicolons
NOTE
After testing this answer, it will only work if the sum is in a different column due to a circular dependency error. Otherwise, the solution is valid.
It's a bit of algebra, but we can take advantage of Spreadsheets' lower right corner drag.
=SUM(X:X) - SUM(X2:X)
Where X is the column you are working with and X2 is your ending point. Drag the formula down and Sheets will increment the X2, thus changing the ending point.
*You mentioned that you had tens of such calculations to make. So in order to fit your exact need, we would subtract your last summation to get that "middle" range that we wanted.
e.g.
B1..B9 should be summed on B10, and B11..B19 should be summed on B20
Because of the circular dependency error mentioned earlier, I can't solve it exactly and put the sum on the same line, but this could work in other cases where the sum needs to be stored in a different column.
=SUM(B:B) - SUM(B9:B) //Formula on C10 (Sum of B1..B9)
=SUM(B:B) - SUM(B19:B) - B10 // Formula on C20 (Sum of B11..B19)
This is based on #PsychoFish, here is the solution:
=SUM(INDIRECT(SUBSTITUTE(ADDRESS(1,COLUMN(),4),"1","")&"3:"&ADDRESS(ROW()-1,COLUMN(),4)))
Simply replace the "3:" for the row to start sum.
#PsychoFish is correct but cannot be dragged and copied since the column is literal and hard coded, and #Druvision was in the right direction but was wrong... basically ended up with the same issue of having to re-enter the ranges and then sliding the formulas over and over.
You guys are making this harder than you have to. I just leave a couple of empty rows above by "sum" row (you can format them to be filled with color or something to keep them from being inadvertently used), then just add your new rows just above those special rows.
Agree with what user7255446 said that everyone is overcomplicating. Keep one row blank before your sum row. And then whenever you want to insert a new row, click on your blank row and use "Insert row ABOVE" instead of "insert row below". Your sum formula will automatically adjust.
Example: I want to sum from B1 to B19. I leave row 20 blank. In cell B21, put =SUM(B1:B20). Then if you ever need to insert a new row, click on row 20 and choose "Insert row above". The sum formula automatically changes to =SUM(B1:B21) for you. And of course your sum cell is now B22.
General syntax:
=SUM(INDIRECT(cell_reference_as_string1 &":"& cell_reference_as_string2)
with for example:
cell_reference_as_string1 = ADDRESS(ROW(),COLUMN(),4)
cell_reference_as_string2 = ADDRESS(ROW()-1,COLUMN(),4)
I like how #abernier describes the general solution. So far only alphabet-based A1 notation (A being first column, 1 being first row) are being used. It keeps confusing me, especially when thinking of number of columns left of another column. I like the number-based R1C1 notation much better. To use R1C1 notation for INDIRECT, you need to pass FALSE like so:
=SUM(INDIRECT("R1C"&COLUMN()&":R"&(ROW()-1)&"C"&COLUMN(), FALSE))
I hope you find that helpful, too.
OFFSET() can be used/abused for this purpose. Give it the absolute address of the top left of the range, 0 and 0 for the row/column offsets, and the height/width of the range. Let OFFSET() be the argument to SUM(), SUMIF(), etc.
ROW() and COLUMN() are handy when computing the desired height/width. Be sure to remember to subtract one to exclude the current row/column, or else you're liable to end up with a circular reference. If you have header rows/columns, subtract for them too.
For example, to sum everything from A2 down, excluding the current row, try:
=SUM(OFFSET($A$2,0,0,ROW()-2,1))
To sum everything to the left of the current cell, wherever it may be, try:
=SUM(OFFSET(INDIRECT("RC1",FALSE),0,0,1,COLUMN()-1))
Now let's flip things upside down, to show that this works in the other direction. Suppose you want to sum the B column, starting below the current row, until (and including) row #10. Try this:
=SUM(OFFSET($B$10,ROW()-9,0,10-ROW(),1))
You can avoid negative offsets, while still summing column B:
=SUM(OFFSET(INDIRECT("RC2",FALSE),1,0,10-ROW(),1))
Remove the "2" to instead sum the current column:
=SUM(OFFSET(INDIRECT("RC",FALSE),1,0,10-ROW(),1))
(Credit to Tom Sharpe, who commented above.) INDEX() can be used in a range expression. You might prefer this over OFFSET(), so I'm putting it here. The following sums everything from G1 down to the row above the current:
=SUM(G1:INDEX(G:G,ROW()-1))
Here's how I do it.
This formula does not require you to edit or enter anything about the particular column you would like to sum
=SUM(INDIRECT(CONCATENATE(address(1,column(),4),":",LEFT(address(1,column(),4),1))&ROW()-1))
The answer by #PsychoFish led me in the correct way.
The only issue that I had to rewrite the formula again from each column and each sum. So here is the improved formula, which sums the previous 9 cells on the same column, without hardcoding the column or row numbers:
=SUM(INDIRECT(ADDRESS(ROW()-9,COLUMN(),4)&":"&ADDRESS(ROW()-1,COLUMN(),4)))
The only issue is that I had to rewrite the formulas if someone adds or deletes a row. In this case I should change 9 to 10 or 8 corrspondingly.

Resources