Translating each result row of a Google Sheets QUERY operation via VLOOKUP - google-sheets

I have the multi-column results of a QUERY in Google Sheets, where I want to translate the strings in one column based on a lookup table in another named range. I can accomplish this indirectly with an additional VLOOKUP call, but I'd like to do it all in one go, with no intermediary steps. See this sheet for an example of my format.
The initial query I make looks something like this:
=QUERY(votes, "SELECT B, SUM(C) GROUP BY B LABEL B 'Option', SUM(C) 'Votes'")
I can then translate each row with something like this in a new column...
=VLOOKUP(A2, options, 2, 0)
... and then just pick out the columns I need:
={C2:C4,B2:B4}
How can I combine all this? I think I need to do this with an ARRAYFORMULA, but this doesn't seem to be it:
=ARRAYFORMULA(VLOOKUP(options, QUERY(votes, "SELECT B, SUM(C) GROUP BY B LABEL B 'Option', SUM(C) 'Votes'"), 2, FALSE))

=ARRAYFORMULA({VLOOKUP(INDEX(
QUERY(votes, "select B,sum(C) group by B LABEL sum(C)''"),,1), options, 2, 0),
QUERY(votes, "select sum(C) group by B LABEL sum(C)''")})

=ARRAYFORMULA(QUERY({IFERROR(VLOOKUP(Votes!B2:B, options, 2, 0)), Votes!C2:C},
"select Col1,sum(Col2) where Col1 is not null group by Col1 label sum(Col2)''", 0))

Related

Returning top value of column from range in google sheets

