Google Spreadsheet adding sheetname - google-sheets

I have my current query below;
=QUERY({Source_1!B2:I;Source_2!B2:I;Source_3!B2:I},"select * where Col3 is not null",0)
I want to have all my data be combine to one to "All_Sheet" tab it works so far.
Now I need in Col A of my tab "All_Sheet" be included to all rows the Sheet Name like Source_1, Source_2 and so on.

Try this:
Add a calculated column
First build a range that contains the sheet name you want to reference. I did it by concatenating a range that is empty and the sheet name:
{N2:N&"Sheet1",Source_1!B2:I}
The above by itself won't work, because it needs to be told that it is an array. So we wrap it in ARRAYFORMULA():
ARRAYFORMULA({N2:N&"Sheet1",Source_1!B2:I})
Which gives a range that looks like this:
Then build up the rest of your query like this:
Sheet 1 | B2 Value | C2 Value | D2 Value | ...
Build our Query
Take the above and build up your multiple ranges and complete your query. Linebreaks for readability
=QUERY(
ARRAYFORMULA(
{
N2:N&"Sheet1",Source_1!B2:I;
N2:N&"Sheet2",Source_2!B2:I;
N2:N&"Sheet3", Source_3!B2:I
}),
"select * where Col3 is not null",0)

Related

Array of IMPORTRANGEs in Google Sheets

