Create dynamic Year-Month headers based on a start Year - Google Query - google-sheets

Is there a way to create a single header row of 12 dynamic Year-Month columns (formatted yyyy-mmm) using Google Query? These dates should change based on a Year selection in say, Cell C1. e.g. C1 could have values like 2016,2017,2018,2019,2020.
Example with formulas e.g.
=arrayformula(EDATE(date(C1,1,1),SEQUENCE(1,12,)))
or
=Arrayformula(EOMONTH(date(C1,1,1),SEQUENCE(1,12,)))

i was able to formulate the following:
=ARRAYFORMULA(TRANSPOSE(QUERY(TRANSPOSE(QUERY(EDATE(date(C1,1,1),SEQUENCE(1,12,)))),"SELECT Col1 LABEL Col1 'DATES' FORMAT Col1 'yyyy-mmm'")))
anyone has a shorter way, do let me know.
Edit:
Alternatively, a shorter version, suggested by Tom Sharpe:
=ArrayFormula(date(C1,sequence(1,12),1))

Related

Use dynamic created Query text as a Query formula in Google Sheets [duplicate]

This question already has answers here:
Stacking Multiple Arrays In Query/Lambda Function
(2 answers)
Closed 3 months ago.
First of all this is my first question here...
Ok, and now my problem:
I created a formula that generates a dynamic Query if you are searching for data in multiple sheets. I just write the names of the sheets in green field and it changes the Query.
The text for the Query is correct, but now I want this to by the actual formula in a cell to display the data.
If I use the = and add the cell with the Query text, it copy the text. I tried the INDIRECT formula, but it does the same.
How can I use a this dynamic text to be my Query to dispaly the data depending on the amount of names in the green field?
EDIT: As advised by user doubleunary (as mentioned, this is my first time), I'm going to rephrase the problem.
In an spreadsheet a manager imputs for every task the amount a worker has made. He creates an new sheet for each date and imputs the data for that day. Each day can have different rows of data. The boss wants a spreadsheet that display in one sheet all that has been done in the current month. I used Query because you can easy join data from multiple sheets and ignor the empty rows.
But the problem is, that every day a new sheet is added with a new "name" (date). And the easyest way I found tho make a dynamic Query, without the Boss to manualy edit the Query, was to create a text with TEXTJOIN formula, in witch he ony needs to enter the new "name" (date).
If there is a better was, please share, but it should not be in App Script if possible.
Thank you.
Easily you can't transform a text into a formula.
In this case, you should create the query formula and insert the ranges with Indirect function, like this:
=Query({Importrange(LINK,Indirect("'"&O4&"'!I38:S107"));Importrange(LINK,Indirect("'"&O5&"'!I38:S107"))},"Select * Where Col1 is not null",0)
If the formula you quote and the data sheets are all in the same spreadsheet file, you should not to use importrange() but indirect(), like this:
=query(
{
indirect(O4 & "!I38:S107");
indirect(O5 & "!I38:S107")
},
"where Col1 is not null",
0
)
In the event the row references such as I38:S107 change from time to time as well, you can put those references in a cell and refer to them the same way.
try (assuming url is same for all sheets):
=QUERY(LAMBDA(x, QUERY(REDUCE(SEQUENCE(1, 11), x,
LAMBDA(a, c, {a; IMPORTRANGE("url", c"!I38:S107")})),
"where Col1 is not null", 1))
(O4:INDEX(O:O, MAX((O:O<>"")*ROW(O:O)))), "offset 1", )
more examples: https://stackoverflow.com/a/74280310/5632629

Only apply complex arrayformula() to rows with certain value in dataset

