Since Query is such a powerful formula, i try to avoid my team members to avoid messing up the pivot. I try to to come out with query to replace my pivot. Based on the raw data Google Sheet Trix, i try to sum column based on the criteria i have described in Grey colour (B2:E2). Appreciate if someone could lay some foundation for me, so i can further tweaks based on the formula you create here.
I created a new tab and entered the following in cell A1:
=query(
{'RAW DATA'!B:B,'RAW DATA'!E:E,'RAW DATA'!G:J},
"select Col1, sum(Col3), sum(Col4), sum(Col5), sum(Col6)
where Col1 is not null and Col2<>'Black and Yellow'
group by Col1"
)
Use a query() with a group by clause, like this:
=query(
'RAW DATA'!B1:J,
"select B, sum(G), sum(H), sum(I), sum(J)
where B is not null and E <> 'Black' and E <> 'Yellow'
group by B",
1
)
The result table will only include weeks where there is at least one data row where E is not Black or Yellow.
See the new Solution sheet in your sample spreadsheet.
Here is your solution. which also included in new tab named Solution Pivot in Query.
=ArrayFormula(QUERY(SPLIT(FLATTEN(TO_TEXT('RAW DATA'!B2:B)&"|"&'RAW DATA'!E2:E&"|"&'RAW DATA'!G1:J1&"|"&'RAW DATA'!G2:J),"|"),"Select Col1, Sum(Col4) where Col2 is not null And Col2 <> 'Yellow' And Col2 <> 'Black' Group by Col1 Pivot Col3 label Col1 'Date'"))
Related
I am trying to calculate a count of how many times a course is listed in different columns and return the count in a list of all the courses found in those columns.
I have a spreadsheet that shows the start of my formula...
query(UNIQUE({C2:C21; F2:F21;I2:I21}),"select C,F,I, count(C), count(F), count(I) where B is not null group by C,F,I")
Clear cells A23, A24 and B23 and then try in A23
=query({C2:C21; F2:F21;I2:I21},"select Col1, count(Col1) where Col1 <> '' group by Col1 label Col1 'Fall Courses', Count(Col1) 'Count'")
and see if that helps?
If it does, you can repeat the same logic for 'winter' and 'spring'.
Alternatively, you can also try this single formula
=query(ArrayFormula(split(transpose(split(textjoin("~", true, regexreplace(C1:K1, "(Winter|Spring|Fall)", "_$1")&"_"&C2:K21), "~")), "_")), "Select Col3, Count(Col3) where Col3 <>'' group by Col3 pivot Col2", 0)
I'm looking for a formula, =query possibly, that will turn this:
Into this:
I used the following formula successfully to get the AFTER picture, but I can't get the counts. I tried using concatenate but there isn't a way to do that to each item in the array. Maybe there is a way to do this using a query?
=transpose(split(join(",",unique(C1:C98)),","))
And here's my flavour of it, which also needs to be dragged to the right, to fill the other columns:
=ARRAYFORMULA(QUERY(
{UNIQUE(A$2:A) & " - " & COUNTIF(A$2:A,UNIQUE(A$2:A))},
"where Col1<>' - 0'",0))
Yes, you can use query to get the unique values and counts then join the two columns
=ArrayFormula(index(query(A2:A,"select A,count(A) where A is not null group by A label count(A) ''"),0,1)&"-"&index(query(A2:A,"select A,count(A) where A is not null group by A label count(A) ''"),0,2))
Apologies, in order to drag it across you would have to put:
=ArrayFormula(index(query({A2:A},"select Col1,count(Col1) where Col1 is not null group by Col1 label count(Col1) ''"),0,1)
&"-"&index(query({A2:A},"select Col1,count(Col1) where Col1 is not null group by Col1 label count(Col1) ''"),0,2))
Try the following in cell A2 and drag to the right.
=ARRAY_CONSTRAIN(ArrayFormula(IFERROR(CONCAT(UNIQUE(A2:A99)&" - ",
ArrayFormula(COUNTIFS(A2:A99,UNIQUE(A2:A99)))))),COUNTUNIQUE(A2:A99),1)
Also using a query
=ArrayFormula(index(query({A2:A},"select Col1,count(Col1) where Col1 is not null group by Col1 label count(Col1) ''"),0,1)&"-"&
index(query({A2:A},"select Col1,count(Col1) where Col1 is not null group by Col1 label count(Col1) ''"),0,2))
The only difference is that using the first formula you maintain the names within the order they appear, while using the query they get sorted alphabetically.
Functions used:
ARRAY_CONSTRAIN
ArrayFormula
IFERROR
CONCAT
UNIQUE
COUNTIFS
COUNTUNIQUE
I have a data set with multiple columns and roughly 1000 rows. I need to find out how many times certain combinations of columns can be found within the data set.
In my example below, columns A:B represents the raw data set. In C2 I have a formula that finds all non-unique combinations from columns A:B. What I need is a formula that counts how many times combinations in columns C:D are found within columns A:B. The desired output should be in ColE.
you can do it all in one go... delete columns C, D, E and use this formula:
=ARRAYFORMULA(QUERY({A2:B, A2:A&B2:B},
"select Col1,Col2,count(Col3)
where Col1 is not null
group by Col1,Col2
order by count(Col3) desc
label count(Col3)''"))
for a selected combination only use this formula in E2 cell:
=ARRAYFORMULA(IFERROR(VLOOKUP(C2:C&D2:D, QUERY({A2:A&B2:B},
"select Col1,count(Col1)
where Col1 is not null
group by Col1
label count(Col1)''"), 2, 0)))
It's always better to share a copy of your spreadsheet, but try entering in E1
={"Count"; ArrayFormula(IF(LEN(C2:C), VLOOKUP(C2:C&D2:D, query({A2:A&B2:B, A2:B}, "Select Col1, count(Col3) where Col1 <>'' group by Col1"), 2, 0),))}
and see if that works?
Note that you can create the same output (columns C, D and E) with a single formula
=query(ArrayFormula(query({A2:B, A2:A&B2:B}, "Select Col1, Col2, count(Col3) where Col1 <>'' group by Col1, Col2")), "where Col3 >1 label Col1 'Value 1', Col2 'Value 2'")
Here is a problem to solve with a Google Sheets formula.
I have a big table (sheet “data” with headers on the first row) with those columns:
A, product reference
B: customer
C to F, KPI1 to KPI4
On another sheet, a grid of product references (A2:A) by customer (B1:1).
Now I need to fill each cell from the grid with the concatenation of KPIs (data!C24&"|"&data!D24&"|"&data!E24&"|"&data!F24)
Could you workout a single formula to fill all the cells?
Here is a sample spreadsheet with the data and grid sheet:
https://docs.google.com/spreadsheets/d/1iA_kw4kKw99Qk69X4tST9U-QN2SeG2EN3KEeyG6AtHs/edit?usp=sharing
I have worked out a formula which does the job, though with very poor performance on large dataset:
=ARRAYFORMULA(
IFNA(
VLOOKUP(
$B3:$B&"|"&C$2:$2,
ARRAYFORMULA(
{data!A2:A&"|"&data!B2:B,data!C2:C&"|"&data!D2:D&"|"&data!E2:E&"|"&data!F2:F}
),2,0
),""
)
)
Solution
Use an ArrayFormula on a Query with Pivot:
=ARRAYFORMULA(
QUERY(
{data!A2:A, data!B2:B, data!C2:C&"|"&data!D2:D&"|"&data!E2:E&"|"&data!F2:F},
"select Col1,max(Col3) where Col1 is not null group by Col1 pivot Col2",0
)
)
a shorter version of previous answer (no need for pre-sorting coz pivot will sort it on its own):
=ARRAYFORMULA(QUERY(
{data!A2:A, data!B2:B, data!C2:C&"|"&data!D2:D&"|"&data!E2:E&"|"&data!F2:F},
"select Col1,max(Col3) where Col1 is not null group by Col1 pivot Col2", 0))
Try this on the first cell of your grid:
=ArrayFormula(query(sort({data!A:A,data!B:B,transpose(substitute(query(transpose(data!C:F),,4)," ","|"))},1,true,2,true),"select Col1, max(Col3) where Col1 is not null group by Col1 pivot Col2",0))
In cell C1:C of my table I got 6 rows with ticket id's. I like to search different spreadsheets to search for those ticket id's and calculate the total hours spent on that ticket.
I have it working using the following formula:
=QUERY({IMPORTRANGE("SPREADSHEETID";"B3:B")\ARRAYFORMULA(TO_PURE_NUMBER(IMPORTRANGE("SPREADSHEETID";"F3:F")-IMPORTRANGE("SPREADSHEETID";"E3:E")))};"SELECT SUM(Col2) WHERE Col1 = '"&C1&"' GROUP BY Col1 LABEL SUM(Col2) ''")
In this example, C1 is where the ticket ID can be found.
Now I thought I could just wrap QUERY in a ARRAYFORMULA and use C1:C instead of just C1 but that won't work. Now I could just copy and paste the above formula in every cell but there must be a cleaner way.
ANSWER
I used the following formula to make it work, thanks to Max's answer below.
=QUERY(
{
IMPORTRANGE("SPREADSHEETID";"B3:B")\
ARRAYFORMULA(
TO_PURE_NUMBER(
IMPORTRANGE("SPREADSHEETID";"F3:F") - IMPORTRANGE("SPREADSHEETID";"E3:E")
)
)
};
"
SELECT Col1, SUM(Col2)
WHERE Col1 = '" & JOIN("' OR Col1 = '";FILTER(C:C; C:C <> "")) & "'
GROUP BY Col1
LABEL SUM(Col2) ''
")
Sample formula is:
=QUERY({A:B},"select * where Col1 = '"&JOIN("' or Col1 = '",FILTER(D2:D,D2:D<>""))&"'")
No, one cannot create an array of query strings and use arrayformula(query(...)) to run them all at once.
Alternative: instead of
SELECT SUM(Col2) WHERE Col1 = '"&C1&"' GROUP BY Col1 LABEL SUM(Col2) ''
use the query
SELECT Col1, SUM(Col2) GROUP BY Col1
elsewhere on the sheet, and then use vlookup to look up the sum for each value of Col1 that you want. vlookup can be used inside of arrayformula like this:
=arrayformula(vlookup(C1:C10, E:F, 2, 0))
looks up each of values in C1..C10 in the column E (exact match required) and returns the corresponding value in column F (2nd column of the searched range).