For this sample table:
A
B
C
D
E
1
Range!A1:C5
URL1
= Formula here
2
URL2
3
URL3
4
...
I have this working formula in C1:
={IMPORTRANGE(B1, A1); IMPORTRANGE(B2, A1); IMPORTRANGE(B3, A1)}
A1 contains the range to pull from the target Sheets.
Links to the target Sheets are found in column B.
(This is a simplified example. My actual Sheet has 21 entries in column B.)
Question:
If I will have to add/remove URLs in column B, I have to adjust the formula in C1 and add/remove IMPORTRANGEs.
I would like to have a formula that automatically adjusts to the entries in column B.
Solutions tried:
I tried this, but it did not work:
={JOIN("", ARRAYFORMULA(IF(LEN(B1:B), "IMPORTRANGE(""" & B1:B & """, """ & A1 & """); ", "")))}
The JOIN function returns a text that should be identical to what the array { ... } parses as a formula. But it doesn't. Wrapping it with INDIRECT also does not work.
Any suggestions on how I could make this work?
if by "working formula" you mean valid formula that returns data then only thing you can do is hardcode it like this:
=QUERY({
IFERROR(IMPORTRANGE(B1, A1), {"","",""});
IFERROR(IMPORTRANGE(B2, A1), {"","",""});
IFERROR(IMPORTRANGE(B3, A1), {"","",""})},
"where Col1 is not null", )
A1:C5 = 3 columns = {"","",""}
you can generate formula dynamically with arrayformula but the result will be always a text string and not active formula. tho if you wanna go this way you will need a script that will convert your text string into active formula. example:
https://stackoverflow.com/a/73119313/5632629
https://stackoverflow.com/a/61819704/5632629

Import multiple Sheets into one with Criteria

I am trying to import several ranges from 8 different spreadsheets in to one master list.
This is my first Sheet.
This is my second Sheet.
This is my master list Sheet.
For now, I am testing it on just 2 spreadsheets. Will add the remaining once I figure this out.
So on my master list sheet, A4, I have this code
=query(
{
importrange("https://docs.google.com/spreadsheets/d/1scqPKhWpbZeOK_iYR7EaypNxMhh__V_O2FLUjbjrNFc/edit#gid=0","Scholarship Applicants Database!A4:W");
importrange("https://docs.google.com/spreadsheets/d/1PkBWxUG_0BG4gNhXFGHInSdVI5nMo4FReRpCQIVVs3w/edit#gid=664164578","Scholarship Applicants Database!A4:W")
},"Select * where Col1 <> ''")
Which works well. Then on Column X, these code does not work.
=query(
{
importrange("https://docs.google.com/spreadsheets/d/1scqPKhWpbZeOK_iYR7EaypNxMhh__V_O2FLUjbjrNFc/edit#gid=0","CDP Scoring Sheet (Initial)!G4:J");
importrange("https://docs.google.com/spreadsheets/d/1PkBWxUG_0BG4gNhXFGHInSdVI5nMo4FReRpCQIVVs3w/edit#gid=664164578","CDP Scoring Sheet (Initial)!G4:J")
},"select * where A = '"&$A4&"'")
What I am trying to do here is, import the ranges from Sheet1 and 2, from this sheet (CDP Scoring Sheet (Initial)), Column G4 to J, with the condition that Column A of Sheet 1 and 2 matches Column A of my Master list. Error I am receiving is "Unable to parse query string for function QUERY parameter 2: NO_COLUMN: A", I then change the "A" on the "where" part to "Col1", I get the error "Query complete with an empty output" and the result of "#N/A".
I also thought of changing my importrange from "G4:J" to A4:J" but I know it's not right cause it will then pull out the data from columns A to F which I no longer need.
Help please.
try:
=QUERY(
{
importrange("1scqPKhWpbZeOK_iYR7EaypNxMhh__V_O2FLUjbjrNFc", "CDP Scoring Sheet (Initial)!A4:J");
importrange("1PkBWxUG_0BG4gNhXFGHInSdVI5nMo4FReRpCQIVVs3w", "CDP Scoring Sheet (Initial)!A4:J")
},
"select Col7,Col8,Col9,Col10
where Col1 = '"&$A4&"'")

Using =averageifs with multiple text criteria in different columns

I'm busy with creating an spreadsheet where I can get the average price of holidaydeals with a specific tag (f.e. a destination). When I do this for only one tag column the formula is working :) :
=averageifs(C4:C10,E4:E10,L1,F4:F10,"Frankrijk vakantie")
But... I have more then 10 tag columns in total and in every column something like "Frankrijk vakantie" could be found. My simple mind thought, okay lets change F4:F10 (in this example) to F4:G10 to look for "Frankrijk vakantie" in two columns. But... the formula didn't work.
Link to spreadsheet: https://drive.google.com/file/d/1Gw5VC5qT1bzbFIPBu5j4dLXBmJjh7GuW/view?usp=sharing
I've also added a screenshot. I hope that someone can help me with this. Would be great, thank you!
In L2 try
=query({C4:C11, ArrayFormula(N(mmult(N(F4:O11="Frankrijk vakantie"), transpose(column(F3:O3)^0))>0))}, "Select AVG(Col1) where Col2 > 0 label AVG(Col1) '' ")
and see if that works?
EDIT: to include a month/year filter try
=query({C4:C11, ArrayFormula(N(mmult(N( (F4:O11="Frankrijk vakantie")*(E4:E11=L1)), transpose(column(F3:O3)^0))>0))}, "Select AVG(Col1) where Col2 > 0 label AVG(Col1) '' ")
The formula makes use of a virtual array, containing the values of column C and the output of the mmult() function. The latter creates a column with 1's if 'Franrkijk vakantie' is found in that row AND the date in column E matches the date in L1. The query then averages the values from column C and filters out the rows where the conditions of the MMULT() are not met.
EDIT 2: To check for a 'double match' in the row, try
=query({C4:E11, transpose(query(transpose(F4:S11),,9^99))}, "Select AVG(Col1) where Col3='"&L1&"' and Col4 contains 'Frankrijk vakantie' and Col4 contains 'Europa vakantie' label AVG(Col1)''", 0)
Change range to suit.

Calculate total of values in columns?

I have a Google Sheets document with four sheets. In each sheet, there are two columns: first is a name, and second a value.
I need to get the total of the values from the four sheets in a fifth sheet where the names from the four name-columns correspond.
The column with the values is called differently in most sheets.
How can I accomplish this task?
You can use Query + Arrays, like on image below:
Code:
=QUERY({Sheet1!A2:B;Sheet2!A2:B;Sheet3!A2:B;Sheet4!A2:B};
"select Col1,sum(Col2) where Col1 is not null group by Col1 label Col1 'Name',sum(Col2) 'Total'";0)
Explanation:
First: you combine 4 sheets using an Array. Ranges start from row 2 to omit different columns headers:
{Sheet1!A2:B;Sheet2!A2:B;Sheet3!A2:B;Sheet4!A2:B}
Second: Using Query to perform a total. In Query there are some extra operations:
Col1 is not null - for not showing empty rows in totals
label Col1 'Name',sum(Col2) 'Total' - for naming result columns
Syntax may be a little different depending on your local settings, so look at the working copy:
Link to working copy
Is that serves your needs?

IMPORTRANGE with condition

Using Google Sheets I want, within the same document, to import data from one sheet to another using IMPORTRANGE with conditions.
I have tried unsuccessfully:
=IF(IMPORTRANGE("https:URL","Inc Database!B2:B300")="permanent",IMPORTRANGE("htps://URL","Inc Database!A2:A300"),"")
and
=QUERY(IMPORTRANGE("https:/URL", "Inc Database!A2:A300"),"SELECT Col1 WHERE Col1 <> 'permanent'")
and
=FILTER(IMPORTRANGE("URL","Inc Database!A1:A250"),IMPORTRANGE("URL","Inc Database!B1:B250"="venture permanent"))
I want the function to say: Import any values from range A that meet criterion "permanent" in range B.
A | B
_________|_________
Name |type
---------|-------
Henry |Permanent
William |Intern
John |Permanent
I have put a few examples in the following spreadsheet:
e.g. =QUERY(IMPORTRANGE("https://docs.google.com/spreadsheets/d/1LX7JfbGvgBTfmXsYZz0u5J7K0O6oHJSnBowKQJGa9lY/edit#gid=0", "Inc Database!A2:B300"),"SELECT Col1 WHERE not(Col2 = 'Permanent') ")
You need a single quote around the reference to the sheet/tab since there is a space in the name. Using your example:
IMPORTRANGE("https:/URL", "'Inc Database'!A2:A300")
But this will only import column A, so you cannot check against column B
Then use the Query. If you want everything where B is 'Permanent' then you want (untested):
=QUERY(IMPORTRANGE("https:/URL", "'Inc Database'!A2:B"),"SELECT Col1 WHERE Col1 = 'Permanent'")
This will:
Import all of the rows, starting at A2 from the main data sheet to use in the Query().
Via Query, return only those where Col2 (B) contains 'Permanent'

Resources