Trying to create SUMIF statements inside and IF statement - google-sheets

I currently have a SUMIF statement that outputs a graph, based upon the value of a dropdown.
=SUMIF('Rev Tracking'!$C$16:$C$57,$E$21,'Rev Tracking'!I$16:I$57)
This creates a sum of all the values in I$16:I$57 where the value of $E$21 is found in $C$16:$C$57.
I'm looking to select from multiple ranges of cells dependent upon the dropdown result, but I'm not sure you can nest SUMIFS inside an IF statement.. i.e.
=if(E21="Client1",(SUMIF('Rev Tracking'!$C$65:$C$76,$E$21,'Rev Tracking'!I$65:I$76)),(SUMIF('Rev Tracking'!$C$85:$C$100,$E$21,'Rev Tracking'!I$85:I$100)))
This doesn't work and feels like it should be quite different.
Any ideas?

Try nested =QUERY() inside an IF statement.
E.g.
=IF($E$21 = "Client1",
QUERY(<<CELL_RANGE_HERE>>, "SELECT SUM(Col1) WHERE col1 = '''&$E$21&''' ", 1),
IF($E$21 = "Client2",
QUERY(<<CELL_RANGE_HERE>>, "SELECT SUM(Col1) WHERE col1 = '''&$E$21&''' ", 1), ... ))
I've found this to be helpful when trying to sum different ranges.

Related

How do I return the cell address of every instance of a key in a 1D array?

