Add column with cell values to tables merged using Query - google-sheets

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

Related

How to SUMIF identical data spread out on multiple columns

I using excel to organize my cut list for a panel board. I have a table created like so, and i need help creating a formula that adds the number of pieces of panels of identical dimension and material, regardless of which panel it belongs
I have done formulas before where the datas where in a single column and there was only 1 criteria to check using UNIQUE and SUMIF formulas. I can already extract all the unique dimensions using
=UNIQUE(FLATTEN(C17:C19,E17:E19,G17:G19))
but that all that i got for now.
try:
={"Material", "Dimension", "Pcs";
QUERY({A2:C9; A2:A9, D2:E9; A2:A9, F2:G9},
"select Col1,Col2,sum(Col3) where Col1 is not null
group by Col1,Col2 label sum(Col3)''", 0)}

Google Sheets Query Search Box | Search criteria

so basically I have a searchbox in my sheet that searches and pulls up data. For a reference see this image: https://i.imgur.com/MVTUCSw.png. So basically in cell A4 I put the data that I am looking for, but my formula restricts me to only looking up stuff in 1 row. For example, data starting with the word MELD, but I would like to be able to also look up data based on for example the someone their name.
The formula I use for the searchbox: =QUERY({'Pallets & Locaties'!A2:G;Voorraadverschillen!A2:G}, "SELECT * WHERE Col1 "&Opzoeken!B4&" '"&A4&"'")
The data that I want to be able to look up is stored in 2 sheets: Pallets & Locaties - https://i.imgur.com/qV7h2tz.png and in Voorraadverschillen - https://i.imgur.com/foqLkKa.png.
The searchbox is only able to lookup data in row, but I just want to be able to search for any kind of stored data in any of the sheets.
Here is my sheet for reference: https://docs.google.com/spreadsheets/d/10wmnxV16JUiD_b_54abkiLPwTqLITWRcUw3gsqvXoBE/edit?usp=sharing
I'd recommend you add more rows for the lookup criteria and add a column for what column it would search for.
Sheet modification:
Formula:
=QUERY({'Pallets & Locaties'!A2:G;Voorraadverschillen!A2:G}, "SELECT * WHERE "&TEXTJOIN(" AND ", TRUE, ARRAYFORMULA(IF(ISBLANK(A4:A10), "", A4:A10&" "&B4:B10&" '"&C4:C10&"'"))))
Test Sheet
Note:
The above formula will allow you to search on other columns with their own words and criteria to search.
Only rows with Kolom values will be included in the criteria. If you only need Col1 criteria, make sure to leave other rows blank.
This does use an AND search, meaning all of the criteria should be true and match the row. Feel free to use OR in the TEXTJOIN function if you only want to search all rows matching any of the criteria.
This will only search on sheets Pallets & Locaties and Voorraadverschillen. Add the necessary sheets if you need them.
EDIT:
Cleaned up the formula to not be so repetitive.
=IF(A4<>"",(QUERY({'Pallets & Locaties'!A2:G;Voorraadverschillen!A2:G},"Select * WHERE "&textjoin(" OR ", true, arrayformula("Col"&ROW(1:7)&" "&B4&" '"&A4&"'")))),(QUERY({'Pallets & Locaties'!A2:G;Voorraadverschillen!A2:G},"Select * WHERE Col1 IS NOT NULL")))
This searches every column for the data, as long as data is not identical in two columns you won't have issues. An example would be the search criteria "MELD" being in both Column A and B. If that were the case, only the results from the first matching column would populate.

How can I combine multiple columns into one column in Google Sheets?

(Note: Please simply look at the Google sheet for the quickest understanding of what I'm describing in the below bulletpoints)
My data has rows which each represent an order
Each order (row) can consist of multiple products
For each product in an order (row) there is another set of columns in the same row
I need this data to convert into only one set of columns per row (i.e. one product per row)
The products (new rows) need to remain next to eachother so the columns can't just be added to the bottom of the array (which is more simple)
Can you please take a look at the example below and help me achieve this?
Example Sheet
Screenshot of linked sheet
Try this in another sheet
=SORT({query({Reference!$A5:$A,Reference!B5:F},"select * where Col2 is not null ");query({Reference!$A5:$A,Reference!G5:K},"select * where Col2 is not null ");query({Reference!$A5:$A,Reference!L5:P},"select * where Col2 is not null ")})

How to add an extra column including tab name when merging multiple sheets in Google Sheets?

I'm trying to merge multiple tabs into one master sheet. This is working fine:
=QUERY({SheetOne!A16:B;SheetTwo!A16:B;SheetThree!A16:B;SheetFour!A16:B}, "SELECT * WHERE Col1 IS NOT NULL", 0)
And it gives me the table
Week,Users
12,123
13,234
14,345
12,123
13,234
14,345
12,123
13,234
14,345
However, I'd like another column that creates a reference to the sheet. So the result I'd like is:
SheetName, Week, Users
SheetOne,12,123
SheetOne,13,234
SheetOne,14,345
SheetTwo,12,123
SheetTwo,13,234
SheetTwo,14,345
SheetThree,12,123
SheetThree,13,234
SheetThree,14,345
Thanks. Bonus question: How to insert a custom string-value instead of SheetOne,SheetTwo [...]

Combining multiple spreadsheets in one using IMPORTRANGE

I would like to aggregate the data of multiple spreadsheets into one spreadsheet.
Spreadsheet 1 has a Row of Strings A2:A500
Spreadsheet 2 has a Row of Strings A2:A500
Spreadsheet 3 is supposed to have a Row of both (Spreadsheet1!A2:A500 AND Spreadsheet2!A2:A500).
Duplicates shall not be handled differently. I would like them to appear as often as they appear in the different sheets.
Is it possible to do this without writing a script or using jQuery, e.g. by using IMPORTRANGE?
What does not work: I have tried using IMPORTRANGE as follows:
ARRAY{IMPORTRANGE("key-of-spreadsheet1","list!A2:A500"), IMPORTRANGE("key-of-spreadsheet2", "list!A2:A500")}
This causes an error.
You should be able to use a vertical array in the Spreadsheet 3:
={IMPORTRANGE("Sheet1Key","SheetName!A2:A500");IMPORTRANGE("Sheet2Key","SheetName!A2:A500")}
Of course, it is also possible to combine several IMPORTRANGE() functions with the QUERY() function, which gives us a greater control over the results we import.
For example, we can use such a construction:
=QUERY(
{
IMPORTRANGE("key-or-url-of-spreadsheet-1", "'sheet-name-1'!A2:Z100");
IMPORTRANGE("key-or-url-of-spreadsheet-2", "'sheet-name-2'!A2:Z100");
IMPORTRANGE("key-or-url-of-spreadsheet-3", "'sheet-name-3'!A2:Z100");
IMPORTRANGE("key-or-url-of-spreadsheet-4", "'sheet-name-4'!A2:Z100")
},
"SELECT * WHERE Col1 IS NOT NULL ORDER BY Col3 ASC"
)
###Explanation:
The above query removes blank lines from imported ranges:
SELECT * WHERE Col1 IS NOT NULL
and sorts ascending all data collected together in relation to the third column:
ORDER BY Col3 ASC
For descending, just use DESC in place of ASC.
Of course, we can also arrange any other criteria, or omit them displaying everything without modification:
"SELECT * "
###Note:
In order to use the above constructed query, we first need to call a single IMPORTRANGE() method for each of the spreadsheets we want to refer:
=IMPORTRANGE("key-or-url-of-spreadsheet-1", "'sheet-name-1'!A2:Z100")
We have to do this even if we refer to the same spreadsheet in which we write this formula, but for every spreadsheet it is enough to do it once.
This is to be able to connect these sheets and allow access to the sheets (to which we have the access rights anyway):
                                                    
After giving permission for all spreadsheets, we can use the above query.
I am also applying above given formula for getting data from multiple spreadsheet which is getting an error something is like IN ARRAY_LITERAL An array literal was missing values for one or more rows.
Easy fix: Apply the filter to the entire column / sheet instead of just the current selection. This will automatically update all of the filters to include new additions.

Resources