How do I get Row Totals using QUERY() in Google Sheets? - google-sheets

I'm working with the following Google Sheet.
Sheet2 uses the following QUERY() function to retrieve data from Sheet1
=QUERY(IMPORTRANGE("1s8krJ7rbZ1DMblZ3vdLcG5pySVM3ESCBy1o7R5Zv4LM", "Sheet1!B3:D"))
Is it possible to return the Row Totals (For Example: B4+C4+D4 for Row 4) using the above QUERY() function?
Please Advise.
My Query and Expected Output are Outlined on the Google Sheet.

You should be able to do something like this:
=QUERY(IMPORTRANGE("1s8krJ7rbZ1DMblZ3vdLcG5pySVM3ESCBy1o7R5Zv4LM", "Sheet1!B3:D"), "Select Col1+Col2+Col3 label Col1+Col2+Col3 ''")
Note that importing from another tab in the same spreadsheet doesn't require importrange. In that case, this should also work:
=QUERY(Sheet1!B3:D, "Select B+C+D label B+C+D ''")
Another way, to achieve the same result would be
=ArrayFormula(if(len(Sheet1!B3:B), Mmult(--Sheet1!B3:D, transpose(column(Sheet1!B2:D2)^0)),))

Be aware that while a simple QUERY can return the results, it will actually take up the entire column if you don't limit it. In other words, all the null rows from Sheet1! B:D will also come over with the QUERY. If you want only the results that have numbers, try something like this:
=QUERY(Sheet1!B3:D, "Select B+C+D WHERE B+C+D is not null label B+C+D ''")
Or you could use MMULT like this:
=MMULT(FILTER(Sheet1!B3:D,Sheet1!B3:B<>""),SEQUENCE(3,1,1,0))
The results may look the same whether limited or not. But in a QUERY or MMULT without limitations, you won't be able to use the space below the visible results for anything. I only mention this because, currently in your sheet, you do have data (a TRANSPOSE formula) below the main results. If you won't in your real sheet and don't care about the are below the visible results being inaccessible to other data or formula entry, then you don't need to limit.

Related

Using Countif, Vlookup and Importrange

