Spreadsheet
Tab Ladder - Col J has a COUNTIFS formula that calculates % of matching criteria within the last 24hrs. I would like to limit the calculation to the last 10 matching entries within the 24hr window.
I have a stub formula in J35 outlining how I would like it structured. I have done a lot of searching, and while I have found a few examples of "Last x rows" - I haven't been able to translate those concepts to my specific needs.
Thank you in advance for your help!
instead of your:
=COUNTIFS($A$18:$A, ">"&Dates!$C$4+TIME(Dates!$C$3,0,0)-1,
$D$18:$D, $D35,
$H$18:$H, $H35,
$Y$18:$Y, "")
use:
=COUNTA(IFERROR(QUERY(FILTER(ROW($A$18:$A),
$A$18:$A>Dates!$C$4+TIME(Dates!$C$3,0,0)-1,
$D$18:$D=$D35,
$H$18:$H=$H35,
$Y$18:$Y=""), "limit 10")))
I have also discovered that the following nested QUERY would work - though I would have to restructure the sheet a little bit so that the formula was outside of the bounds of the QUERY so not to trigger a circular dependency.
=QUERY(QUERY($A$18:$Y, "Select T Where H = '"&$H35&"' and D = '"&$D35&"' and E = '"&$E35&"' Order by C desc limit 10", 0), "select avg(Col1) label avg(Col1)''", 0)
Related
I would like to modify the following QUERY, or some formula variation that accomplishes the same result, to only be entered a single time at the top of the column and fill the cells below using ARRAYFORMULA
=IFERROR(QUERY(Items!$A$2:$T,"SELECT E,F,G,H,I,J,K,L,M,N,O,P,Q,R where A = '"&Estimate!A2&"' and B = '"&Estimate!B2&"' and C = '"&Estimate!C2&"' "&if(Estimate!D2<>"", "and D = '"&Estimate!D2&"'",)&" AND A is not null LIMIT 1", 0),"")
My formula is in Estimate!G2 of my spreadsheet.
this answer is pretty much correct, tho the exact formula would be:
=ARRAYFORMULA(IF(LEN(A2:A), VLOOKUP(A2:A&B2:B&C2:C&IF(LEN(D2:D), D2:D, "*"),
{Items!A2:A&Items!B2:B&Items!C2:C&Items!D2:D,Items!A2:T},
{6,7,8,9,10,11,12,13,14,15,16,17,18,19}, 0), ))
I think you'd have to use Vlookup, which isn't quite as convenient as Query. This is what I have so far, but it might need a bit more work to get exactly the same results as query in all cases (owing to the ORDER statement within the query):
=ArrayFormula(if(A2:A="","",vlookup(A2:A&B2:B&C2:C&if(D2:D="","*",D2:D),{Items!A2:A&Items!B2:B&Items!C2:C&Items!D2:D,Items!A2:T},{2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21},false)))
I'm trying to refine my coding for a working spreadsheet by updating all the code to QUERY functions. One thing I am stuck on is where I average the last x rows matching specific conditions.
I was hoping that the following formula would work:
=QUERY(Ladder!A2:AE,"Select Avg(J) where F = '"&H$1&"' order by B desc limit "&$C$5)
My thought being that this would return the average of the last 3 rows - but I receive the
COL_IN_ORDER_MUST_BE_IN_SELECT error.
I only want the formula to return a single cell with the % average in it.
I realise that I could use the following code and then QUERY the Avg of that, but I want to be able to return % values for each different deck.
Here's a link to a cut down version of the spreadsheet:
Spreadsheet
Ladder: Where I enter the data
Ladder Filter: Where the data will filter based on query commands depending on criteria in the Ladder Stats tab
Ladder Stats: Where I can view all statistics and generate various statistical reports
It's all working at the moment, but as you can see it's very clunky and formula-heavy. I've worked most of it out except for the scenario you will find on the "Ladder Stats" tab - whereby B3="" and C5 != NULL. It is designed to give statistics for the last x games played by each of the decks.
you can either use a double query like this and format cell as %
=QUERY(QUERY(Ladder!A2:AE,
"select J
where F = '"&H$1&"'
order by B desc
limit "&$C$5, 0),
"select avg(Col1)
label avg(Col1)''", 0)
or you can use AVERAGE formula and click twice to get decimal position:
=AVERAGE(QUERY(Ladder!A2:AE,
"select J
where F = '"&H$1&"'
order by B desc
limit "&$C$5, 0))
In Google Spreadsheet, I have the expression:
=QUERY(database; "select b,c, where a='anyone-condition' order by c desc";-1)
The column a,b are strings and c is a number.
How can I include in this query one calculated field, c/sum(c)?
Although Ed Nelson is perfectly right, you can be creative with multiple nested Queries and get desire result without an extra column.
I have build simple example that will show you the way
Data
Solution 1 - simpler but with ugly column name
=QUERY(
A1:C3,"select A,B,C/"&QUERY(
QUERY(A1:C3,"select sum(C)"),"select * offset 1",0)&"" ,1
)
Solution 1.1 - more complex but with custom column name
=QUERY(
A1:C3,"select A,B,C/"&QUERY(
QUERY(A1:C3,"select sum(C)"),"select * offset 1",0)&
" label C/"&QUERY(
QUERY(A1:C3,"select sum(C)"),"select * offset 1",0)&
" 'C/sum'",1
)
Finals:
You can go further and add formating to last column as well
Link to working copy
For your specific example probably code should be like this:
=query(database; "select b,"&query(query(database; "select sum(c), where a='anyone-condition' "),"select * offset 1",0)&" where a='anyone-condition' order by c desc";-1)
But, it should be tested against real data
Is that serves your needs?
You can't do a sum without doing a group which you don't want. You can add a column D to your sheet to sum. Try =sum($C$2:$C) in D and copy down. Then this will work:
=query(database, "select B,C/D where A='anyone-condition' order by C desc label C/D ''",-1)
The problem is with the Google Spreadsheet 'kernel', not with my coding. Look the scream that I printed
https://drive.google.com/file/d/1yJbcfVZ1tDmW8WoG224PALRIwyEbX9LH/view?usp=sharing
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
How do you arrayformula() a sum() such as:
=sum(A1:H1)
I need to go down 1000 rows.
Another option:
=ArrayFormula(SUMIF(IF(COLUMN(A1:H1),ROW(A1:A1000)),ROW(A1:A1000),A1:H1000))
Of the two answers that work, Jacob Jan Tuinstra and AdamL, Jacob gives a better answer. Jacob's runs faster and is easier to remember.
However, why use crazy formulas when it is much easier to use Google Sheets to the fullest?
=ARRAYFORMULA(A2:A+B2:B+C2:C+D2:D+E2:E+F2:F+G2:G+H2:H)
In the foregoing formula, you can use named ranges instead of the ranges by reference.
=ARRAYFORMULA(range1+range2+range3+range4+range5+range6+range7+range8)
As well, you can sum across rows that span sheets rather than being stuck working with columns within the same sheet.
To fix the formula to block returning zeros for blank rows, use this:
=arrayFormula(if(isNumber(A2:A),A2:A+B2:B+C2:C+D2:D+E2:E+F2:F,G2:G,H2:H))
See: See Ahab's answer on Google Forums
For a cool sum accumulation formula, see Otávio Alves Ribeiro's answer on Google Forums
This is what you are looking for:
=MMULT(A1:H1000,TRANSPOSE(ARRAYFORMULA(COLUMN(A1:H1000)^0)))
See this answer on Web Application I gave: https://webapps.stackexchange.com/a/53419/29140
Note: tried it on the new Google Spreadsheet, without succes.
with new functions in google sheets (since 20 Sep, 2022) all you need is:
=BYROW(A:H; LAMBDA(x; SUM(x)))
Summing A-H horizontal and running down for 523 lines:
=ARRAYFORMULA(iferror(mmult(A1:H523;TRANSPOSE(column(A1:H1))^0)))
if I look at this formula I really think the following might be simpler. Add this to Tools > Script Editor:
function row_sum(range_to_sum_per_row) {
var result_column = [];
for (var r = 0; r < range_to_sum_per_row.length; r++) {
var row_sum = parseFloat(0);
for (var c = 0; c < range_to_sum_per_row[r].length; c++) {
row_sum += range_to_sum_per_row[r][c] || 0;
}
result_column.push([row_sum]);
}
return result_column;
}
use this like so for performance reasons, where C:H is the range you want to sum up and A:A is a column that does not contain an empty string:
=row_sum(filter(C2:H, len(A2:A)>0))
If you want to be able to add rows and sum to the last row for all values in A1:H, you can use:
=ArrayFormula(SUMIF(IF(COLUMN(A1:H1),ROW(A1:A)),ROW(A1:A),A1:H))
Alternatively, if you want be be able to add rows and columns to the spreadsheet and sum to the last of both this can also be done. Paste the following code into any cell and it will create a column of summed values for all cells in each row below and to the right of pasted cell:
=arrayformula(SUMIF(IF(COLUMN(indirect(concatenate(REGEXREPLACE(address(row(),column()+1),"[^[:alpha:]]", ""),VALUE(REGEXREPLACE(address(row(),column()),"[^[:digit:]]", "")),":",VALUE(REGEXREPLACE(address(row(),column()),"[^[:digit:]]", ""))))),ROW(indirect(concatenate(REGEXREPLACE(address(row(),column()+1),"[^[:alpha:]]", ""),VALUE(REGEXREPLACE(address(row(),column()),"[^[:digit:]]", "")),":",REGEXREPLACE(address(row(),column()+1),"[^[:alpha:]]", ""))))),ROW(indirect(concatenate(REGEXREPLACE(address(row(),column()+1),"[^[:alpha:]]", ""),VALUE(REGEXREPLACE(address(row(),column()),"[^[:digit:]]", "")),":",REGEXREPLACE(address(row(),column()+1),"[^[:alpha:]]", "")))),indirect(concatenate(concatenate(REGEXREPLACE(address(row(),column()+1),"[^[:alpha:]]", ""),VALUE(REGEXREPLACE(address(row(),column()),"[^[:digit:]]", "")),":"),address(rows($A:$A),columns($1:$1))))))
Using Query
=INDEX(TRANSPOSE(
QUERY(TRANSPOSE(FILTER(A2:H,A2:A<>"")),
"select sum(Col"&JOIN("), sum(Col",SEQUENCE(COUNTA(A2:A)))&")",0)
),,2)
notes:
generating query string on the fly
Using DSUM:
=ARRAYFORMULA(DSUM(
TRANSPOSE(FILTER({A2:A,A2:H},A2:A<>"")),
SEQUENCE(COUNTA(A2:A)),{IFERROR(1/0);IFERROR(1/0)}))
notes:
{IFERROR(1/0);IFERROR(1/0)} is to make zero creteria for DSUM.
{A2:A,A2:H} -- added fake column for DSUM to mimic header column.
may be able to cahnge the formula into DMAX or DAVERAGE
Answer similar to #adamL suggestion, but removing the internal if.
=ArrayFormula(
sumif(ROW(A1:A10)*COLUMN(A1:H1)^0,ROW(A1:A10),A1:A10)
)
In this case I use ROW(A1:A10) * COLUMN(A1:H1)^0 to generate the row number matriz. To understand how it works, you can test just this part in your Google sheets:
= ArrayFormula(ROW(A1:A10) * COLUMN(A1:H1)^0)
So, with your row matrix, sumif can operate for each line, is the line has the matched row number, it will be summed up.
Let us not complicate this.
Simply put parenthesis in each of the array in the range.
=arrayformula( Sum( (A1:A):(H1:H) )
This spans not only upto 1000 rows but upto infiinity.
If you really want to limit then go
=arrayformula( Sum( (A1:A1000):(H1:H1000) )