Transpose a table row by row - google-sheets

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<>''"))

Related

Implement formula in a column based on contents of each cell

In my Google Sheet, I have 1000+ rows of Date entries. For each Date, I am calculating the Month# and Week# using MONTH() and WEEKDAY() functions respectively.
Here is the link to a sample file: https://docs.google.com/spreadsheets/d/1Af5-pYMFWZ1QtLoaAbPZYMGRvk43JBslUp4KyOFADfA/edit?usp=sharing
Problem Statement:
For all rows which have a unique Month# and Week#, I would like to implement a formula and calculate Output. For example, in my sheet, rows 3 to 6 pertain to Month=1 and Week=4. For this set of 5 rows I am calculating Output column as the subtraction from the first element in that set (ie... C3-$C$3, C4-$C$3, C5-$C$3 so on ). Similarly row 7 to 10 pertain to Month=1 and Week=5, and so I calculate Output
as Data-$C$7 and so on.
How do I implement this structure to calculate Output column on each set of unique Month# and Week# values?
Delete everything from Column F (including the F2 header). Then place the following formula into cell F2:
=ArrayFormula({"Output";IF(C3:C="",,IFERROR(C3:C-VLOOKUP(E3:E,{E3:E,C3:C},2,FALSE)))})
This one formula will create the header and return results for all valid rows.
Since VLOOKUP always finds only the first matching instance of what it is looking up, we can use it to ask that each value in C3:C subtract that first instance of where week-number match for each row.
By the way, although you didn't ask about this, you can also use this type of array formula in Columns D and E, instead of all of the individual formulas you have. To do that, delete everything from Columns D and E (including the headers). Then...
Place the following formula in D2:
=ArrayFormula({"Month #";IF(B3:B="",,MONTH(B3:B))})
... and the following formula in E2:
=ArrayFormula({"Week #";IF(B3:B="",,WEEKNUM(B3:B))})

Google Sheets Fill Down with Formula

I have a very hard problem to solve, which must be completed with a formula (not a script).
Basically, the Raw input column needs to be dynamically filled down until it hits the next piece of text.
Here's an example file with includes the expected output.
https://docs.google.com/spreadsheets/d/1ibqCvY39NlhCRWsbBdxKITUUpVpp9wXdEz44T-pHDY0/
Is it even possible to achieve?
Thanks
This will work based on your ask, assuming that A2 is never blank, place this in the first row of data (not header):
=ArrayFormula(IF(A2:A<>"", A2:A, B1:B))
It checks to see if there is a value in column A, if there is, it fills that column, if not, it copies the cell above.
Delete everything in Column B (including the header) and place the following formula in B1:
=ArrayFormula({"Header";VLOOKUP(FILTER(ROW(A2:A),ROW(A2:A)<=MAX(FILTER(ROW(A2:A),A2:A<>""))),FILTER({ROW(A2:A),A2:A},A2:A<>""),2,TRUE)})
Here is a basic explanation of how this formula works:
A virtual array is created between the curly brackets { }; this virtual array contains a header and all results. You can change the header name to whatever you like.
VLOOKUP looks up every row number that is less than or equal to the highest row number that contains text in A2:A. Each of these qualifying rows is looked up in a second array that contains only the row numbers and Column-A data from non-blank rows, returning the data itself. Since rows are in perfect ascending order and the last parameter of VLOOKUP is set to TRUE, all blank rows in the first array will "fall backward" to find the most recent row that did have something in Column A.

Compare two tab's headers, copy column below maintaining blank columns

