Google Sheets - Query Malfunction - google-sheets

Say I have Sheet 2 data as below:
|----+----|
| A1 | B1 |
|----+----|
| A2 | B2 |
|----+----|
| A3 | 3 |
|----+----|
I want to use query to show it in Sheet 1 with below command:
=query(Sheet2!A:B)
But row 1 and 2 are merged together like the following table
|-------+-------|
| A1 A2 | B1 B2 |
|-------+-------|
| A3 | 3 |
|-------+-------|
If I change the number in B3 from "3" to "B3", then it works just fine. That means if it's a number, query API will not work as expected.
Is it a query API problem or my mistake?

Try using the optional headers argument in QUERY. e.g:
=query(Sheet2!A:B,,1)
Alternatively use:
=index(Sheet2!A:B)
or
={Sheet2!A:B}

Related

use ARRAYFORMULA to find all matching rows/values in a source table from a lookup table

I have a source table like so. As you can see, each each Lookup Value can have one or more Result.
| Lookup Value | Result |
|--------------|--------|
| a | a1 |
| a | a2 |
| a | a3 |
| b | b1 |
| b | b2 |
| c | c1 |
| c | c2 |
Then I have an input table like so:
| queries | results | | | | | |
|---------|---------|---------|---------|---------|---------|---------|
| a | ... | ... | ... | ... | ... | ... |
| c | ... | ... | ... | ... | ... | ... |
The ... for each row should be the transposed values from the lookup table. So, for example, the above table would look like this:
| queries | results | | |
|---------|---------|----|----|
| a | a1 | a2 | a3 |
| c | c1 | c2 | |
Right now I have to use multiple formulas like so:
I am trying to replace it with a single ARRAYFORMULA but it doesn't seem to work.
Is there another way to do this? Basically lookup all the matching rows from a lookup table and then transpose them?
Suppose your "Lookup Value" and "Result" data run from A1:B (with headers in A1 and B1). And suppose that your "queries" list is in D1:D (header in D1) with the "results" header in E1.
Depending on the maximum number of possible matches in B:B for any value in A:A, you could use this in E2:
=ArrayFormula(IFERROR(VLOOKUP(D2:D,QUERY(FILTER({A2:B,COUNTIFS(A2:A,A2:A,ROW(A2:A),"<="&ROW(A2:A))},A2:A<>""),"Select Col1, MAX(Col2) Group By Col1 Pivot Col3"),SEQUENCE(1,10,2),0)))
If your maximum possible matches is fewer than 10 or more than 10, feel free to edit the second argument of the SEQUENCE function accordingly.
Understand that, with such an array formula that is asked to process a range, you wouldn't be able to put other data anywhere below or to the right of your "queries and results" that you've asked the array formula to assess or fill. So if you want data under it, you'll need to limit your VLOOKUP from D2:D to, say, D2:D50 (or whatever your max queries range would be). Likewise, if that second argument of the SEQUENCE function is 10, you'll have "reserved" 10 columns (i.e., E:N) for possible results, and you won't be able to put data there or you'll "break" the array formula. That being the case, you may want to give yourself some sort of visual line of demarcation around the area you've reserved for the formula's use (e.g., change the background color of the block or place a border around it, etc.).
try:
=ARRAYFORMULA(IFERROR(VLOOKUP(D:D, SPLIT(TRANSPOSE(QUERY(TRANSPOSE(IF(ISNUMBER(
QUERY(QUERY(A:B, "select A,count(A) where A is not null group by A pivot B"), "offset 1", 0)),
QUERY(QUERY(A:B, "select A,count(A) where A is not null group by A pivot B"), "limit 0", 1),
QUERY(QUERY(A:B, "select A,count(A) where A is not null group by A pivot B"), "offset 1", 0)))
,,999^99)), " "), TRANSPOSE(ROW(INDIRECT("A2:A"&COUNTUNIQUE(B:B)))), 0)))

Join two tables and count values from second table in Google Sheets