Say I had a list like this.
Group 1 Group 2
Edward, C Kate, A
Mark, F Ava, Z
Now, in a different column =IF(COUNTIF('Gender'!$A$2:$B$3, "Kate, A"), "", "") is my current formula. I would like to make the first set of empty speech marks return Group 1 or Group 2 depending what column it is. I still require the COUNTIF because my actual sheet has values which will not be contained in either column.
Thanks.
try:
=INDEX(IFNA(VLOOKUP("Kate, A", SPLIT(FLATTEN(
IF(Gender!A2:B="",,Gender!A2:B&"×"&Gender!A1:B1)), "×"), 2, 0)))
update:
=INDEX(IFNA(VLOOKUP(A1:A, QUERY(SPLIT(FLATTEN(
IF(Groups!B2:C="",,Groups!B2:C&"×"&Groups!A1&"×"&Groups!B1:C1),
IF(Groups!F2:G="",,Groups!F2:G&"×"&Groups!E1&"×"&Groups!F1:G1)), "×"),
"select Col1,max(Col3) where Col2 is not null
group by Col1 pivot Col2 label Col1'"&A1&"'"), {2, 3}, 0)))
demo sheet

is there a way to use the char(10) function without separating every value?

I am currently using char (10) to merge columns, from a separate tab called "scripts", into one cell based on a name.
=IFERROR(TEXTJOIN(CHAR(10),1,QUERY(Scripts!$A$3:A,"select B, C, D, E where A ='"&A1&"'")))
my information shows up in a list. is there a way to combine columns B+C and then D+E.
row 1 is what my data looks like on "scripts" tab.
row 7 is what the data looks like with the above equation.
row 12 is what i would like the equation to do.
try:
=ARRAYFORMULA(IFNA(VLOOKUP(A1:A, QUERY(
{Scripts!A:A, Scripts!B:B&" "&Scripts!C:C&CHAR(10)&Scripts!D:D&" "&Scripts!E:E},
"where Col1 is not null"), 2, 0)))
update:
=ARRAYFORMULA(REGEXREPLACE(TRIM(SPLIT(FLATTEN(QUERY(QUERY(
{Data!A2:A&"×", CHAR(10)&TEXT(Data!B2:B, "m/d/yy")&
CHAR(10)&Data!C2:C&", "&Data!D2:D&", "&Data!E2:E, ROW(Data!A2:A)},
"select max(Col2) where not Col1 starts with '×' group by Col3 pivot Col1")
,,9^9)), "×")), "^"&CHAR(10), ))

Google Query SELECT statement concatenated with a NESTED IF result

Is it possible to return a Nested IF result from a CELL that will be concatenated to the SELECT statement in the QUERY function?
For example, I am trying to return the result for the following Nested IF function into the Query Function:
https://docs.google.com/spreadsheets/d/15i1E8AZHORRmPlu1VQqFRN1_7-aUyAz-hlYMOUtIlY4/edit?usp=sharing
Appreciate it, if anyone could take a look.
Regards
JVA
its done like this:
=QUERY(TESTDATA!A1:D16, "SELECT A, D, SUM(C) WHERE 1=1 "&
IF(AND(M3="NAME",N3="Customer"), " GROUP BY A, D PIVOT B",
IF(AND(N3 = "Customer"," AND A = '"&M3&"' GROUP BY A, D PIVOT B"),
" AND A = '"&M3&"' GROUP BY A, D PIVOT B",
" AND A = '"&M3&"'
AND D = '"&N3&"' GROUP BY A, D PIVOT B")), 1)
Sometimes, it's easier to FILTER the results before applying QUERY:
=ArrayFormula(QUERY(FILTER(A1:D16, A1:A16=M3, D1:D16=N3), "SELECT Col1, Col4, SUM(Col3) GROUP BY Col1, Col4 PIVOT Col2 LABEL Col1 'Name', Col4 'Customer'",0))
As you can see, this requires using Colx notation instead of letters to indicate columns in the SELECT clause; but this is actually (in my opinion) more versatile, since you don't have to rewrite the QUERY if you ever insert columns before the existing source data.
You'll also notice that I needed to LABEL the first two columns, since FILTER will have FILTERed out the headers. (In fact, for this reason, the ranges in the formula could just as easily have begun with row 2, e.g., A2:A16, etc.)
Finally, at least in your sample spreadsheet, you didn't need the sheet name to reference the source ranges, since the result is in the same sheet.

How do I return a result based on criteria versus totals given?

I have a sheet that looks similar to this:
So column A and column B are combined along with a number in column C. What I am trying to do is add up each value in each column (for example: add each C column for each time "Cat" appears, and "Dog" and "Grass", etc) and then find the value in columns A and B that is the highest, and return that value.
So for example, in my example above, Dog would be the formula result because it's C column totals to 28. Is there a formula (or, most likely, a combination of formulas) that can accomplish this?
Also, is it possible to do the inverse: so it would show the least combined value?
for max:
=INDEX(QUERY({A:A, C:C; B:B, C:C},
"select Col1,sum(Col2)
where Col1 is not null
group by Col1
order by sum(Col2) desc
label sum(Col2)''", 0), 1, 1)
for min:
=INDEX(QUERY({A:A, C:C; B:B, C:C},
"select Col1,sum(Col2)
where Col1 is not null
group by Col1
order by sum(Col2) asc
label sum(Col2)''", 0), 1, 1)

Apply array formula to sumifs

I have Google sheet with sum-ifs functions, but I need to make it apply to the whole columns instead of just one cell and to apply automatically to new rows when there added
I know that arrays don't work with sumifs after doing research but I can't seem to figure out to apply an array function to this.
=ArrayFormula(SUMIFS(K:K,C:C,C2,L:L,false))
so I tried instead to make it a =sum(if(and function instead with an array, but couldn't get that to work either. not sure how to get it to apply to the same affect as the above formula
I need to apply the following sum-ifs all the conditions are met to each cell in the selected column
basically like this:
=QUERY({C2:C, K2:L},
"select Col1,sum(Col2)
where Col1 is not null
and Col3 = FALSE
group by Col1
label sum(Col2)''", 0)
if you want to pair it with list of names then use VLOOKUP:
=ARRAYFORMULA(IFERROR(VLOOKUP(C2:C, QUERY({C2:C, K2:L},
"select Col1,sum(Col2)
where Col1 is not null
and Col3 = FALSE
group by Col1
label sum(Col2)''", 0), 2, 0)))

Resources