Convert columns to rows in Google Sheets - google-sheets

I have a spreadsheet that looks like this
A B C D E
-------------------------------------------------------------------------
Mercedes A Class C Class E Class G Wagon
BMW 1 Series 3 Series 4 Series
and I wish to create a spreadsheet that looks like this
A B
------------------------------
Merecedes A class
Merecedes C class
Merecedes E class
Merecedes G Wagon
BMW 1 Series
BMW 3 Series
BMW 4 Series
I have used the transpose function, but it doesn't create a new record for each value, but rather just rearranges the columns.
How might I achieve this?

I have been looking for this same answer.
What you are trying to do it called unpivoting but it doesn't look like there is a built in function in google sheets to do it.
I found this fix in the google product forums. https://productforums.google.com/forum/#!topic/docs/5UAxnQKMFm8
At first glance it looks a little complicated but victor tabori's code worked like a charm.
copy and paste his code into tools script editor in your google sheet and then use the =unpivot function in your spreadsheet. Only limitation I ran into was it failed trying to reference data in a different page of the sheet.

For the sake of anyone who's search brought them to this old question on this perennial need; this type of rearranging is a bit easier than it used to be, with Google's addition of the FLATTEN function. Example formula:
=ARRAYFORMULA(QUERY(SPLIT(FLATTEN(A2:A&"|"&B2:E),"|"),"select * where Col2 is not null"))
From the inside out, here we work on a temporary array of makes and models,
with a symbol | concatenated between makes from A and models found in B:E,
FLATTEN then puts it all into a single column,
then SPLIT uses the temporary | characters to split it to a two-column array,
and finally, a quick QUERY lets us filter out blanks after the fact (like the row E3 would become), without having to do all the calculations twice.
Oh, and ARRAYFORMULA lets it work across the whole array of makes and models we input.

Related

Google sheets sum of arrayformula

