How to combine content from several tables into one? - google-sheets

this is the first time I'm posting here.
I'm trying to make a spreadsheet for my work on Google Sheets and I want to make a table with all the information from 5 other tables without having to copy and paste it.
I have 5 tables, all of them have the same number of columns and the same type of data in each column.
I need to make a new table that shows all the data from all the tables in one.
To make it simpler let's suppose I have 3 tables A, B and C
Each of the tables are like this:
Table A:
DATE|DESCRIPTION|PRICE
1/10|AAAAAAAAAAA|10000
2/10|BBBBBBBBBBB|20000
3/10|CCCCCCCCCCC|30000
Table B:
DATE|DESCRIPTION|PRICE
4/10|DDDDDDDDDDD|40000
5/10|EEEEEEEEEEE|50000
6/10|FFFFFFFFFFF|60000
Table C:
DATE|DESCRIPTION|PRICE
7/10|GGGGGGGGGGG|70000
8/10|HHHHHHHHHHH|80000
9/10|IIIIIIIIIII|90000
I would like to make a table D with the information from tables A, B and C like this:
Table D:
DATE|DESCRIPTION|PRICE
1/10|AAAAAAAAAAA|10000
2/10|BBBBBBBBBBB|20000
3/10|CCCCCCCCCCC|30000
4/10|DDDDDDDDDDD|40000
5/10|EEEEEEEEEEE|50000
6/10|FFFFFFFFFFF|60000
7/10|GGGGGGGGGGG|70000
8/10|HHHHHHHHHHH|80000
9/10|IIIIIIIIIII|90000
Also if I add one more row in any of the tables I want table D to update as well automatically either by adding the new row at the end or in another position... It doesnt really matter as long as it appears in table D.
How can I do this with a formula without having to copy and paste it?
Thank you so much!

Use query() with an { array expression }, like this:
=query(
{
Sheet1!A1:C;
Sheet2!A2:C;
Sheet3!A2:C
},
"where Col1 is not null",
1
)

You can use query for that
=QUERY({Sheet1!A2:C; Sheet2!A2:C; Sheet3!A2:C},"Select * where Col1 is not Null",0)
Please note while combining array, make sure to use similar range reference, that means row and column size of each array reference should be same otherwise it will throw Reference Error
Reference
Query

Related

Google Sheets Query with Variables in the data section

I am trying to create a health related spreadsheet that has a lot of data - a lot of which isn't relevant to this question so I've simplified it. There is a column for each type of pain where you write on a scale of 0-10 how intense your pain was, and another column for any relevant notes. The data is broken up into named ranges to make it easier to display on different tabs (HeadData = Head Pain, ChestData = Chest Pain, etc. - 15 named ranges in total.)
One of the tabs I'm working on has a table where you are viewing only the specific named range, in this case HeadData.
=query({HeadData}, " Select * where Col1 is not null ",1)
This works perfectly, but I want to replace {HeadData} with a reference cell to a drop menu so you can select the specific pain area column you want to be displayed.
If I put the reference cell in G1 with a drop down list of the named ranges and select ChestData and try to do
=query({&G1&}, " Select * where Col1 is not null ",1)
It is only picking up G1 (ChestData) as a string and not the actual named range.
So my question is, is there a way to make a drop menu containing named ranges that turn into actual sets of data and not strings when placed in the data section of the query?
Here is my spreadsheet, any help is appreciated. Thanks!
https://docs.google.com/spreadsheets/d/1CcuSV2bbfxsUPPkmj-fru2yYYmmtpXk9LKEF85sxVUw/edit?usp=sharing
You can use INDIRECT for this.
INDIRECT
Returns a cell reference specified by a string.
Change your formula to
=query({INDIRECT(G1)}, " Select * where Col1 is not null ",1)
The INDIRECT function will convert the string in G1 to a cell reference and then the rest of your formula will query the relevant named range.

Google Sheets query pivot multiple values

I am trying to use a Query to replicate the behaviour pictured below. I have a pivot table with two values (as rows) displaying a job name and it's status. This works fine with the built-in pivot table.
Sample Data:
Resulting pivot table:
I've then tried to recreate this using the following Query formula:
=QUERY(A1:D6, " select C, max(A), max(D) GROUP BY C PIVOT B Order by C ")
This gives me the result below. This is as close as I've managed to get to replicating the original pivot table.
Edit
The main part of this question has been solved below but I'm wondering how to extend this out if I had more columns of data that I then wanted to display in the same way. Below I have added an additional column 'Other'.
New data:
New desired result:
Try:
=Index({"",Transpose(Unique(Filter(B2:B,B2:B<>"")));Flatten({Sort(Unique(C2:C)),IFError(Unique(C2:C)/0)}),IFNA(VLookup(Transpose(Unique(Filter(B2:B,B2:B<>"")))&Flatten({Text(Sort(Unique(C2:C)),"hh:mm"),Text(Sort(Unique(C2:C)),"hh:mm")&".1"}),{Flatten({B2:B&Text(C2:C,"hh:mm"),B2:B&Text(C2:C,"hh:mm")&".1"}),Flatten({A2:A,D2:D})},2,0))})

IF cell contains, THEN return certain value with more values and return possibilities

