[Goal]
I want to create a table with the Query Function where it counts the number of 'Drivers' for each month in a dynamic manner. Meaning that when the data (example sheet is called 'Data') is updated, it'll be updated automatically as well.
[What I was able to do so far]
I was able to create a table with the Query Function, however, it only displays 1 column worth of Months when I want to show up to 4 months. And I also want to show the recent months from the left and the older months on the right.
[Formula that I have so far]
=QUERY(Data!$A:$B,"
SELECT B,
Count(B)
Where B != '' AND MONTH(A)=MONTH(DATE'"&TEXT(A2,"YYYY-MM-DD")&"')
Group By B
Pivot A
Order By B asc
Label B 'Drivers', Count(B) '"&TEXT(A2,"MMMM YYYY")&"'",1)
[Issue that I'm facing]
I've tried specifying the date range like the below, however there are 2 problems.
The date format is not mmmm yyyy (Example: May 2022) and it'd show as: 2022-2-1 May 2022
The months are ordered in an ascending manner (Example: 2022-2-1, 2022-3-1, 2022-4-1) instead of descending (Example: 2022-4-1, 2022-3-1, 2022-2-1)
So I'm not sure what I need to do to fix this. Hopefully I can have support.
Where B != '' AND MONTH(A)<=MONTH(DATE'"&TEXT(A2,"YYYY-MM-DD")&"')
AND MONTH(A)>=MONTH(DATE'"&TEXT(EDATE(A2,-3),"YYYY-MM-DD")&"')
[Sample Sheet]
https://docs.google.com/spreadsheets/d/1AJYTRga9-dXbj64nl4RfpDKs5JYSFwP2SiR7v7gAhMI/edit#gid=1297239620
=ARRAYFORMULA(REGEXREPLACE(""&TRANSPOSE(QUERY(TRANSPOSE(
QUERY({Data!A:B, TEXT(Data!A:A, "yyyymmdd×MMMM yyyy")},
"select Col2,count(Col2)
where Col2 != ''
AND MONTH(Col1)<=MONTH(DATE'"&TEXT(A2,"YYYY-MM-DD")&"')
AND MONTH(Col1)>=MONTH(DATE'"&TEXT(EDATE(A2,-3),"YYYY-MM-DD")&"')
group by Col2
pivot Col3", 1)),"order by Col1 desc")),"^(.*×)", ))
Reference:
How do I change the date format in a Google Sheets query pivot table with date filters?
Sort Query Pivot Table - Google Sheets
Related
I would like to query in google sheets and sort the query by a specific column in accending order, and have a secondary sort that is also in ascending order. I already know how to do this by
=QUERY(A:C,"select * where month(A)+1 = 1 order by A,B ",0)
Here i queried 3 columns month, unique ID, and name. I selected the data with the necessary month, and sorted it by month, followed by a secondary sort of unique ID. But this query outputs 3 columns. How would i change the formula so the output does not include the month column anymore.
Your question is:
How would i change the formula so the output does not include the month column anymore.
Try wrapping your QUERY formula within another QUERY. Like:
=QUERY(QUERY(your_query_here),"select Col2, Col3")
For your given example it would be:
=QUERY(QUERY(A1:C22,"select * where month(A)+1 = 1 order by A,B ",0),
"select Col1, Col3")
=QUERY(IMPORTRANGE(some_range);"
SELECT Col2,dateDiff(Col20,now())
WHERE Col20 IS NOT NULL AND dateDiff(Col20,now()) <= 30
ORDER BY Col2 ASC
LABEL Col2 'SANITARNA'
";0)
So, i have this query formula which works perfectly for a column that has only dates. However i need to apply it to a column where there are dates and some text values. The problem is when i change the dateDiff column i get an error "Unable to parse query string for Function QUERY parameter 2: Can't perform the function 'dateDiff' on values that are not a Date or a DateTime values" which makes sense. However, i cant seem to figure out how to incorporate a filter within the dateDiff function to just skip the text values and only output the ones that have dates. My best guess so far is that the filter has to be applied within the dateDiff function in SELECT and not WHERE. I've tried a filter/isnumber formula but get parse error and my brain is fried and can't see the problem.
Test sheet: https://docs.google.com/spreadsheets/d/1thpXBSp-Vt1E5MGaM89Xko6GvekjmzyidO94Sil2AjQ/edit?usp=sharing
See my newly added sheet ("Erik Help"), which contains the following version of your original formula:
=QUERY(FILTER(A:C,ISNUMBER(C:C))," SELECT Col1,dateDiff(Col3,now()) WHERE Col2 IS NOT NULL AND dateDiff(Col3,now()) <= 30 ORDER BY Col1 ASC LABEL Col1 'PRIJAVA DO' ",0)
FILTER will first filter in only those rows where the value in Col C is a number. And since all dates are numbers as far as Google Sheets is concerned, that will be only your rows with dates in Col C.
NOTE: You currently have no date values in Col C that are less than or equal to 30 days from TODAY() — that is, all of your Col-C dates are in the future — so your table is returning empty (yet without error, because it is working). Because QUERY is acting on a FILTER of the original data and not on the original data itself, all QUERY column references must be in Colx notation, not A-B-C notation.
The solution Erik Tyler provided works, ill just add here the syntax for the importrange as well for anyone with similar problem.
=QUERY(FILTER(
IMPORTRANGE("some_range";"some_sheets!A:QQ");
ISNUMBER(
IMPORTRANGE("some_range";"some_sheets!C:C")));"
SELECT Col2,dateDiff(Col20,now())
WHERE Col20 IS NOT NULL AND dateDiff(Col20,now()) <= 30
ORDER BY Col2 ASC
LABEL Col2 'SANITARNA'
";0)
I wish to count events that occurred within a custom time interval : it could be within 24h, or within a week or 2-months span.
I am using google sheets: I can create a pivot table and group by month, however I'd like to explore insights using custom intervals (I'm looking for pattern in epilepsy).
As final result, I wanna have a table that, for each day, it is reported the number of frequencies within that interval.
Particularly, I wanna focus on the interval of 24h to count the number of events of epilepsy (known as cluster seizures).
And then, on custom days intervals to explore periodicity or trends - like each 48 hours, or each 15 or 30 days.
See a mockup of Google Sheet here:
https://docs.google.com/spreadsheets/d/1tCxYV5mUcq6vKm8-fL-0HUAOjcB9fipLCqPD2Znv-X0/edit#gid=1372548551
I tried this attempts:
find out how many events occurred in the last 30 days prior to the reported date:
= IFERROR(
QUERY(
A:E,
"SELECT COUNT(A)
WHERE
A IS NOT NULL AND
E = FALSE AND
A >= date '" &
TEXT(
A2-30,
"yyyy-MM-dd"
) &"' AND
A <= date '" &
TEXT(
A2,
"yyyy-MM-dd"
) &"'
LABEL COUNT(A) '' "), "N/A")
Then, dragging the cell, I get the column "# events in the prior 30 days".
It works but seems a bit messy - especially for updating the intervals.
I tried this other approach:
=query(B:E, "select B, count(E), -1+count(E) where E = FALSE group by B label B 'Date with Clusters', count(E) 'Cluster seizures '")
That produces the last table.
I like this approach better, but here I am just grouping by the same date, without possibility to have a custom interval.
As an example, I will have that two events will be counted within the same day, not withing the same 24h interval.
Could you tell a better approach to handle datetime differences, so to create binning and group by with custom intervals ?
Below an example:
on the left table, data in input; on the middle column, result of first approach; on the right table, results of second approach.
given the table:
in order to group stuff with QUERY we need to "fix" the A column in order to get a custom period. lets say we need to group events every 3 weeks (21 days). we take the lowest and highest date and create a sequence with all the dates in between.
=INDEX(ROW(INDIRECT(MIN(A2:A)&":"&MAX(A2:A))))
then we use running total on it to get every date which is 21 days apart from the previous/next one. we could use simple SEQUENCE (for min>max) to create this array but with SEQUENCE we cant go "back in time" (for max>min) so we use MMULT and negative number
therefore, to start from a frame of the first date and create 3 weeks group by windows (eg. min>max) we use:
=ARRAYFORMULA({MIN(A2:A); MIN(A2:A)+MMULT(TRANSPOSE((
ROW(INDIRECT(MIN(A2:A)&":"&MAX(A2:A)))<=TRANSPOSE(
ROW(INDIRECT(MIN(A2:A)&":"&MAX(A2:A)))))*21); SiGN(
ROW(INDIRECT(MIN(A2:A)&":"&MAX(A2:A)))))})
and to get a reverse of it and start from frame of end date and create 3 weeks windows backwards (eg. max>min) we use:
=ARRAYFORMULA({MAX(A2:A); MAX(A2:A)+MMULT(TRANSPOSE((
ROW(INDIRECT(MIN(A2:A)&":"&MAX(A2:A)))<=TRANSPOSE(
ROW(INDIRECT(MIN(A2:A)&":"&MAX(A2:A)))))*-21); SiGN(
ROW(INDIRECT(MIN(A2:A)&":"&MAX(A2:A)))))})
at this stage, we can start fixing the A column via VLOOKUP and 4th argument set to 1 - approximate mode (instead of 0 - exact match mode) so forward in time will be:
=ARRAYFORMULA(IFNA(VLOOKUP(A2:A; SORT({MIN(A2:A); MIN(A2:A)+MMULT(TRANSPOSE((
ROW(INDIRECT(MIN(A2:A)&":"&MAX(A2:A)))<=TRANSPOSE(
ROW(INDIRECT(MIN(A2:A)&":"&MAX(A2:A)))))*21); SIGN(
ROW(INDIRECT(MIN(A2:A)&":"&MAX(A2:A)))))}); 1; 1)))
and backward in time shall be:
=ARRAYFORMULA(IFNA(VLOOKUP(A2:A; SORT({MAX(A2:A); MAX(A2:A)+MMULT(TRANSPOSE((
ROW(INDIRECT(MIN(A2:A)&":"&MAX(A2:A)))<=TRANSPOSE(
ROW(INDIRECT(MIN(A2:A)&":"&MAX(A2:A)))))*-21); SIGN(
ROW(INDIRECT(MIN(A2:A)&":"&MAX(A2:A)))))}); 1; 1)))
and now we just create a virtual array {} and pair fixed column A with column C and input it as range into QUERY
side note:
to put columns next to each other in english spreadsheets we use ,
to put columns next to each other in non-english spreadsheets we use \
=ARRAYFORMULA(QUERY({IFNA(VLOOKUP(A2:A; SORT({MIN(A2:A); MIN(A2:A)+MMULT(TRANSPOSE((
ROW(INDIRECT(MIN(A2:A)&":"&MAX(A2:A)))<=TRANSPOSE(
ROW(INDIRECT(MIN(A2:A)&":"&MAX(A2:A)))))*21); SIGN(
ROW(INDIRECT(MIN(A2:A)&":"&MAX(A2:A)))))}); 1; 1))\ C2:C};
"select Col1,count(Col1)
where Col2 = FALSE
group by Col1
order by count(Col1) desc
label count(Col1)''"))
and backwards in time:
=ARRAYFORMULA(QUERY({IFNA(VLOOKUP(A2:A; SORT({MAX(A2:A); MAX(A2:A)+MMULT(TRANSPOSE((
ROW(INDIRECT(MIN(A2:A)&":"&MAX(A2:A)))<=TRANSPOSE(
ROW(INDIRECT(MIN(A2:A)&":"&MAX(A2:A)))))*-21); SIGN(
ROW(INDIRECT(MIN(A2:A)&":"&MAX(A2:A)))))}); 1; 1))\ C2:C};
"select Col1,count(Col1)
where Col2 = FALSE
group by Col1
order by count(Col1) desc
label count(Col1)''"))
demo spreadsheet
The primary sheet has one column with dates; and thousands of rows on this sheet.
There are hundreds of other sheets, named by date. The format of each of these other date sheets is identical.
I need a query that can use each date on the primary sheet as reference to query a corresponding date sheet.
I would like to drag this formula down across all rows, and have the formula ref the date in the row rather than manually enter each cell.
Since I can't name the date sheets by date alone, I will need a naming formula that adds a word to the dates.
I've tried applying Named-Ranges to the date sheets and referencing them in the cells with import-range, but this did not work. I also tried inserting a link to the named ranges in each date cell, but this did not work either.
I've attached a screen grab of the idea. The data is just for illustration, it is fake. Sorry if this is a dumb question, I'm new to this, experimented, researched, could not find a solution. Thanks!
try:
=QUERY({INDIRECT("C"&ROW(C8)&"!A:H")},
"select Col5,Col8
where Col3 > date '"&TEXT(C8, "yyyy-mm-dd)&"'
and Col4 = 'SALE'
and Col5 <= "&D8 + (D8*E8)&"
and Col8 < 1
and Col8 > 0.01
order by Col8
limit 1", 0)
I have a spreadsheet with 2 sheets in it, I want to summarize the weekly results by date. I'm trying to use the query sum function to summarize everything since I wasn't able to do it with arrayformula.
but I'm not able to do it with a query as well. I don't want to just copy-paste the sum function from each row to the next I want to just type the date I need in column A and get all the results in the different columns.
https://docs.google.com/spreadsheets/d/1ZsKXw32ycO_5KGD2I-Ug_GmqSIB_Z-D3Z1jlGd6fpTE/edit?usp=sharing
link to sheets.
getting the data from the database sheet. I want to display the data-oriented by date and summed.
=ArrayFormula(IFERROR(FILTER(QUERY(DataBase!A2:E,"select A,sum(E)
where A is not null
group by A
label sum(E)''"), WEEKNUM(DataBase!A2:A)=WEEKNUM(A2:A))
))
I tried this formula and it dosent work..
=ARRAYFORMULA(IFERROR(VLOOKUP(A2:A,
QUERY(FILTER(DataBase!A2:E,WEEKNUM(DataBase!A2:A)=WEEKNUM(A2)),
"select Col1,sum(Col5)
where Col1 is not null
group by Col1
label sum(Col5)''"), 2, 0)))