I want to use arrayformula or filter like with the result of a list of numbers. My question is how to place at the bottom a total of the preceding numbers. The challenge is this is dynamically sized. So when I try to include an option to sum the numbers it doesn't work.
What complicates this even further I would then want to include some numbers below this that aren't included in the sum.
What I tried was this:
={"title"; ArrayFormula; sum of previous cells}
Projects
Title
P1
1
P2
2
P3
1
P4
2
Total
6
Edit: An example that I've used that didn't work out is this:
={arrayformula(if(filter($A2:$A,$A2:A<>"",$A2:$A<>"Total Sum",$A2:$A<>"(to be hired)")="","",{countifs('org 1'!$F$2:$F$1000),"="&filter($A2:$A,$A2:$A<>""),'org 1'!$A$2:$A$1000"),"<>(to be hired)")}));"Total hired"}
Edit:
Here's an example link:
https://docs.google.com/spreadsheets/d/1iBkX1B53vYQVUJ6j8dNOE_ck1dONidsqyNuO0lgSKHw/edit?usp=sharing
I'm using multiple tabs as a reflection to what I'm working with. The tab everything's suppose to show is the Indirect example tab.
I'm using the Source tab as a reference for the tabs I'm targeting. The data tabs is the place I'm drawing the info from.
I'm thinking that I won't be able to do this with ArrayFormula. Probably with Query instead. Not sure what it would look like. If ArrayFormula won't work then a query example would be nice.
see:
={"title"; FILTER(A:A, A:A<>""); SUM(A:A)}
={QUERY({A1:B}, "where Col1 is not null label Col2'title'"); "Total:", SUM(B:B)}

Arrayformula - Adding together a dynamically generated list of ranges

I'm looking for a way to add together a dynamically generated list of ranges using (I'm guessing) an ARRAYFORMULA.
The normal way of attacking this is fine if there is a known list of ranges, the example of the results I want would work using this:
=ARRAYFORMULA( A1:A10 + B1:B10 )
In the case I'm after I want to add together ranges in multiple sheets. I don't want the users to have to manually adjust the array formula every time they add a new sheet to be calculated, and I also want to be able to add some logic to include or remove the particular sheet from the calculation, but for now I'm happy to ignore that and just focus on adding cells together.
My approach to this was to create a column with a list of names, each one matching a sheet in the document, and then using that list to dynamically build the list of ranges to add together, using INDIRECT.
.------------.
| sheet1 | <---- SheetListNamedRange
|------------|
| sheet2 |
`------------'
Here's a quick example
=ARRAYFORMULA( INDIRECT("'" & SheetListNamedRange & "'!D4:75") )
There are lots of failure modes depending on how it's done, but this particular formula only puts in the values of the first sheet and ignores any others, which I guess makes sense.
What I'm after is kind of the equivalent of i++ in a loop found in a normal coding language. Is there some way of making this work?
If I understand you correctly, you'd like to get a list generated based on different ranges across different sheets. If your case is as simple as the one you mention in the beginning of your post, the following would do the job.
={Sheet1!A1:A2; Sheet2!B1:B2}
If you want the sum of all these values, you can use SUM.
=SUM({Sheet1!A1:A2; Sheet2!B1:B2})
Please let me know if this isn't what you were looking for, so I can change the answer accordingly.
you can't refer to array of arrays in INDIRECT. you will need to INDIRECT each sheet which contains array.
=SUMPRODUCT(ARRAYFORMULA(INDIRECT(A1&"!"&"D:D")+
INDIRECT(A2&"!"&"D:D")+
INDIRECT(A3&"!"&"D:D")+
INDIRECT(A4&"!"&"D:D")))
note1: in this case result is 25 as sum of 10 + 15.
10 is sum of sheet1!D:D
and 15 is sum of sheet2!D:D
note2: there is no sheet3 and sheet4 which is equal to 0 in INDIRECT
note3: D:D of the sheet where you have the list of sheets needs to be empty

Array Several Columns to One with Column Headers as Separate Column

I am looking to take several columns in Google Sheets and combine them into one with the column header in an adjacent cell.
I would like to go from something like this:
to something like this:
I am currently achieving this by arraying the first column over and over for the number of columns. I am then arraying each column on on top of another so something like this:
Column 1 Column 2
={A1:A4;A1:A4;A1:A4} ={B1:B4;C1:C4;D1:D4}
In a third column I am using an if-then statements to get the Column name wherever the name column = "Name"
My issue is when I am trying to do this with several columns is becomes very tedious to build out these arrays. I am looking for help with a single formula to achieve this by just referencing the range for all columns.
It's a little tricky, but possible. For explanatory reason I am not putting everything into one, convoluted formula, but present step by step solution. DISCLAIMER: It's fast one, so it in not perfect.
Assumptions:
"Start" data are in Sheet named "Data"
Result is in Sheet named "Result"
Procedure
Add one column (A) to "start" data with this code in A1 (it will be our index):
=ArrayFormula(if(B1:B<>"";row(A1:A)-1;""))
In "Result" sheet put in A1 this code:
=ArrayFormula(INT((ROW(B1:INDIRECT("C"&(COUNTA(Data!$B1:B)-1)*4))-1)/4)+1)
In B1 this code:
=ArrayFormula(if(A1:A<>"";VLOOKUP(A1:A;Data!A1:E;2;0);"" ))
In C1 this:
=transpose(SPLIT(REPT(TEXTJOIN("*";1;Data!C1:1)&"*";COUNTUNIQUE(B1:B));"*";0;0))
In D1 this:
=ArrayFormula(IFERROR( VLOOKUP(B1:B;Data!B1:F;MATCH(C1:C;Data!B1:F1;0));""))
Finally in F1 - to clean the result put this:
={{"Name"\"Column"\"Number"};{B1:D20}}
Link to working copy
Link to spreadsheet
Final thoughts
You can pack everything into one formula
Its very hasty solution, so it will not expand to more than 4 columns by itself (One, Two, etc). This is hard-coded now. If you can't figure it out, just give me a comment - I will show this places / fix them to make it dynamic

Add data to row if it meets criteria, else ignore

I have raw data in my spreadsheet that comes from a Google Form that looks like the following:
(Cost) (Source) (Frivolous) (Medium) (Comments)
A B C D E
1 15.94 McDonalds Yes Credit was hungry
2 98.32 School No Check Paid for textbooks
3 843.00 Hospital No Check Surgery
4 0 asdff Yes N/A Ignore this one woops
5
6 23.99 Dentist No Credit Check up
I want this data to always be copied to a different sheet, but ONLY the data that matches a condition. That condition in this case is if Frivolous is No, meaning I only want on this separate page to track valid important spending.
My second page I want them to look like the following:
(Cost) (Source) (Frivolous) (Medium) (Comments)
A B C D E
1 98.32 School No Check Paid for textbooks
2 843.00 Hospital No Check Surgery
3 23.99 Dentist No Credit Check up
Notice how empty entries are ignored and also entries with Yes under Frivolous are ignored as well.
How would I achieve this? I have absolutely no idea how that would work since I've only been able to achieve this through filter which will not work for this.
I would like to say a few words in defense of Google Spreadsheets and show some great functions that will work, but they are not supported by [excel].
Query
First you may use simple query:
=QUERY(sheet1!A:E,"select * where C = 'No'")
This single short formula will give the desired result, there's no need to fill right and down.
Filter
Actually you may use filter too. This function seems to work too:
=FILTER(sheet1!A:E,sheet1!C:C="No")
Please, read more info about this functions:
Filter
Query and full Query Language Reference
You'll find many exciting things that could be done in Google spreadsheets.
Actually, I was having some trouble with [google-sheets] ArrayFormula function so I used an old-school formula with SMALL and INDEX function in its array form. In A2,
=iferror(index(Sheet13!A$1:A$99, small(index(row($1:$99)+(Sheet13!$C$1:$C$99<>"no")*1E+99, 0, 0), row(1:1))), "")
Fill both right and down.
So you were in fact correct that this could be solved in [excel] with an identical solution as [google-spreadsheet]. However, there are superior methods in newer [exce] (2010+) using the AGGREGATE function that [google-spreadsheet] does not support and I'm sure that [google-sheets] has more elegant functions that I am not recalling right this moment.
Look to Sheet13 and Sheet14 here for the working sample.

Using IF to return cell contents in Google Sheets

Edit for clarity:
The end result that I'm looking for will pass the name picked from the dropdown list in B1 into formulas below to check if each student has completed the training modules listed. The two sheets I'm looking at (for now) are 'Data' and 'Quick Reference' the rest are just placeholders.
I've looked and not found an answer so apologies if this has been asked before!
I'm trying to write a formula that will check form results and if it finds a match, return the contents from another column in that row.
The logical IF function works fine with hard coded entries, but not with cell contents.
So if I'm looking for "Deckhand USS Minnow I" I want to show the contents of Column H. Look on the Data and Quick Reference (Training Progress) tabs to see what I'm talking about.
This works:
=IF(((VLOOKUP($B1,Data!C2:J20,4)="Build Coconut Phone I")),"Yes", "No")
This doesn't:
=IF(((VLOOKUP($B1,Data!C2:J20,4)="Deckhand USS Minnow IV")),Data!H2:H20)
Google spreadsheet
Short answer
Use QUERY() instead of IF(Vlookup(),)
=QUERY(Data!C1:J20,"Select H where F = 'Deckhand USS Minnow IV'",1)
Explanation
While could be possible to use an array formula using IF and VLOOKUP, QUERY() does the work in a direct and simple way.
The formula in the short answer will include the column heading. If you don't want it use
=QUERY(Data!C2:J20,"Select H where F = 'Deckhand USS Minnow IV'",0)
References
QUERY - Google Docs editors Help

Resources