I have two Sheets with data like so-
Sheet 1:
A
--------
1 | Name |
2 | sue |
3 | bob |
4 | mary |
5 | john |
Sheet 2:
A B C D
---------------------------------------------
1 | ID | Asignee | Due | Days Left |
2 | ID001 | sue, bob | 1 | 5 |
3 | ID002 | sue, mary | 2 | 8 |
4 | ID003 | bob | 3 | 2 |
5 | ID004 | bob, john | 1 | 9 |
6 | ID005 | bob, mary, john | 4 | 1 |
7 | ID006 | sue, bob | 1 | 8 |
8 | ID007 | john, sue, mary | 2 | 6 |
On a 3rd sheet, I want to join and combine the data to get some totals/counts.
Sheet 3:
A B C D
---------------------------------------------------------
1 | Name | Number Rows | Total Due | Minimum of Days Left |
2 | sue | 4 | 6 | 5 |
3 | bob | 5 | 10 | 1 |
4 | mary | 3 | 8 | 1 |
5 | john | 3 | 7 | 1 |
For the 3rd sheet:
It has the same # of rows and values as Sheet 1
Column Sheet 3!B is the # of rows in Sheet 2 where Sheet 2!B contains Sheet 1!A (or Sheet 3!A)
There are 4 rows in Sheet 2 where Sheet 2!B contain sue
There are 5 rows in Sheet 2 where Sheet 2!B contain bob
There are 3 rows in Sheet 2 where Sheet 2!B contain bob
There are 3 rows in Sheet 2 where Sheet 2!B contain bob
Column Sheet 3!C is the total of Sheet 2!C where Sheet 2!B contains Sheet 1!A (or Sheet 3!A)
Column Sheet 3!D is the smallest value of Sheet 2!D where Sheet 2!B contains Sheet 1!A (or Sheet 3!A)
I've been staring at a blank sheet and am not sure where to start. I think I have to use filter, and arrayformula but I'm not sure how or where to start.
=ARRAYFORMULA(QUERY(SPLIT(TRIM(TRANSPOSE(SPLIT(TEXTJOIN("♀", 1,
IF(IFERROR(SPLIT(B2:B, ","))<>"",
SPLIT(B2:B, ",")&"♦"&C2:C&"♦"&D2:D, )), "♀"))), "♦"),
"select Col1,count(Col1),sum(Col2),min(Col3)
group by Col1
label count(Col1)'',sum(Col2)'',min(Col3)''"))
Edit by #IMtheNachoMan to add details on why/how I think the above formula works:
split the values in column B and concatenate the values in column C and column D with an arbritary value that is assured to not be used in any of the columns
because everything is wrapped in an arrayformula, each value from the column B split will get concatenated
splitting column B will create an errror for rows that don't have a value in column B
so the if and iferror will check if the split will create an error and if it does it will return null instead of the concatenated string from the first bullet
at this point we have one row for each row in the source table with column B split and concatenated with column C and D
join all the rows using a second arbritary value that is assured not to be in any of the columns
be sure to ignore empty values
empty values will be there from the rows that didn't have any values in the split from the first bullet
split the joined data (that doesn't have empty rows cause of the previous bullet) on the 2nd arbritary value that was used
transpose it back into rows
trim each row to remove spaces (not sure how/where the spaces got added though)
split the column in each row with the first arbritary value
use this as the input for a query call and use aggregate functions to get the data we want
if you really need to preserve order do:
=ARRAYFORMULA(IFERROR(VLOOKUP(Sheet1!A2:A,
QUERY(SPLIT(TRIM(TRANSPOSE(SPLIT(TEXTJOIN("♀", 1,
IF(IFERROR(SPLIT(B2:B, ","))<>"",
SPLIT(B2:B, ",")&"♦"&C2:C&"♦"&D2:D, )), "♀"))), "♦"),
"select Col1,count(Col1),sum(Col2),min(Col3)
group by Col1
label count(Col1)'',sum(Col2)'',min(Col3)''"), {1, 2, 3, 4}, 0)))

How to import data from one sheet to "only one cell" in another sheet

Using google sheets, I am able to transfer data from one sheet to another sheet using
=IMPORTRANGE
but IMPORTRANGE creates more cells depending on how many cells you are copying over. I need to copy all data from sheet1 into only one cell of sheet 2. Is there a way to do that?
Thanks!
After you authorize the access to the external spreadsheet, try
=JOIN(CHAR(10),QUERY(TRANSPOSE(IMPORTRANGE(spreadsheet_key,reference_string)),,2E6))
If the source data is
+---+----+----+
| | A | B |
+---+----+----+
| 1 | A1 | B1 |
| 2 | A2 | B2 |
+---+----+----+
the result will be
+---+---------+
| | A |
+---+---------+
| 1 | A1 B1 |
| | A2 B2 |
+---+---------+

Sum of row values in Google Spreadsheets

I'm new to Google Sreadsheets syntax, so forgive me if this sounds too trivial. :)
I want to sum up the row values of certain columns in my Google Spreadsheets sheet into a new column.
I'm looking for the right command to do this.
Minimal Working Example
| Column A | Column B | Column C | Column D | Sum B + D |
|----------|----------|----------|----------|-----------|
Row 1| a1 | b1 | c1 | d1 | b1 + d1 |
Row 2| a2 | b2 | c2 | d2 | b2 + d2 |
Row 3| a3 | b3 | c3 | d3 | b3 + d3 |
I want to construct the last column (Sum B + D) with a spreadsheet formula.
In cell E1 add one of the following formulas
=B1 + D1
or
=ADD(B1,D1)
or
=SUM(B1,D1)
Note: The argument separator could be ; (semicolon) instead of , (comma). It depends on the regional setting being used.
Then fill down as necessary.
If you want to avoid to have to do fill down then use the following formula
=ArrayFormula(B1:B3 + D1:D3)
You could use the following if you wnat to sum every row in columns A, D and E
=SUM(A:A,D:D,E:E)
or this if you just want to sum, say, the first 15 rows
=SUM(A1:A15,D1:D15,E1:E15)
Basically SUM can take more than one range parameter!
It may not be what you need right now but SUMIFS are also very useful. This allows you to sum one range based on one or more conditions in other ranges.

Sort range by order of values in separate column

Given a Google Sheet with the following data:
table 1
| A | B | C
1 | q | w | e
2 | a | s | d
3 | z | x | c
table 2
| A
1 | a
2 | z
3 | q
What formula(s) can sort the rows in 'table 1' according to the stored order of values in 'table 2'?
Edit: How can this be done using a formula in only one cell?
Does this formula work as you want (assuming your tables are named ranges):
=SORT(table1,MATCH(FILTER(A:A,LEN(A:A)),table2,0),1)
See this example Sheet to see it working: https://goo.gl/veSFI4
(1) If you want to sort table 1 as a one-off, add the following in D1 and pull down, then sort on column D:-
=MATCH(A1,table2!A$1:A$3,0)
(2) If you want a table which updates dynamically, in a new sheet (say table 3) enter this in A1 and pull down:-
=table2!A1
and enter this in B1 of the new sheet and pull down and across:-
=INDEX(table1!B$1:B$3,MATCH($A1,table1!$A$1:$A$3,0))
I'm assuming there are no duplicates.

Resources