Creating a recursive Formula in Google Sheets - google-sheets

So here's what I'm trying to do: I have a query formula on Sheet 3 that pulls data from Sheet 2, based on values from Sheet 1. Then I add a header/footer row before/after the data. What I want to do is re-run that formula block for each row in Sheet 1 Column A that has data, but in each instance increase the value of the row by one, and stop when the rows are empty.
Can't just do it with a single query with grouping/ordering, because of the headers/footers.
So far, I've got this (just the first two cycles):
`={
{Sheet1!A3,"","","","","",""};
query(Sheet2!D:S, "select D,F,H,J,N,P,R where J contains '"&Sheet1!A3&"' order by F",0);
{"TOTAL","","","","","",Sheet1!B3};
{"","","","","","",""};
{Sheet1!A4,"","","","","",""};
query(Sheet2!D:S, "select D,F,H,J,N,P,R where J contains '"&Sheet1!A4&"' order by F",0); {"TOTAL","","","","","",Sheet1!B4}
}`
The result is this:
screenshot
It works great, but it'll be super tedious to repeat dozens of times.
This is pushing against the bounds of my formula knowledge, so I need someone to point me in the right direction. Is this handled with some sort of INDEX or VLOOKUP or ARRAY function? Or would it require a script?

Could you try with this REDUCE option and let me know if it returns your desired result?
=REDUCE({"","","","","","",""},SEQUENCE(COUNTA(Sheet1!A3:A)),LAMBDA(a,b,{a;
{INDEX(Sheet1!A3:A,b),"","","","","",""};
query(Sheet2!D:S, "select D,F,H,J,N,P,R where J contains '"&INDEX(Sheet1!A3:A,b)&"' order by F",0);
{"TOTAL","","","","","",INDEX(Sheet1!B3:B,b)}}))
You may change the first argument all those "","","" with the headers from your table. Let me know if it's useful

Related

Google Sheets Filter + Match? Not sure