I’m trying to find every instance of a name in a list of names, and compile their locations in a list. I already know TEXTJOIN can do this with comma delineation, but I can’t figure out how to implement it.
I’ve tried using FIND, SEARCH, and MATCH in various ways, but can’t get it to work.
Here’s a mockup, where the filled in section is the goal.
This is the formula I’m trying to use:
=INDEX(IFNA(VLOOKUP(E2:E, QUERY(L:O, "select L,sum(O) where L is not null group by L label sum(O)''"), 2))
Update:
And here’s an actual graph photo.
For reference, Base Stakes and Results work perfectly, the only issue being that they are referring to the ELO column(the one for which the desired result is filled in), which is broken. How should I translate the working formula to function without detecting circular logic?
try:
=QUERY(D:F; "select D,sum(F) where D is not null group by D label sum(F)''")
if you insist on injection use:
=INDEX(IFNA(VLOOKUP(A2:A; QUERY(D:F;
"select D,sum(F) where D is not null group by D label sum(F)''"); 2; )))
update:
use in F2:
=INDEX(IFNA(VLOOKUP(E2:E, QUERY({L:L, O:O},
"select Col1,sum(Col2) where Col1 is not null group by Col1 label sum(Col2)''"), 2, )))
Formula in Table 1 $A$2
=MAP(SORT(UNIQUE(D2:D)),
LAMBDA(n, IF(n<>"",
{n,SUM(INDEX(FILTER(D:F,D:D=n),,3))},"")))

Why does my column not exist in Google Sheets? Query error

I'm trying to run a simple query to condense a list of information from a column (which contains a consistent data type), removing blanks. I have the following code:
=query({Z:Z},"select * WHERE NOT Z =''")
This produces the error: Unable to parse query string for Function QUERY parameter 2: NO_COLUMN: Z.
I've encountered similar issues with query elsewhere - for some reason not yet figured out by my brain, my columns somehow don't exist. I've tried using col26, col1, and everything between col1 and col30, to no avail. While I could workaround this particular simple situation with more of a filter() function, I'd like to better understand why my column's aren't computing. I'd appreciate any clarification on this.
Try this:
=query({Z:Z},"select * WHERE Col1 is not null")
When using curly brackets {} instead of normal ones () you can not use the column letters any more but the number of the column.
Example: When you have =query({D1:F10},"select * WHERE Col3 is not null") it means F is not null (D=Col1, E=Col2, F=Col3)
you can either use:
=QUERY({Z:Z}, "where Col1 !=''")
or:
=QUERY(Z:Z, "where Z !=''")

Marrying Query and Arrayformula through cell reference in Google Sheets

Trying to understand if it is possible to apply ARRAYFORMULA to situations when QUERY is used in Google Sheets.
For example, I used QUERY for querying and aggregating a set of items, like so:
=QUERY($H$2:$I$17,"select sum(I) where H='"&A2&"' label sum(I) ''",0)
But in order to make that work across the spreadsheet, I will have to drag this formula down. There is also the ARRAYFORMULA thing, which is supposed to help with getting rid of excessive dragging, however it does not seem to work with QUERY, or am I just missing something?
A picture for a quick look:
And a shared file for the longer thinking:
https://docs.google.com/spreadsheets/d/1xOdqeESrFbrBknNYahSeF0ripA5fr2vVFQ-r--lkdA0/edit?usp=sharing
use this formula:
=QUERY(H2:I17, "select H,sum(I) where H is not null group by H label sum(I)''", 0)
and then you can do simple VLOOKUP like:
=ARRAYFORMULA(IFNA(VLOOKUP(A2:A, QUERY(H2:I17,
"select H,sum(I) where H is not null group by H label sum(I)''", 0), 2, 0)))
Here two method alternatively:
first ==>
=arrayformula(sumif(H2:H,"=" & unique(filter(H2:H,H2:H<>"")),I2:I))
second ==>
=arrayformula(
query(
filter({vlookup(H2:H,{A2:A,row(A2:A)},2,false),H2:I},H2:H<>"")
,"Select sum(Col3) group by Col1 label Sum(Col3) ''"
)
)

ARRAYFORMULA repeating results of VLOOKUP

I've been forced to change a formula in my spreadsheet and can't seem to get the ARRAYFORMULA to fill down the column without repeating the same result.
Here is the code that works in a single row:
=ArrayFormula(SPLIT(CONCATENATE(QUERY(coreAbilities!$A$2:$E,"SELECT B,C,D,E where A = '"&VLOOKUP(C2:C,unitData!$C$2:$D,2,)&"'",0)&":"),":"))
​I've tried wrapping it in a VLOOKUP, but nothing seems to get the job done.
Basically, I'm searching a table of 5 columns and finding a match to a name, however, the name is not the same between these two tables. The name I'm searching has to first be matched to it's "code name" in another table and then I can search the final table. On top of that, each name has 3-5 matches, each with 5 values. So simply using a VLOOKUP doesn't seem to do the trick.
I've tried VLOOKUP and FILTER in hopes of making this easier on myself, but the only formula I can manage to get the results I need (at least in the proper order) is by using QUERY.
I'm not married to the method I'm using, but it's gotten me this far, so I've just been trying to force it through.
Here's my sheet
Heros!H2
QUERY does not support arrays/ranges inside itself under string parameters.
the only possible way is to use regex capabilities within QUERY like:
however TEXTJOIN belongs to a group of formulae that are limited to 50000 character input, eg. this won't work with your massive arrays. therefore double VLOOKUP is your friend. but as you noticed VLOOKUP can output always only the first found result and every next finds will be just a clones/repeats of the first match.
so the trick is to append a counter to the vlooked up value which will force new finds instead of cloning previous finds:
=ARRAYFORMULA(IFERROR(VLOOKUP(COUNTIFS(IFERROR(VLOOKUP(C2:C, unitData!$C$2:$D, 2, 0)),
IFERROR(VLOOKUP(C2:C, unitData!$C$2:$D, 2, 0)), ROW(M2:M), "<="&ROW(M2:M))&
IFERROR(VLOOKUP(C2:C, unitData!$C$2:$D, 2, 0)),
{COUNTIFS(coreAbilities!A2:A, coreAbilities!A2:A, ROW(coreAbilities!A2:A), "<="&
ROW(coreAbilities!A2:A))&coreAbilities!A2:A, coreAbilities!B2:E}, {2,3,4,5}, 0)))
UPDATE:
=ARRAYFORMULA(TRANSPOSE(QUERY(TRANSPOSE(IFERROR(SPLIT(IFERROR(VLOOKUP(
IFERROR(VLOOKUP(C2:C, unitData!$C$2:$D, 2, 0)),
SPLIT(TRANSPOSE(QUERY(TRANSPOSE(QUERY(QUERY({coreAbilities!A2:A&"♦",
"♠"&coreAbilities!B2:B&"♠"&coreAbilities!C2:C&"♠"&coreAbilities!D2:D&"♠"&coreAbilities!E2:E,
coreAbilities!C2:C}, "select Col1,max(Col2) group by Col1 pivot Col3", 0),
"offset 1", 0)),,999^99)), "♦"), 2, 0)), "♠"))), "offset 1", 0)))

Google SpreadSheet Query: Can I remove column header?

I'm doing this query at my google spreadsheet:
=QUERY(H4:L35;"select sum(L) where H='First Week'"; -1)
But it returns a little table with "sum" as header and result below it. What I want is just the result! How I remove header? Can I?
Try this:
=QUERY(H4:L35,"select sum(L) where H='First Week' label sum(L) ''")
=QUERY(QUERY(A1:D, "SELECT *", 1), "SELECT * OFFSET 1", 0)
The outer query: "SELECT * OFFSET 1" excludes the first row (the header).
The inner query explicitly specifies one row of headers (via the third argument supplied to QUERY), whilst the outer query specifies none.
=INDEX(QUERY(H4:L35;"select sum(L) where H='First Week'"; -1),2,1)
This just parses the returned array and selects the 2nd record returned in the first column.
You can also do this with the filter function which is less compute intensive.
=SUM(FILTER(L4:L35, H4:H35 = "First Week"))
I have a QUERY which is returning the top 3. I could not get this to work when returning multiple rows. I ended up just hiding the row with the formula and only the answers show now.
For queries using pivot, try using INDEX to remove headers from the pivoted columns.
=INDEX(QUERY('Class hours'!A2:C11,
"select sum(C)
where A = '"&A5&"'
group by A
pivot B"), 2)
Got the answer from this thread:
https://stackoverflow.com/a/63468791/5424088
... or this
=QUERY(QUERY(H4:L35;"select sum(L) where H='First Week'"),"OFFSET 1",0)
This is more concise when ALL label classes are not wanted.
Note that 'select' and 'where' classes are not required in the second QUERY statement.
Instead of labeling column names as blanks using '', you can omit all headers like this:
=QUERY(H4:L35,"select sum(L) where H='First Week'", 0)
See the format here.
Example:
=QUERY(B4:C38,
"SELECT C, sum(B) where C!='' group by C label C 'Member', sum(B) 'Sum'"
)

Resources