How to importrange based on date in filename? - google-sheets

There's a rolling list of live spreadsheets for each day with the filenames in this format, "ranking_file-2022-10-05".
I need my importrange query to always grab today's dated filename and another that always grabs the file from 7 days ago. Is this possible? Any help would be hugely appreciated.
Here's my current query which I need two versions of based on above:
=SORTN(SORT(QUERY(IMPORTRANGE("xxxxx/edit","ranking_file!A2:O250"),"Select Col1,Col4,Col7,Col5,Col14,Col15 where Col5 contains 'xxxxx' and Col15 contains 'Google.co.uk_' and Col4 is not null order by Col4 asc", ), 3, 1), 9^9, 2, 2, 1)

try:
=SORTN(SORT(QUERY(
{IMPORTRANGE("xxxxx/edit","ranking_file-"&TEXT(TODAY(), e-mm-dd)&"!A2:O250")},
"select Col1,Col4,Col7,Col5,Col14,Col15
where Col5 contains 'xxxxx'
and Col15 contains 'Google.co.uk_'
and Col4 is not null
order by Col4 asc", ), 3, 1), 9^9, 2, 2, 1)

Related

Google sheets pivot table - Value by max date in year for each name

See example in Sheet and image below. Contains the original table, the pivot table and the expected table.
I have a table with three columns: Name, Date, and Value.
I want to take the last date in each year and sum the values by names.
In both years I want the sum to be 2.
The problem: I can't get the sum of Values for the max Date in a year grouped by Name.
try:
=LAMBDA(x, {QUERY(x, "select Col2,max(Col3) where Col1 is not null group by Col2 pivot Col1"),
QUERY(x, "select sum(Col3) where Col1 is not null group by Col2 label sum(Col3)'GT'")})
(SORTN(SORT({A2:A, YEAR(B2:B), C2:C, A2:A&YEAR(B2:B)}, B2:B, 0), 9^9, 2, 4, 1))

how to import rows that contain the same value more than once to another sheet

I have a list of sessions, I want to be able to import the ones that have been rescheduled (along with the new rescheduled session) to another sheet. So under colC one will say 'Rescheduled' and the other will say 'Attended'. So what I'd like to do is grab the participant name, but only if it shows up more than once, so I'd get both the rescheduled session and the new session. I tried using importrange and query, but couldn't quite get it right. How can I do this?
https://docs.google.com/spreadsheets/d/10ZWcO9DeBx6KjiyvFvcoBlGk7yw2b8z2gFPi26orOGc/edit?usp=sharing
try:
=INDEX(REGEXEXTRACT(FLATTEN(QUERY(QUERY(TRANSPOSE(QUERY(TRANSPOSE(QUERY({
IMPORTRANGE("10ZWcO9DeBx6KjiyvFvcoBlGk7yw2b8z2gFPi26orOGc", "Sheet1!A1:C"),
FLATTEN(QUERY(TRANSPOSE(
IMPORTRANGE("10ZWcO9DeBx6KjiyvFvcoBlGk7yw2b8z2gFPi26orOGc", "Sheet1!A1:C")),,9^9))},
"select max(Col4) where Col1 is not null group by Col2 pivot Col3")),
"where Col1 matches 'Rescheduled|Attended'", )),
"where Col1 is not null and Col2 is not null"),
"offset 1", )),
"(\d+/\d+/\d+) (.*) (Att.*|Res.*)"))
Formula Explanation:
Take the IMPORTRANGE
Merge all columns into one column
Create a virtual array {}
Pass into QUERY and pivot the data
Transpose the result
Use query to get only needed columns
Transpose back
Compare 1st and 2nd columns and remove
Remove header row
Flatten data into one column and split into the 3 needed columns
and if that's not enough:
=INDEX(REGEXREPLACE(SORT(TEXT(REGEXEXTRACT(FLATTEN(
QUERY(QUERY(TRANSPOSE(QUERY(TRANSPOSE(QUERY({
IMPORTRANGE("10ZWcO9DeBx6KjiyvFvcoBlGk7yw2b8z2gFPi26orOGc", "Sheet1!A1:C"),
FLATTEN(QUERY(TRANSPOSE(
IMPORTRANGE("10ZWcO9DeBx6KjiyvFvcoBlGk7yw2b8z2gFPi26orOGc", "Sheet1!A1:C")),,9^9))},
"select max(Col4) where Col1 is not null group by Col2 pivot Col3")),
"where Col1 matches 'Rescheduled|Attended'", )),
"where Col1 is not null and Col2 is not null"),
"offset 1", )),
"(\d+/\d+/\d+) (.*) (Att.*|Res.*)"), {
"yyyymmdd\×m/d/yyyy", "#", "#"}), 2, 0, 1, 1), "^(\d+×)", ))
demo sheet

how to count values, if they fall under a separate category and within a group

Columns D:H show my initial info, where two workstreams (Column D) have different issues occur (Column F:H). These issues fall under one of three categories, under Columns A:B. I want the chart to show the count of the category each issues falls under, making sure it's also showing that it's within a certain workstream.
what i was working with so far is below to match issues to the three categories
=INDEX(A:B, MATCH(F1, A:A, 0), 2)
and below to count the times a value is within the workstream group (A or B).
=countif(flatten(filter(F:H,D:D="A")),"one")
How can I combine these two?
https://docs.google.com/spreadsheets/d/1hs-Srt-qdOR44V_rmvKF-rC5DThRrQrrCfiA_jAvnH0/edit?usp=sharing
delete J:M range and use in D1:
=INDEX({QUERY(SPLIT(FLATTEN(D1:D9&"×"&VLOOKUP(F1:H9, A1:B12, 2, 0)), "×"),
"select Col1,count(Col1) where Col2 is not null
group by Col1 pivot Col2 label Col1'workstream'"); "TOTAL",
TRANSPOSE(MMULT(1*QUERY(
QUERY(SPLIT(FLATTEN(D1:D9&"×"&VLOOKUP(F1:H9, A1:B12, 2, 0)), "×"),
"select count(Col2) where Col2 is not null
group by Col2 pivot Col1"), "offset 1", ),
SEQUENCE(COUNTUNIQUE(D1:D9), 1, 1, 0)))})
formula explanation

Finding the top 3 values and returning the column ID

Thank you beforehand for your kind response.
Problem: I am trying to do a sheet to sort out the 3 top categories of a given range as shown by the attached screen grab.
enter image description here
I only managed to get the results by using the "large" command. "=large(range,n)". Using this I got the value but what I need is the column index/id rather than the numeric values.
Any ideas on how I would go about achieving this?
Thanks for your reply.
Try the following maybe:
Formula in G2:
=TRANSPOSE(QUERY(TRANSPOSE({A$1:E$1;A2:E2}),"Select Col1 Order by Col2 desc limit 3"))
It's definitely possible in arrayformula.
Using vlookup, a sequence of numbers for the search key:
=arrayformula(sequence(max(if(A:E<>"",row(A:A),))-1,columns(A:E)))
...and a range of columns that are sorted by each row of numbers:
=arrayformula({
sequence((max(if(A:E<>"",row(A:A),))-1)*columns(A1:E1)),
sort({
array_constrain(int((row(A2:A)-2)/(columns(A:E)))+1,(max(if(A:E<>"",row(A:A),))-1)*columns(A1:E1),1),
array_constrain(iferror(split(flatten(if(A2:E<>"",A1:E1&char(9999),)&A2:E),char(9999)),),(max(if(A:E<>"",row(A:A),))-1)*columns(A1:E1),2)
},1,1,3,0)
})
The final formula for the solution, in cell G2:
=arrayformula(
array_constrain(
vlookup(
sequence(max(if(A:E<>"",row(A:A),))-1,columns(A:E)),
{sequence((max(if(A:E<>"",row(A:A),))-1)*columns(A1:E1)),sort({array_constrain(int((row(A2:A)-2)/(columns(A:E)))+1,(max(if(A:E<>"",row(A:A),))-1)*columns(A1:E1),1),array_constrain(iferror(split(flatten(if(A2:E<>"",A1:E1&char(9999),)&A2:E),char(9999)),),(max(if(A:E<>"",row(A:A),))-1)*columns(A1:E1),2)},1,1,3,0)}
,3,0),
max(if(A:E<>"",row(A:A),))-1,3)
)
It will work with blank cells in some of the rows.
The width of the source data is determined by:
=columns(A1:E1)
and the height of the source 'numbers' by:
=max(if(A:E<>"",row(A:A),))-1 (it needs to sit within an arrayformula).
you may use this superior formula:
=ARRAYFORMULA(ARRAY_CONSTRAIN(IFERROR(SPLIT(FLATTEN(SPLIT(QUERY(FILTER(
QUERY(SPLIT(FLATTEN(IF(A2:E="",,ROW(A2:A)&"×"&A2:E&"×"&A1:E1)), "×"),
"select Col3 where Col2 is not null order by Col1,Col2 desc"), COUNTIFS(
QUERY(SPLIT(FLATTEN(IF(A2:E="",,ROW(A2:A)&"×"&A2:E&"×"&A1:E1)), "×"),
"select Col1 where Col2 is not null order by Col1,Col2 desc"),
QUERY(SPLIT(FLATTEN(IF(A2:E="",,ROW(A2:A)&"×"&A2:E&"×"&A1:E1)), "×"),
"select Col1 where Col2 is not null order by Col1,Col2 desc"),
SEQUENCE(COUNTA(A2:E), 1, ROW(A2)), "<="&SEQUENCE(COUNTA(A2:E), 1, ROW(A2)))<4)&
FLATTEN(IFERROR(SEQUENCE(MAX((A2:E<>"")*ROW(A2:A)), 3)*
{"a", "a", 0})),,9^9), 0)), " ")), 9^9, 3))

How to find moving average/max/min of specific period in an array in google sheet

I have a google sheet with following data. I have to find moving average of 14(any period) period. It can be found by copy and pasting average function But I want to find it in an array.
Anyone's help is appreciated.
This is a simpler approach that might work for you:
=ArrayFormula(if(A2:A="","",(sumif(row(A2:A),"<"&row(A2:A)+14,A2:A)-sumif(row(A2:A),"<"&row(A2:A),A2:A))
/countifs(row(A2:A),">="&row(A2:A),row(A2:A),"<"&row(A2:A)+14,A2:A,"<>")))
The running average uses a diminishing range at the end of the data.
avg:
=INDEX(IFNA(VLOOKUP(ROW(A1:A),
QUERY(SPLIT(FLATTEN(ROW(A1:A)&"×"&ARRAY_CONSTRAIN(
SPLIT(FLATTEN(QUERY(TRANSPOSE(IF(ROW(A1:A)<=TRANSPOSE(ROW(A1:A))=TRUE,
TRANSPOSE(A1:A*1)&"×", )),,9^9)), "×"), 9^9, 14 +N("14 day window"))), "×"),
"select Col1,avg(Col2)
where Col2 is not null
group by Col1"), 2, )))
max:
=INDEX(IFNA(VLOOKUP(ROW(A1:A),
QUERY(SPLIT(FLATTEN(ROW(A1:A)&"×"&ARRAY_CONSTRAIN(
SPLIT(FLATTEN(QUERY(TRANSPOSE(IF(ROW(A1:A)<=TRANSPOSE(ROW(A1:A))=TRUE,
TRANSPOSE(A1:A)&"×", )),,9^9)), "×"), 9^9, 14 +N("14 day window"))), "×"),
"select Col1,max(Col2)
where Col2 is not null
group by Col1"), 2, )))
min:
=INDEX(IFNA(VLOOKUP(ROW(A1:A),
QUERY(SPLIT(FLATTEN(ROW(A1:A)&"×"&ARRAY_CONSTRAIN(
SPLIT(FLATTEN(QUERY(TRANSPOSE(IF(ROW(A1:A)<=TRANSPOSE(ROW(A1:A))=TRUE,
TRANSPOSE(A1:A)&"×", )),,9^9)), "×"), 9^9, 14 +N("14 day window"))), "×"),
"select Col1,min(Col2)
where Col2 is not null
group by Col1"), 2, )))

Resources