Google sheets formula transposing column title to row - google-sheets

Attached is the link of my question
I would like to transpose the data like that.
My original data could be thousand of lines.
My thought is to make the same number of tables of my column title, then combine the four tables into one. My thought is on the google sheet as well. It might do the work but I would like a nicer solution.
A picture of my question

After having answered this question hundreds of times in the last couple years after the discovery of the FLATTEN() function, i decided to write a custom function for Google AppsScript to do it. While in some sense, a custom function is more opaque than the SPLIT(FLATTEN( formulaic solution you will find all over the forums in the last couple years, it is at least a little easier to understand in it's operation by the user.
In your shared sheet there is a new script file called MKHelp.gs. In it, you will find the code that I wrote to construct the "UNPIVOT()" function.
On a new tab in your sheet, you will find this formula in a tab called MK.Help.
=QUERY(UNPIVOT(A2:B,"V",C2:E,"B",C1:E1,"H"),"where Col3 is not null")
for unpivot to work well it is best surrounded by a query() to weed out unnecessary rows. In your case, it is that the "amount" not be "empty" or "null".
The letters "V","H" or "B" that follow each range describe the "shape" of the input data. Whether it is "vertical", "horizontal" or "Both".

Try
=query(arrayformula(split(flatten({(A3:A4&"♦"&B3:B4&"♦"&C3:E4)}),"♦")),"select * where Col3 is not null")
Explanation
step1: =arrayformula(A3:A4&"♦"&B3:B4&"♦"&C3:E4)
step2: =arrayformula(flatten(A3:A4&"♦"&B3:B4&"♦"&C3:E4)) , flatten will put all data inside one single column flatten()
step3: =query(arrayformula(split(flatten({(A3:A4&"♦"&B3:B4&"♦"&C3:E4)}),"♦")),"select * where Col3 is not null")

Related

Stack Google Sheets columns in a cleaner way?

I am making a spreadsheet to compare various lists in various different ways. One of those ways is to take a bunch of separate lists (each in different columns) and combine them into one large column (without losing anything). This allows me to keep things separate while still having the option to VLOOKUP and FILTER and stuff. Maybe this isn't the way to go about it, but it's the way I've gone with so far.
The solution I came up with for now is to QUERY it all as an array (I think that's the right term) and then just select everything that isn't "".
It looks like this though:
=QUERY({Decklists!B8:B;Decklists!C8:C;Decklists!D8:D;Decklists!E8:E;Decklists!F8:F;Decklists!G8:G;Decklists!H8:H;Decklists!I8:I;Decklists!J8:J;Decklists!K8:K;Decklists!L8:L;Decklists!M8:M;Decklists!N8:N;Decklists!O8:O;Decklists!P8:P;Decklists!Q8:Q;Decklists!R8:R;Decklists!S8:S;Decklists!T8:T;Decklists!U8:U;Decklists!V8:V;Decklists!W8:W;Decklists!X8:X;Decklists!Y8:Y;Decklists!Z8:Z;Decklists!AA8:AA;Decklists!AB8:AB;Decklists!AC8:AC;Decklists!AD8:AD}, "select Col1 where Col1 <>''", 0)
Which is really ugly. It works and does what I want, but I'd much prefer it to be clean and work if I add more columns in the future, without having to go and add the additional "call".
If you'd like to look at the whole sheet, it's here: https://docs.google.com/spreadsheets/d/17Nwek5ZCgu7Jvk922hl_gv9UJAeORjeH9brXqu3zL_Y/edit#gid=940775206
On the "Best Buys" sheet cell C2 Paste this simple formula.
=QUERY(FLATTEN(Decklists!B8:AF), "select Col1 where Col1 <>''")
Explanation
1 - FLATTEN the range needed in this case the Decklists range Decklists!B8:AF.
2 - QUERY the resulted column from the FLATTEN function with the query "select Col1 where Col1 <>''", to get only non empty cells
Try
=query( flatten(offset(Decklists!A:A,0,1,,columns(Decklists!1:1)-1)) ,"where Col1 is not null",0)
So we have 3 things to do, first one is transpose so the columns are below the other, then flatten so that it is ordered as necessary and finally a filter to remove extra blank columns we selected This will work wonders considering the columns are different lengths, are already next to each other as in a table. If columns are at multiple places use {A:A;D:D} to select columns and remove flatten and transpose.
=filter(flatten(transpose(A1:D10)), len(flatten(transpose(A1:D10))))

Making a dynamic timecard for a work schedule in excel

Maybe I'm asking a pretty basic question, but I've searched here (and on other sites) for an answer and I couldn't find it.
I'm making a work schedule for my workplace on Google Sheets, analogous to the example posted as an image down here, that includes an automatic time card for every worker (that is, a formula answering the question "which days had [WORKER] worked?").
I managed to make one, with the filter formula
=FILTER(A2:A16;{or(COUNTIF(B2:D2;$L$1);COUNTIF(F2:H2;$L$1));or(COUNTIF(B3:D3;$L$1);COUNTIF(F3:H3;$L$1));or(COUNTIF(B4:D4;$L$1);COUNTIF(F4:H4;$L$1));or(COUNTIF(B5:D5;$L$1);COUNTIF(F5:H5;$L$1));or(COUNTIF(B6:D6;$L$1);COUNTIF(F6:H6;$L$1));or(COUNTIF(B7:D7;$L$1);COUNTIF(F7:H7;$L$1));or(COUNTIF(B8:D8;$L$1);COUNTIF(F8:H8;$L$1));or(COUNTIF(B9:D9;$L$1);COUNTIF(F9:H9;$L$1));or(COUNTIF(B10:D10;$L$1);COUNTIF(F10:H10;$L$1));or(COUNTIF(B11:D11;$L$1);COUNTIF(F11:H11;$L$1));or(COUNTIF(B12:D12;$L$1);COUNTIF(F12:H12;$L$1));or(COUNTIF(B13:D13;$L$1);COUNTIF(F13:H13;$L$1));or(COUNTIF(B14:D14;$L$1);COUNTIF(F14:H14;$L$1));or(COUNTIF(B15:D15;$L$1);COUNTIF(F15:H15;$L$1));or(COUNTIF(B16:D16;$L$1);COUNTIF(F16:H16;$L$1))}=TRUE)
that is the day column A2:A16 filtered with a manual array column that check if every row B:D (representing an AM shift) OR every row F:H (PM shift) contains the worker's name $L$1.
This is not an optimal solution, because I have to write the array manually, in a very time-consuming and not flexible way (to add another column/another worker to the workshifts means that I'll have to correct every single row).
Is there another better way to use FILTER? So far I've been trying to use MATCH or HLOOKUP, but I'm a rookie and not so familiar with excel arrays.
Post this formula in your K2 cell:
=query(
{{A2:A\B2:D};{A2:A\F2:H}};
"select Col1 where Col2 = '"&L1&"' or Col3 = '"&L1&"' or Col4 = '"&L1&"'"
)
It will work dynamically with your workers:

Using a Named Range to identify worksheets to use a SUMIFS on a to add an array of data

Below is a sample of the Google Sheet I'm working on:
https://docs.google.com/spreadsheets/d/1LDsfn_FMdUSfuFZAyuXf5gdlOSN9yLqhT1RY8aX7Nfs/edit?usp=sharing
On the spreadsheet Team Totals, I'm trying to calculate all the sales based on the date, the type of sale, and the program type starting from Row 27.
The problem I have run into is that the Named Range, Consultants, only pulls the data from the first cell of the Named range (Octo). What I want is for a formula that will pull the data of the selected range from each entry in the named range and sum them together.
For reference, I put what the proper values should be starting from Row 33.
The closest example I have seen was from this website: https://www.got-it.ai/solutions/excel-chat/excel-tutorial/sumif/sumif-across-multiple-sheets
However, I get the same problem that only the first cell in the named range gets pulled.
I feel like I'm missing something simple, but I have been bouncing it around my head for hours and can't figure it out. Any help is greatly appreciated.
So after trying to figure out a way not to use an iterative process, I folded and did this:
=arrayformula(SUMIFS(INDIRECT("'"&$B$4&"'!F6:F100"),INDIRECT("'"&$B$4&"'!D6:D100"),$B$2,INDIRECT("'"&$B$4&"'!E6:E100"),E$27,INDIRECT("'"&$B$4&"'!A6:A100"),$A41)+
SUMIFS(INDIRECT("'"&$B$5&"'!F6:F100"),INDIRECT("'"&$B$5&"'!D6:D100"),$B$2,INDIRECT("'"&$B$5&"'!E6:E100"),E$27,INDIRECT("'"&$B$5&"'!A6:A100"),$A41)+
SUMIFS(INDIRECT("'"&$B$6&"'!F6:F100"),INDIRECT("'"&$B$6&"'!D6:D100"),$B$2,INDIRECT("'"&$B$6&"'!E6:E100"),E$27,INDIRECT("'"&$B$6&"'!A6:A100"),$A41)+
SUMIFS(INDIRECT("'"&$B$7&"'!F6:F100"),INDIRECT("'"&$B$7&"'!D6:D100"),$B$2,INDIRECT("'"&$B$7&"'!E6:E100"),E$27,INDIRECT("'"&$B$7&"'!A6:A100"),$A41)+
SUMIFS(INDIRECT("'"&$B$8&"'!F6:F100"),INDIRECT("'"&$B$8&"'!D6:D100"),$B$2,INDIRECT("'"&$B$8&"'!E6:E100"),E$27,INDIRECT("'"&$B$8&"'!A6:A100"),$A41)+
SUMIFS(INDIRECT("'"&$B$9&"'!F6:F100"),INDIRECT("'"&$B$9&"'!D6:D100"),$B$2,INDIRECT("'"&$B$9&"'!E6:E100"),E$27,INDIRECT("'"&$B$9&"'!A6:A100"),$A41))
This formula allows me to cut and paste it to various cells easily, and does the job I need it to, while still using the indirect reference so I can change names without breaking the formula (granted I change the worksheet name as well).
I will need to edit this to include all possible worksheet amounts I can forsee, but once it's done, I won't have to tinker with names anymore.
I'm not happy with this answer, as it creates a really long and ugly formula, essentially repeating the same formula 20 times, but it does work. I feel like there should be an easy function that would be able to do this.
This will return Total sales:
=if(isna(ArrayFormula(QUERY({Lo!$A$5:$F; Tulio!$A$5:$F;Ya!$A$5:$F; Miguel!$A$5:$F;Kevin!$A$5:$F; Octo!$A$5:$F}, "select sum(Col6) where Col1=date '"&TEXT(A28,"yyyy-mm-dd")&"' label sum(Col6) ''",0)))=TRUE,0,ArrayFormula(QUERY({Lo!$A$5:$F; Tulio!$A$5:$F;Ya!$A$5:$F; Miguel!$A$5:$F;Kevin!$A$5:$F; Octo!$A$5:$F}, "select sum(Col6) where Col1=date '"&TEXT(A28,"yyyy-mm-dd")&"' label sum(Col6) ''",0)))
BTW, I get Total sales of $0, $615.50 and $2,498.00
The best way to pull data from a lot of unknown tab names without a script is to actually CREATE the tab names beforehand. On your sheet it looks like you're anticipating/making space for 21 employees. If I were you, I would just create 15 more tabs named Temp7,Temp8,Temp9... etc. Then you can just "hide" those tabs. Then a formula can be built in your totals section that will easily stack all the tabs up using an array literal and a QUERY( { } ) to add up the totals for you without all this INDIRECT() nonsense that you have been going through to pull from the individual tab names.
Would pre-adding tab names be something you were interested in if I could show you the formulas to make the totals easy to calculate?

Google Sheets, Is it possible to use Arrayformula to expand formulas downward when the formula uses arrays

I've been using Arrayformula to auto-expand formulas (such as "=Left(A2:A,B2:B-1") downward, but I need some help understanding this formula. I've read up on the function itself and browsed many forums about this but I can only find articles explaining how to use this with simple formulas, so I'm going to try to as this as simply as possible here: Is it possible to use Arrayformula to expand formulas downward when the formula uses arrays?
The summary for Arrayformula reads "Enables the display of values returned from an array formula into multiple rows and/or columns and the use of non-array functions with arrays." This was my understanding of how Arrayformula populated a formula into rows automatically. Using this I thought of it as writing a formula that generated an array of formulas, and then splitting them up with Arrayformula. This seems to not work with some formulas such as concatenate, which I will focus my question on. This example is far from my real life problem, but if someone could show me a solution I can apply it elsewhere.
Arrayformula spreadsheet example
Usually when I use Arrayformula with A1:A it would expand the formula through the column, referencing the corresponding rows as it went. With this example I want to have Column C be the concatenated result of columns A and B. Is this possible with Arrayformula? This question is not specific to concatenate, that is just the simplest one that came to mind. Another example would be Countif. Lets say I want to see how many values in the first 5 columns are over 20, and I want that formula to auto populate down, is that possible and if so how would it be done?
Arrayformula second example
P.S. Please don't say copy the formula using the drag handle in the lower right.

Using IMPORTRANGE, INDIRECT, and CONCATENATE together

I have a sheet that pulls numbers from several different sheets to amalgamate numbers. Each week, a new sheet is added to the source files, all with the same name. I'd like to update the amalgamated sheet by changing one cell instead of many.
When the tab is located in the same Google sheet, this is easily done with INDIRECT.
Right now, the formula in the amalgamated sheet is:
=IMPORTRANGE(M4, "Aug29!$F$2")
That formula is on each line to pull from several different sheets:
=IMPORTRANGE(M5, "Aug29!$F$2")
=IMPORTRANGE(M6, "Aug29!$F$2")
and so on.
Each week, the "Aug29" changes to the new date - "Sep5", "Sep12", etc.
What I'd like to do is use INDIRECT to pull that part of the equation in (from cell Z1) so that I don't have to update each formula.
I was thinking I could use CONCATENATE to create the "Aug29!$F$2" portion of the formula with INDIRECT:
=IMPORTRANGE(M4, CONCATENATE(INDIRECT(Z1), "!$F$2"))
The problem is, the IMPORTRANGE formula requires quotations around the range string, and I can't figure out how to add them.
Weird problem, I'm sure, but wondering if there's anyone with a solution?
Short answer
Use =IMPORTRANGE(M4, CONCATENATE(Z1, "!$F$2")) or =IMPORTRANGE(M4, Z1&"!$F$2")
Explanation
INDIRECT requires a string that represents a cell or range references, like "Z1" but it will increase unnecessarily the complexity of your formula.
If Z1 has the name of the sheet, then just concatenate it by using CONCATENATE, CONCAT or &
UNTESTED. Please try something like:
=IMPORTRANGE(M4,Z$2)
with in Z2 something like:
=Z1&"!$F$2"
In other words, I think your IMPORTRANGE does not like INDIRECT.

Resources