I have a sheet with the following columns:
Column 1: contains text of the form "TS001", "TS002", "DR001", "MS002" etc.
The 2 letter in the beginning are a code for the manufacturer name, so for example "MS=Microsoft".
For the second column, I would like to have a formula that goes through the first column and searches for those letters, in order to then return the complete name of the manufacturer.
For example, it should look something like this:
Column 1
Column 2
MS001
Microsoft
TS002
Tesco
DR001
DR. Pepper
TS003
Tesco
Is something like that possible?
Thank you very much!
When you say "MS=Microsoft" it implies somewhere you have a table with that reference. For the purposes of the following example I created a sheet named ReferenceTable where column A contains the two letter code, and column B contains the name of the company. So it looks like this:
A
B
MS
Microsoft
TS
Tesco
And now in the main sheet in column B you would write the following formula:
=ARRAYFORMULA(VLOOKUP(MID(A1:A,1,2),ReferenceTable!A1:B,2,FALSE))
This will give you the name of the company, looked up from the reference table.
The array formula is there so that you only have to put this formula in cell B1, and assumes you will use the ReferenceTable sheet as a list; that way as you add records to Column A Column B is populated by the arrayformula in B1.
I'd simply use a Reference Table and a VLOOKUP formula
If cell B7 contains "MS0001"
the following formula will attempt to match just the first two letters again a reference table located in cells O7:P9
=VLOOKUP(MID(B7,1,2),O7:P9,2,FALSE)
and will return "Microsoft" when it finds "MS"
In order to achieve what you want, somewhere you need to have a list of the two letter codes and the corresponding company name.
As with all vba, there’s any number of ways to do this, but I would probably put the two letter code and company data into an array, then iterate through col1 to create the desired output for col2.
E.g below assumes the two letter code and company names are in col3 and col4 respectively, but you can change it to wherever they’re located.
Sub CompName()
Dim Cmpname () as string
Dim col1 as range, rng as range
Cmpname = range(range(“C1”), range(“D1048576”).end(xlup))
Set col1 = range(range(“A1”), range(“A1048576”).end(xlup))
For each rng in col1
For i = lbound(Cmpname, 1) to ubound(Cmpname, 1)
If left(rng, 2) = Cmpname(i, lbound(Cmpname, 2)) then
rng.offset(0,1) = Cmpname(i, ubound(Cmpname, 2))
Exit For
End if
Next
Next
End Sub
I’ve admittedly just written this on my phone and have not tested it, but hopefully there’s minimal mistakes.
I just reread your question and realized that you may actually want a formula rather than vba code.
If this is correct, using an INDEX MATCH is probably your best bet.
In this example I’ll assume the same setup as described above - col3 has company codes and col4 has company name - and this formula can be inserted into cell B1:
=index(D:D,match(left(A1,2),C:C,0))
You can then just filldown for the rest of the entries in col2.
Again, done from memory without testing so hopefully got it right.

Query particular row + remove X columns + and sum the rest in one formula?

I have a CSV file that I'm pulling from a database. It's in an awkward layout so I need to reorganise it and display the result in a separate sheet.
Here is a dummy example of the data structure I get.
https://docs.google.com/spreadsheets/d/1sTfjr-rd0vMIeb3qgBaq9SC8felJ1Pb4Vk_fMNXQKQg/edit?usp=sharing
It looks like that. The database grows every day by date and sometimes countries so I need to account to that in my formula.
I need to pull data per each country and display it by date.
I don't need data from Column A, C and D. And when there are multiple states I need to sum them up in one column.
It should look like this and keep growing downwards. I'm gonna use this table for a graph chart
What I've tried so far
=TRANSPOSE(QUERY(IMPORTRANGE("url_to_a_separate_sheet_where_I_importing_a_row_csv_file", "CSV-source-sheet!A1:500"), "SELECT * WHERE Col2='Germany'"))
This works, kinda. But pulls in unnecessary columns and I can't figure out how to sum countries with multiple states. When I add select sum(*) it gives me a big and long error. I assume it might be because of unnecessary columns that the formula cant sum up and I don't know how to omit them. I'm stuck
I tried offset and skipping no luck. Any ideas?
try:
=ARRAYFORMULA(TRANSPOSE(QUERY({Sheet2!B:B, Sheet2!E:BE},
"select Col1,"&TEXTJOIN(",", 1,
"sum(Col"&ROW(INDIRECT("Sheet2!A2:A"&COUNTA(Sheet2!1:1)-5))&")")&"
where Col1 is not null
group by Col1
label Col1'Date'", 1)))
spreadsheet demo

Add column with cell values to tables merged using Query

I use this Query formula in Google Spreadsheet to merge tables from many source sheets into one table:
=query({Data1!A4:B;Data2!A4:B;Data3!A4:B}; "select * where Col1 is not null")
To distinguish original tables in the merged table, I need to add new column to the merged table with their identification. The indentification string is in cells on every source sheet. How to do it?
This is example spreadsheet I prepared for tests. Copy it to your Google Drive to make changes please.
https://docs.google.com/spreadsheets/d/1YimEsiDa3gTiKqv7DMohfNvBDruvQ13zrK23Y3R3Gsw/edit?usp=sharing
I suspect the simplest way is the "long-winded" add a column in each sheet (say a new "A") and populate that with the sheet name, so then a query like so:
=query({Data1!A4:C;Data2!A4:C;Data3!A4:C}; "select * where Col2 is not null")
Edit re Comment:
Not the layout required but at least differentiates the source without adding anything to the sources:
={query(Data1!A4:B,"select * where A is not NULL label A '"&Data1!B1&"'");query(Data2!A4:B,"select * where A is not NULL label A '"&Data2!B1&"'");query(Data3!A4:B,"where A is not NULL label A '"&Data3!B1&"'")}

Resources