I have a spreadsheet that looks like this:
Name 8/13/2020 | 8/17/2020 | 8/20/2020
John OT OT OT
Bob OT AL OT
Echo A LE OT
I would like to enter in one cell a date and then have it output me how many people have "OT", "AL", or "LE" in their column. As such I thought about using the query to do this:
=QUERY(MySheet!B:P, "SELECT COUNT(D) WHERE D = 'OT' OR D = 'AL' OR D = 'LE' LABEL COUNT(D) ''")
However, I always have to specify the column on which it has to count, is there a way I can not specify count and instead have it match up the correct column based on a date I enter in another field?
If you know of a different way I can do this that does not uses query, that is fine too as an answer.
try:
=COUNTIF(INDIRECT(
ADDRESS(6, MATCH(B2, 5:5, 0))&":"&
ADDRESS(ROWS(A:A), MATCH(B2, 5:5, 0))), "OT")
or:
=ARRAYFORMULA({A6:A, INDIRECT(
ADDRESS(6, MATCH(B2, 5:5, 0))&":"&
ADDRESS(ROWS(A:A), MATCH(B2, 5:5, 0)))})
or:
=ARRAYFORMULA(QUERY({A6:A, INDIRECT(
ADDRESS(6, MATCH(B2, 5:5, 0))&":"&
ADDRESS(ROWS(A:A), MATCH(B2, 5:5, 0)))},
"select Col1,count(Col1)
where Col1 is not null
group by Col1
pivot Col2"))
Related
Since Query is such a powerful formula, i try to avoid my team members to avoid messing up the pivot. I try to to come out with query to replace my pivot. Based on the raw data Google Sheet Trix, i try to sum column based on the criteria i have described in Grey colour (B2:E2). Appreciate if someone could lay some foundation for me, so i can further tweaks based on the formula you create here.
I created a new tab and entered the following in cell A1:
=query(
{'RAW DATA'!B:B,'RAW DATA'!E:E,'RAW DATA'!G:J},
"select Col1, sum(Col3), sum(Col4), sum(Col5), sum(Col6)
where Col1 is not null and Col2<>'Black and Yellow'
group by Col1"
)
Use a query() with a group by clause, like this:
=query(
'RAW DATA'!B1:J,
"select B, sum(G), sum(H), sum(I), sum(J)
where B is not null and E <> 'Black' and E <> 'Yellow'
group by B",
1
)
The result table will only include weeks where there is at least one data row where E is not Black or Yellow.
See the new Solution sheet in your sample spreadsheet.
Here is your solution. which also included in new tab named Solution Pivot in Query.
=ArrayFormula(QUERY(SPLIT(FLATTEN(TO_TEXT('RAW DATA'!B2:B)&"|"&'RAW DATA'!E2:E&"|"&'RAW DATA'!G1:J1&"|"&'RAW DATA'!G2:J),"|"),"Select Col1, Sum(Col4) where Col2 is not null And Col2 <> 'Yellow' And Col2 <> 'Black' Group by Col1 Pivot Col3 label Col1 'Date'"))
I'm using Google Sheets and my database looks like this:
I have a query formula that changes dynamically based on the month:
my query:
=QUERY('database',"SELECT B,C WHERE A = '"&A1&"'")
At the beginning of the months I don't have data, so I want to display something like this:
I want the column userID permanent in all month, but the column name to display "NO DATA"
I already tried with IFNA or IFERROR but that won't work, because if there's no data, the query is still successful because it displays the column names.
You can try this longer combination of several formulas:
=arrayformula(
query({A2:C;split(E1&"_"&unique(filter(B2:B,B2:B<>""))&"_NO DATA", "_")},
"select Col2, max(Col3) where Col1 = '"&E1&"'
group by Col2
order by Col2 label max(Col3) ''", 0)
)
Where:
A2:C is the dictionary
B2:B is the column of userIDs
E1 is the Month
Outputs:
This is my data:
(The combination of Lot No. & Date is always unique)
Required result:
I used this:
=QUERY(Production!A:G, "SELECT A,B, C, D, G WHERE A is NOT NULL label C 'NEW LOT NO.', D 'NEW LOT NO. DATE' ", 1)
I am not able to figure how can I add column OLD ASSIGNED in my query formula, Please Help!
Here is the SHEET.
Thank you.
EDIT:
Can someone please provide me a solution which uses only query ( or nested query) and no other formulas like vlookup or filter or anything.
Try this:
=arrayformula(QUERY({Production!A:G,iferror(vlookup(Production!A1:A&"-"&Production!B1:B,{Production!C:C&"-"&Production!D:D,Production!G:G},2,false),)}, "SELECT Col1,Col2,Col8,Col3,Col4,Col7 WHERE Col1 is NOT NULL label Col3 'NEW LOT NO.', Col4 'NEW LOT NO. DATE', Col8 'OLD ASSIGNED' ", 1))
Or for query only:
=
query(
{
query({Production!A:G},"select Col7 where Col6 is not null order by Col4,Col3 label Col7 'OLD ASSIGNED'",1),
query({Production!A:G},"select Col1,Col2,Col3,Col4,Col7 where Col1 is not null order by Col2,Col1 label Col3 'NEW LOT NO.', Col4 'NEW LOT NO. DATE' ",1)
},
"select Col2,Col3,Col1,Col4,Col5,Col6 order by Col5,Col3",1)
This uses query only so you'll need to choose a sort order for the results since Lot No. 277 appears in Col A and Col C with different date order.
If you want your original sort order, then it can work like this if you're prepared to use sequence:
=
query(
{
query({Production!A:G},"select Col7 where Col6 is not null order by Col4,Col3 label Col7 'OLD ASSIGNED'",1),
query({Production!A:G,sequence(counta(Production!A:A)+countblank(Production!A:A),1)},"select Col1,Col2,Col3,Col4,Col7,Col8 where Col1 is not null order by Col2,Col1 label Col3 'NEW LOT NO.', Col4 'NEW LOT NO. DATE' ",1)
},
"select Col2,Col3,Col1,Col4,Col5,Col6 order by Col7",1)
sequence is used to generate a sequence of row numbers (because row() would require the arrayformula - which you don't want). The range of number in the sequence is counta(Production!A:A)+countblank(Production!A:A) which is the number of rows in the Production sheet.
In cell C1:C of my table I got 6 rows with ticket id's. I like to search different spreadsheets to search for those ticket id's and calculate the total hours spent on that ticket.
I have it working using the following formula:
=QUERY({IMPORTRANGE("SPREADSHEETID";"B3:B")\ARRAYFORMULA(TO_PURE_NUMBER(IMPORTRANGE("SPREADSHEETID";"F3:F")-IMPORTRANGE("SPREADSHEETID";"E3:E")))};"SELECT SUM(Col2) WHERE Col1 = '"&C1&"' GROUP BY Col1 LABEL SUM(Col2) ''")
In this example, C1 is where the ticket ID can be found.
Now I thought I could just wrap QUERY in a ARRAYFORMULA and use C1:C instead of just C1 but that won't work. Now I could just copy and paste the above formula in every cell but there must be a cleaner way.
ANSWER
I used the following formula to make it work, thanks to Max's answer below.
=QUERY(
{
IMPORTRANGE("SPREADSHEETID";"B3:B")\
ARRAYFORMULA(
TO_PURE_NUMBER(
IMPORTRANGE("SPREADSHEETID";"F3:F") - IMPORTRANGE("SPREADSHEETID";"E3:E")
)
)
};
"
SELECT Col1, SUM(Col2)
WHERE Col1 = '" & JOIN("' OR Col1 = '";FILTER(C:C; C:C <> "")) & "'
GROUP BY Col1
LABEL SUM(Col2) ''
")
Sample formula is:
=QUERY({A:B},"select * where Col1 = '"&JOIN("' or Col1 = '",FILTER(D2:D,D2:D<>""))&"'")
No, one cannot create an array of query strings and use arrayformula(query(...)) to run them all at once.
Alternative: instead of
SELECT SUM(Col2) WHERE Col1 = '"&C1&"' GROUP BY Col1 LABEL SUM(Col2) ''
use the query
SELECT Col1, SUM(Col2) GROUP BY Col1
elsewhere on the sheet, and then use vlookup to look up the sum for each value of Col1 that you want. vlookup can be used inside of arrayformula like this:
=arrayformula(vlookup(C1:C10, E:F, 2, 0))
looks up each of values in C1..C10 in the column E (exact match required) and returns the corresponding value in column F (2nd column of the searched range).
I've got data like this:
A | B
-----------------------
Date | Data
30/06/2015 | 1.2
01/07/2015 | 2
01/07/2015 | 3
02/07/2015 | 2
02/07/2015 | 3
And I write a simple query like this:
=query(A:B; "select YEAR(A) || ' ' || month(A), SUM(B)
group by YEAR(A), month(A)")
But I've got a "Value Error", so I write this
=query(A:B; "select YEAR(A), MONTH(A), SUM(B)
group by YEAR(A), month(A)")
And it work well, but, how can I concat two values in one cell ?
I though of this:
=QUERY(Journal!A:B;"select A, SUM(B)
group by A
format A 'yyyy-MM'")
But the format operation is done AFTER the group by so it's like the group by never was.
Any idea ?
[Solution]
=query({Arrayformula(text(Foo!A:A; "yyyy-MM")) \Foo!B:B}; "select Col1, sum(Col2) group by Col1"; -1)
Because my local is "french" I've to use the '\' in the Arrrayformula and ; instead of ','...
Use the ampersand (&) to concatenate in arrayformula:
=ArrayFormula(if(len(A:A), year(A:A)&" "&month(A2:A),))
or if you don't want the space between year and month:
=ArrayFormula(if(len(A:A), year(A:A)&month(A2:A),))
EDIT:
If you want to query and sum, try:
=query({Arrayformula(text(Journal!A:A, "yyyy-MM")),Journal!B:B}, "select Col1, sum(Col2) where Col2 is not null group by Col1", 0)
or in locale that use semi-colons as argument separators:
=query({Arrayformula(text(Journal!A:A; "yyyy-MM"))\Journal!B:B}; "select Col1, sum(Col2) where Col2 is not null group by Col1"; 0)
Example spreadsheet