I have two tabs, one with all of my column headers (data tab), the other tab with less headers, but all from the data tab.
I want to search the headers of the "data" tab using the headers from the second tab starting at column I through BI. The second tab's headers will periodically change but always be present in the data tab. If the search criteria isn't present, I want to leave that column blank. If it is present, return the values below from the data tab.
I've tried a few formulas, but can't quite get what I'm looking for.
This formula worked to find the data:
'FILTERED'!I2
=FILTER(data!I2:AK,COUNTIFS($I$1:$1,data!I1:AK1))
This formula got the placement right, but produced the wrong information:
'Copy of FILTERED'!I3
=ArrayFormula(IF(ISBLANK(I2:2),,FILTER(data!I2:AK,COUNTIFS($I$1:$1,data!I1:AK1))))
Here's my sheet.
You've got a large range to process, so there may be a slight delay filling the grid by formula (2 or 3 seconds maybe). But I have added a sheet ("Erik Help") with the following formula in I2:
=ArrayFormula(IF(ROW(A2:A),IFERROR(VLOOKUP(ROW(data!A2:A),{ROW(data!A2:A),INDIRECT("data!I2:"&ROWS(data!A:A))},HLOOKUP(FILTER(I1:1,I1:1<>""),{data!I1:1;SEQUENCE(1,COLUMNS(data!I1:1),2)},2,FALSE),FALSE))))
Honestly, it's hard to explain how this works, but I'll try to cover the basics.
=ArrayFormula(...)
This just means the formula will be processing a range rather than one cell.
IF(ROW(A2:A), ... HLOOKUP(FILTER(I1:1,I1:1<>""),{data!I1:1;SEQUENCE(1,COLUMNS(data!I1:1),2)},2,FALSE) ...)
IF(ROW(A2:A) is important, because it signals to do something for every row, thereby creating a 2D grid instead of just processing the current row. The HLOOKUP will look up every header in I1:1 that isn't blank [FILTER(I1:1,I1:1<>"")], which as the sheet is now, will be all of them. They will be looked up in in a virtual array formed from a top row consisting of all headers in data!I1:1 over a bottom row made up of a SEQUENCE of numbers made of 1 row and the same number of columns as are in data!I1:1, starting at the number 2 and moving up. (It starts at 2, because part of the VLOOKUP virtual array which I haven't explained yet, will be forming a column 1.)
VLOOKUP(ROW(A2:A),{ROW(data!A2:A),INDIRECT("data!I2:"&ROWS(data!A:A))}, *the HLOOKUP RESULT NUMBER*,FALSE)
Now a VLOOKUP will kick in. It will look up every row in data!A2:A within a virtual array made of two columns; the first column will be those same row numbers, and the second will be everything from data!I2 over and down (the INDIRECT setup allows this to be a dynamic grid in case you add or delete columns later). As to which column from that should be returns, that will pull from the HLOOKUP results explained above (which, as you'll recall, will all match the headers).
Finally, IFERROR(...) will return null if any step in that process returns an error, which would be because something wasn't found.
see:
=ARRAYFORMULA(QUERY(VLOOKUP(ROW(A2:A), {ROW(A2:A),
A2:C},
MATCH(HLOOKUP(E1:G1,
{E1, F1, G1;
B1, A1, C1},
2, 0), A1:C1, 0)
+1,
0),
"select *"))
for missing headers:

Google sheet formula that finds row name and returns column headers based on marked cells

I'm trying to return the column headers for a row that is marked with an x. The row is selected from a name in the left column. I'm stuck here.
I can illustrate what I want to do by showing these images:
Start table
The result I want is this:
Outputs of the possibilities for the first sheet
I have put more information in my Example Sheet.
Link to editable example sheet
This formula should create a table (with a single formula) with the months in one column and the headers in the second column.
=ArrayFormula({A4:A15\ substitute(transpose(query(transpose(if(B4:G15="x";B3:G3&char(10);));;rows(A4:A15)));" ";)})
If you'd want to 'lookup' the months you manually type in you can wrap the above in a vlookup. Example:
=ArrayFormula(if(len(L4:L); vlookup(L4:L; {A4:A15\ substitute(transpose(query(transpose(if(B4:G15="x";B3:G3&char(10);));;rows(A4:A15)));" ";)}; 2; 0);))
You can check out both formulas in the copy of the sheet I've made in the spreadsheet you shared.

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.

Resources