I have two tabs in a sheet, that have different ranges. One is preset data, the other is an import from a program we use. I'm trying to figure out a way, if possible, to pull specific data from sheet 1, and match it with values in sheet two, that aren't sorted the same.
Example sheet
https://docs.google.com/spreadsheets/d/1OsSWQ_48VrcTU3pXGeJ_1syluKPVeVRune39UA9I3x4/edit?usp=sharing
I'm trying the formula =sort(filter(Sheet1!B1:B,match(B1:B, Sheet1!B1:B ,0)),2,TRUE), but it's just putting the results in order. If I replace the column # on the sort with 1, it sorts it out of order but it doesn't match column B, like I need it to.
The documents i'm working with are 2000+ rows each, I'd rather not manually do this if at all possible.
Please assist? Either way, thank you for reading.
Try:
=arrayformula(iferror(vlookup(MATCH( B1:B,Sheet1!$B$1:B,0),{(ROW(Sheet1!A1:A)),Sheet1!A1:A},2,0)))
Explanation
step#1 =arrayformula(MATCH( B1:B,Sheet1!$B$1:B,0)) will give the row where each value column B will be found in Sheet1!column B
step #2 =arrayformula({ROW(Sheet1!A1:A),Sheet1!A1:A}) will build a virtual matrix whith in column#1 the row and in column#2 the value of Sheet1!column A
finally: join the two formulas as vlookup(___step#1____,____step#2______)

Arrayformula is overwriting the results of the subsequent rows instead of populating their own values

The below formula is working
=query(INDIRECT(F23&"!A4:G"),"SELECT G WHERE A = '"&Overview!A23&"'")
When i convert it to Array formula like this it overwrites the results of next rows.
=query(INDIRECT(F23&"!A4:G"),"SELECT G WHERE A = '"&Overview!A23&"'")
How can we write array formula for this scenario? Note: Here we are going to different sheets and finding the value.
INDIRECT(), does not iterate within ARRAYFORMULAS. If you want to use Indirect you will have to drag it down.

Transpose a table row by row

I need to transpose parts of a table row by row. The following example illustrates what the result needs to look like:
I tried different combinations of arrayformula(), flatten() and transpose(), succeeded with getting the last column right with =arrayformula(FLATTEN(B4:C)), but need now help with fixing this.
Link to table
Try this in row 2 of the example screenshot:
={flatten({A2:A,A2:A}),arrayformula(flatten({if(A2:A<>"",B$1,),if(A2:A<>"",C$1,)})),flatten({B2:C})}
Or if you want the column headings, put this in row 1:
={"A","B","C";flatten({A2:A,A2:A}),arrayformula(flatten({if(A2:A<>"",B$1,),if(A2:A<>"",C$1,)})),flatten({B2:C})}
The new column 'C' ends up being a mixed data type, so be careful if you run a future query on these results as it doesn't like mixed data.
For local implementation (as per your initial screengrab, EU locale file), try this in cell E4 - since the formula sits in row 4, the array range needs to go from 4 (A4:A):
={flatten({A4:A\A4:A})\arrayformula(flatten({if(A4:A<>"";B$3;)\if(A4:A<>"";C$3;)}))\flatten({B4:C})}
Alternatively, if you want column headings, try this in cell E3 - the array range is still A4:A because "A"\"B"\"C"; puts headings in row 3, ; is a return, then the rest of the formula targets data from row 4 down:
={"A"\"B"\"C";flatten({A4:A\A4:A})\arrayformula(flatten({if(A4:A<>"";B$3;)\if(A4:A<>"";C$3;)}))\flatten({B4:C})}
If you want to limit the array range to a specific row rather than working down the entire sheet (eg. row 20), then A4:A would need to be A4:A20.
This is a basic SPLIT(FLATTEN( problem.
Arrayformula() can always exist on the outside of a formula and will apply to the whole thing.
This is on a new tab in your sample called MK.Help:
=ARRAYFORMULA(QUERY(TO_TEXT(SPLIT(FLATTEN(Data!A2:A&"|"&Data!B1:C1&"|"&Data!B2:C);"|";0;0));"where Col1<>''"))

Troubleshooting formula with array - array arguments are of different size to EQ

In Google Sheets, I have a formula that displays the value of an item in a row if one of its cells contains any of the values listed in a different sheet. It looks like this:
=ARRAYFORMULA(IF(OR(L2 = ZRSKUs!$A$1:$Z$12005), O2, "0"))
If L2 contains any of the values in sheet ZRSKUs, this formula displays the value of the item, which is held in O2. If I drag the formula down it produces the value of every column and I can then get a SUM of this column. I wanted a way to do this without having to drag the formula down every single row (this spreadsheet has about 20,000 rows so it takes a long time to do). I also wanted the formula to add it up too, so it is all done in one cell.
I tried editing the formula to do this, and this is what I came up with:
=ARRAYFORMULA(SUM(IF(OR($L3:$L = ZRSKUs!$A$1:$A$500), $O3:$O, "0")))
However, this gives me an "Array arguments to EQ are of different size" error. I tried adjusting the number of rows in the ZRSKUs sheets so it had the exact same number as my other sheet, but this made no difference.
I'm not sure what's going wrong, so any help or advice would be greatly appreciated!
You get the error because that is not a well-formed array formula, as $L3:$L and ZRSKUs!$A$1:$A$500 are not equal in length. We could rectify this by using another function for the lookup, in this case, MATCH:
=ARRAYFORMULA(SUM(IF(ISNA(MATCH($L$1:$L, ZRSKUs!$A$1:$A$500, 0)), 0, $O$1:$O)))

Google Sheets: Lookup last matching values but with expanding rows

I'm using a dynamic, expanding sheet containing form responses. Search keys are in A2:A, responses are in the Responses sheet. So,
To get an expanding VLOOKUP, I do:
=ARRAYFORMULA(VLOOKUP(A2:A, Responses!A2:C, 3, 0))
To get the last matching value (for example, latest timestamp) of a repeating search key, I use a combination of FILTER, MAX, and INDEX like so:
=INDEX('Responses'!C2:C, MAX(FILTER(ROW('Follow-up Responses'!A2:A), 'Responses'!A2:A=A2)))
And this works by just dragging it down, but is there a way to make this expanding in an ARRAYFORMULA function? I tried modifiying it as ranges but all I get is one row. My form responses are added quickly so there may come a time where the formula has not been dragged down to the most current.
Thanks!
Sample formula with lookup of last value using row:
=VLOOKUP(D2 ; SORT(FILTER({$A:$B\ROW($A:$A)};$A:$A<>"");3;0) ; 2;)
D2 -- search value
A:B -- initial
row(A:A) -- any row for getting last values in the top.

Resources