I have a quite complext formula (i mean that is complex to me) that Tom Sharpe helped me building to aggregate values and ordering them by months in a row(you can find the details in the original post but i think you'll only need the final formula which is:
=ArrayFormula(mmult(sequence(1,counta(A2:A),1,0), if((C2:index(C:C,counta(C:C))<=eomonth(G2,sequence(1,datedif(G2,H2,"M")+1,0)))* (D2:index(D:D,counta(D:D))>=eomonth(G2,sequence(1,datedif(G2,H2,"M")+1,0))),E2:index(E:E,counta(E:E)),0)))
and here is the result -> [J1:U1]
Now, what i would need to do as the final step is to be able to group data by a certain label (John or Jane in the example) on separate rows, but mantaining the order/aggregate by month on the row. On the example, this would mean having one row with only 'John' data and below, one with 'Jane' values.
I am struggling to understand how to adapt the formula to do so.
I have tried:
Using another array to first return a list of these labels with query(unique()) or something like that, but then i struggle looping in it with the other formula.
A bit more simplistic but it could work after all: on the 1st row (the cell next to where the data will be returned) writing 'John', on row 2 'Jane' and then using filter() to only pull data that matches. The 'John, Jane' value is for the example but the real labels won't be that many, the list of labels don't need to be dynamic.
The thing with these solutions is that they work when used separately, but i can't figure out how to nest this in the first arrayformula() that Tom helped me with...As i am just beginning with the google sheets queries.
I don't really need necessarily the complete formula/code but maybe just directions or tips to visualize the way i could solve this.
Thanks to all who might contribute
With hindsight I might have done better to go down the route of using a query to calculate the sums on my previous answer rather than Mmult.
This uses the same method as before to create a 2d array of amounts vs dates (going across) and individuals (going down). Then it uses Textjoin to generate a query to group by name with the required number of columns.
=ArrayFormula(query({A2:A,if((C2:C<=eomonth(G2,sequence(1,datedif(G2,H2,"M")+1,0)))* (D2:D>=eomonth(G2,sequence(1,datedif(G2,H2,"M")+1,0))),E2:E,0)},
"select Col1,sum(Col"&textjoin("),sum(Col",,sequence(1,datedif(G2,H2,"M")+1,2))&") where Col1 is not null group by Col1"))
This is the generated query
select Col1,sum(Col2),sum(Col3),sum(Col4),sum(Col5),sum(Col6),sum(Col7),sum(Col8),sum(Col9),sum(Col10),sum(Col11),sum(Col12),sum(Col13) where Col1 is not null group by Col1
Ideally there should be an extra section saying label sum(Col2) '' etc. to suppress the 'Sum' headers.
=ArrayFormula(query({A2:A,if((C2:C<=eomonth(G2,sequence(1,datedif(G2,H2,"M")+1,0)))* (D2:D>=eomonth(G2,sequence(1,datedif(G2,H2,"M")+1,0))),E2:E,0)},
"select Col1,sum(Col"&textjoin("),sum(Col",,sequence(1,datedif(G2,H2,"M")+1,2))&") where Col1 is not null group by Col1 label sum(Col" & textjoin(") '', sum(Col",,sequence(1,datedif(G2,H2,"M")+1,2)) & ") ''"))

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 it possible to transpose a column of text values into a single row without duplicates based on an if statement?

E.g. I have the following sheet and formula, but I only want to transpose the data if it contains a specific month, specified in A2.
try:
=TRANSPOSE(UNIQUE(FILTER(A5:A; B5:B=A2)))
You can use a query for that.
=TRANSPOSE(QUERY(A5:C, "select A where month(B)+2="&MONTH(A2)&""))
The reason we add +1 is because months in a query start from 0

Google Sheets - Query - Running Total below dynamic results

Testing Sheet:
Wondering if there is a witty way to add a Total to the last row +1 of
a Query result.
See Sheet 'Lookup' for a static example of what I am asking for.
I don't know if there is a way to have a hidden column that calculates
transposed only under the last row of a query, or if there is a smart
way to work Query for this answer.
All great answers. Each on very useful in its use case.
Макс Махров gets the answer with using a query statement.
Now I was not keen on having an extra sheet to hold the totals so I added a row at the top which I can simply hide and used this formula:
query({Orders!A:E;A1:E1},"select Col1, Col3, Col4 where Col2 = '"&C3&"' order by Col4",1)
Only problem I have is trying to figure out how to add TEXT to the bottom row, it seems to only want numerical input.
How do I fix this? What am I glitching?
Thanks !
Mars
The trick is to make second query and count totals for selected product.
Plan of actions:
add new sheet with query on it, something like this: =QUERY(Orders!A:E,"select B, 0, sum(D) where B like '"&Lookup!C2&"' Group by B",0)
Prepare arrayformula which combines data in Lookup sheet: = ArrayFormula({Importrange(1),Importrange(2)}) Note that number of columns must retain the same.
Edit query so it takes Col1, Col2, Col3... instead of A, B, C...
Make word 'total' visible instead of zero. Set number format: 0;0;total Set it for range B9:B on Lookup sheet
Make Conditional Formatting with formula =and($B4 =0,isnumber($B4)) for range A4:C on Lookup sheet.
That's seems have to complete the task.
Hope it Helps!
Your Example
Working example.
Here is one way:
Put TOTAL way down in row 1000
Select the range A3:C999. Select data > filter to create filters
Select C3, set the filter to hide all blanks
A second way is to limit the query result to show only the top 8 results:
Change your query to =query(Orders!A:E, "select A, C, D where B = '"&C2&"' order by D desc limit 8",1) It will reverse-order column D (largest first), and set row limit to 8.
Change the formula of your TOTAL to =sumif(Orders!B:B,C2,Orders!D:D)
Try this formula in the column adjacent to your query:
=ArrayFormula({$C$4:offset($C$4,count($C$4:$C),0,1,1);sum($C$4:offset($C$4,count($C$4:$C),0,1,1))})
It duplicates your column of values (I haven't figured out a way around that yet) and then adds a total to the bottom of that column, and changes dynamically with the range from your query.
Here's a working version.
Interesting challenge! It got the old grey matter turning... ;)
Thanks,
Ben

Resources