I'm currently struggling with using the Countif, Vlookup and Importrage formula on Google Sheets...
I need to pull data from the raw data sheet to the data set that I'm using and can't get it right.
Formula that I have is as follows:
=countif(vlookup(A2,IMPORTRANGE("URL","Data!A2:I940"),9,false),"supplier")
I need to look up the date and then get the count for how many "Suppliers" we had on that specific dates..
Anyone having the same issue or is my brain just over worked???
I think a better choice would be to use a query formula instead.
Please follow the logic of this given formula
=QUERY({B1:D12},"select count(Col1)
where Col3=date'"&TEXT(F1,"yyyy-mm-dd")&"' and Col2='"&F2&"'
label count(Col1) '' ")
(Do adjust ranges to your needs)

Is there a way to make a query with an importrange to mimic a sumif formula from another Google sheet?

I am encountering a capacity issue with one of my spreadsheets but certain information in the sheet still needs to be available to cross-reference. The original spreadsheet contains this formula:
=sumif(A:A,A2,N:N)=O2
I tried to do a direct transfer to the new spreadsheet by adding importranges like this:
=SUMIF(IMPORTRANGE("URL","Sheet1!A2:A"),IMPORTRANGE("URL","Sheet1!A2"),IMPORTRANGE("URL","Sheet1!N2:N")=IMPORTRANGE("URL","Sheet1!O2")
The equation keeps putting out an error saying "argument must be a range". Then I tried to write it out as a query like this:
=QUERY(AND(IMPORTRANGE("URL","Sheet1!A:A"),IMPORTRANGE("URL","Sheet1!N:N"),"SELECT SUM(CASE WHEN Col1=A2 THEN Col14)=O2"))
But the equation is showing FALSE as the result when it should say TRUE. I have attached an example spreadsheet so that these equations make more sense.
I also need the equation to take into account any new information that is placed on the new spreadsheet while also comparing it with the old info. I tried doing this to start with but then slowly realized the initial equation itself doesn't work.
=SUMIF(IMPORTRANGE("URL","Sheet1!A2:A"),IMPORTRANGE("URL","Sheet1!A2"),IMPORTRANGE("URL","Sheet1!N2:N"))=IMPORTRANGE("URL","Sheet1!O2")+SUMIF(A:A,A2,N:N)=O2
Any help is appreciated.
https://docs.google.com/spreadsheets/d/1q1cWh58p23dcQulQ4rz6v4iiDywEFHvcrvASao61xxo/edit#gid=0
use this formula instead of SUMIF:
=ARRAYFORMULA(IF(LEN(
IMPORTRANGE("1q1cWh58p23dcQulQ4rz6v4iiDywEFHvcrvASao61xxo","Sheet1!C2:C")),
IF(IFERROR(VLOOKUP(
IMPORTRANGE("1q1cWh58p23dcQulQ4rz6v4iiDywEFHvcrvASao61xxo","Sheet1!C2:C"),
QUERY(IMPORTRANGE("1q1cWh58p23dcQulQ4rz6v4iiDywEFHvcrvASao61xxo","Sheet1!A2:N"),
"select Col3,sum(Col14) where Col1 is not null group by Col3 label sum(Col14)''"),2,0))=
IMPORTRANGE("1q1cWh58p23dcQulQ4rz6v4iiDywEFHvcrvASao61xxo","Sheet1!O2:O"), TRUE, FALSE),))

How to use absolute references in google sheets query formula or query by column names for multiple sheets?

I have the following problem. I'm trying to write query formula in order to put all values in one sheet by typing yes in one column, however, I have the following problem. Whenever I add a new column, references in query formula (multiple sheets) are changing and the formula doesn't work. How can I prevent this?
Or is there any way to query by a column name in multiple sheets?
I have tried locking relative references by putting sign $
I have tried to use an indirect formula to take references (data set in Helper TAB, cell E2) - nothing worked
I'm out of the ideas, for now, anyone knows how to fix this?
=QUERY({'Sheet 1'!$A:$Z;'Sheet 2'!$A:$Z;'Sheet 3'!$A:$Z;'Sheet 4'!$A:$Z}, "select * where Col1 ='yes'",0)
Here is the file I did, you can see query formula in Master sheet:
https://docs.google.com/spreadsheets/d/1XD-CECy5W5-HM5EkBFJQKDtLlykQq8Cj8ZOvDUXkd1s/edit?usp=sharing
A solution to the problem is to use a formula that searches for the column address based on a reference.
=SUBSTITUTE(ADDRESS(1;COLUMN(A1);4);"1";"")
This formula will return the value A, which is the column of address A1.
It can be used in query functions like this:
=QUERY(A:D;"SELECT "&SUBSTITUTE(ADDRESS(1;COLUMN(A1);4);"1";"")&"")
This function is equivalent to the function:
=QUERY(A:D;"SELECT A")

How to concatenate strings and select the same columns multiple times using Query (Google Sheets)

I am trying to generate a table for the Gantt chart. Table should have this format:
https://developers.google.com/chart/interactive/docs/gallery/ganttchart#data-format
So,I need task name the same like taks ID, but in Query I can't use Col1 twice (I get error)
=QUERY({Tab1;Tab1};"select Col1,Col1,Col5,Col16,Col17 WHERE Col16>now() ORDER BY Col5 DESC,Col17 ";0)
The second point is that it is also not possible to merge two columns as a result, so it doesn't work:
=QUERY({Tab1;Tab1};"select Col1+Col7,Col1,Col5,Col16,Col17 WHERE Col16>now() ORDER BY Col5 DESC,Col17 ";0)
Here is my data and 2 results what I neet to get by QUERY
https://docs.google.com/spreadsheets/d/1CZYgfYo6oIeONZOH6ZR5rOW615HuH4ICaoe7lj0dapw/edit#gid=0
These are such trivial things in a real SQL, is there no way to do it somehow straightforwardly in Google Query? So far I have found a combination of QUERY and ARRAYFORMULA but then there are very complicated queries - mutants. Not easier?
You don't need Query, just Arrays.
You will get the first result from this code:
={ARRAYFORMULA(B3:B&" "&C3:C)\A3:A}
The second result from this code:
={A3:A\A3:A\B3:B1}
Based on your example I assume that you are not using US spreadsheet settings.
If so formulas have to be change to:
First:
={ARRAYFORMULA(B3:B&" "&C3:C),A3:A}
Second:
={A3:A,A3:A,B3:B}
Link to working example: https://docs.google.com/spreadsheets/d/1eMkOkyFwvDeYSy-8UlhQum4OWcb-4WJqGxy_CXM8pVs/edit?usp=sharing
I see that in your real sheet you would like to compare some data with now(). You can easily do this using array I propose as a source to Query. There will you have something like this (of course now it will not work - its only an example - an array have only 2 columns, not 15):
=QUERY({ARRAYFORMULA(B3:B10&" "&C3:C10)\A3:A10};"select * where Col15>now()";0)
About Query - you can't perform arthmetic operations on column containing strings. Look at the documentation: https://developers.google.com/chart/interactive/docs/querylanguage#arithmetic-operators
"I can't use Col1 twice (I get error)"
You can duplicate your indata that to solve this.
QUERY({Tab1 Column 1\Tab1 Column 1};"Select Col1, Col2......"
"Tab1 Column 1" is now Col1 and Col2
"The second point is that it is also not possible to merge two columns as a result, so it doesn't work:"
Yes, adding result of column is possible "select Col1+Col7......" is correct.

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