Count occurrence by weekday in columns and row - google-sheets

I am having a google spreadsheet where the columns are specific dates and the rows are products.
| Weekdays | | | 2 | 3 | 4 | 5 | 6 |
| Activity | Streak | Max | 06.11.18 | 07.11.18 | 08.11.18 | 09.11.18 | 10.11.18 |
|-----------|--------|-----|----------|----------|----------|----------|----------|
| Product 1 | 0 | 5 | x | x | x | x | ... |
| Product 2 | 0 | 6 | x | | x | x | ... |
| Product 3 | 0 | 11 | x | x | x | x | ... |
| Product 4 | 0 | 7 | | | | | ... |
See my Data sheet.
On the tab Evaluation I tried to generate the following matrix, which should answer the question on which day of the week an occurrence of each of the products happened.
| Weekdays vs. Products | Product 1 | Product 2 | Product 3 | Product 4 |
|-----------------------|-----------|-----------|-----------|-----------|
| 1 | #N/A | #N/A | #N/A | #N/A |
| 2 | #N/A | #N/A | #N/A | #N/A |
| 3 | #N/A | #N/A | #N/A | #N/A |
| 4 | #N/A | #N/A | #N/A | #N/A |
| 5 | #N/A | #N/A | #N/A | #N/A |
| 6 | #N/A | #N/A | #N/A | #N/A |
| 7 | #N/A | #N/A | #N/A | #N/A |
I tried simply a =COUNTIF(Data!$2:$2;$B3;C$2;Data!A3), however as you can see above I get #N/A.
Any suggestions how to count for each weekday the occurrence of the product?
Appreciate your reply!
PS.: Find my example google spreadsheet here: google spreadsheet

Try
=countif(filter(filter(Data!$D$3:$BA;Data!$A$3:$A=C$13); Data!$D$1:$BA$1=$B14); "x")
and fill down and to the right as needed.
In Tabellenblatt3, I also added an alternitve approach, using a single formula. In cell B3 you'll find
=ArrayFormula(substitute(query(split(transpose(split(textjoin(;1;query(if(Data!D3:BA10="x";substitute(Data!A3:A10; " ";"/")&"_"&Data!D1:BA1&"_"&Data!D3:BA10;);;rows(Data!A3:A10)));" ")); "_"); "Select Col2, count(Col3) group by Col2 pivot Col1 label Col2 'Weekday'"); "/"; " "))
Note that this formula can break (because of the character limit in textjoin()) for large data sets.

Related

How to use FILTER to return rows if its value is different than the above row in Google Sheets?

I'm trying to make a "friendly" view of a more complicated sheet by filtering out unnecessary data. I want one of the filter conditions to be to return a value only if that value is different than the previous row's value.
I've tried doing a filter, such as =filter(B:B, B2 <> B1) which obviously doesn't resolve. How do I do an offset reference so that the results are similar to as follows:
Sample Data
| Row | Category | Subcategory |
| --- | -------- | ----------- |
| 1 | Accounts | Ac1 |
| 2 | Accounts | Ac2 |
| 3 | Accounts | Ac3 |
| 4 | Accounts | Ac4 |
| 5 | Feedback | FbA |
| 6 | Feedback | FbB |
| 7 | Feedback | FbC |
| 8 | Feedback | FbD |
| 9 | Profile | PfOne |
| 10 | Profile | PfTwo |
| 11 | Profile | PfThree |
Desired Result
| Row | Category | Subcategory |
| --- | -------- | ----------- |
| 1 | Accounts | Ac1 |
| 5 | Feedback | FbA |
| 9 | Profile | PfOne |
Here is another approach (assuming that your posted "Row" column is not actually part of your data, but that your "Category" header is, in fact, in A1):
=ArrayFormula({A1:B1;FILTER(A2:B,A2:A<>INDIRECT("A1:A"&ROWS(A:A)-1))})
Try:
={A1:C1;FILTER(A2:C,B1:INDEX(B:B,COUNTA(B:B),1)<>B2:B)}
go for:
=SORTN(A1:C, 9^9, 2, 2, 1)

Google Sheets - Conditional Formatting a dynamically resizing `Total` Row

I am trying to conditional format a dynamically resizing Total row of multiple tables stacked one above the other. The number of cells in the Total row may change depending upon the selection in a Year dropdown.
A | B | C | D | E | F | G | H | I | J | K | L | M |
<Year dropdown>
|Issues|2020-Jan|2020-Feb|2020-Mar|2020-Apr|2020-May|2020-Jun| | | | | | |
--------------------------------------------------------------------------------
| abc | 9 | 2 | 2 | 1 | 3 | 8 | | | | | | |
| def | 1 | 3 | 7 | 1 | 5 | 3 | | | | | | |
| ghi | 2 | 1 | 3 | 1 | 1 | 2 | | | | | | |
--------------------------------------------------------------------------------
|Total | 12 | 6 | 12 | 3 | 9 | 13 | | | | | | |
--------------------------------------------------------------------------------
I am using this formula to highlight the Total row. However, it is highlighting the entire row depending upon what range is set in the Conditional Formatting > Apply to Range box. I want to highlight only the numeric cells i.e. exclude blank cells.
=ISNUMBER(SEARCH("total",$B1))
This highlights the entire Total row including any blank cells. I modified it to exclude any blank cells in row, but no joy!
=AND(ISNUMBER(SEARCH("total",$B1)), SEARCH("total",$B1)<>""))
You can also do it like this, relying on relative addressing to modify the formula as it's effectively dragged across the formatted area:
=and(isnumber(search("Total",$B3)),B3<>"")
Use formula:
=(ISNUMBER(SEARCH("total",$B1))+(($B1:$Z1<>"")))>1

Automated way to create a confusion matrix in Google Sheets?

I have a table of this form in Google Sheets:
+---------+------------+--------+
| item_id | prediction | actual |
+---------+------------+--------+
| 1 | 1 | 1 |
| 2 | 1 | 1 |
| 3 | 1 | 0 |
| 4 | 0 | 1 |
| 5 | 0 | 0 |
| 6 | 1 | 1 |
+---------+------------+--------+
And I'd like to know if there's an automated way to get this kind of summary, with the counts of items that fit the criteria specified in that row/column combination:
+----------+--------------+--------------+-------+
| | prediction=0 | prediction=1 | total |
+----------+--------------+--------------+-------+
| actual=0 | 1 | 1 | 2 |
| actual=1 | 1 | 3 | 4 |
+----------+--------------+--------------+-------+
| total | 2 | 4 | |
+----------+--------------+--------------+-------+
I've been doing this somewhat manually in Google Sheets by using COUNTIFS, but I'm wondering if there's a built-in way? I tried using pivot tables, but couldn't figure out how to get the calculated fields to show the data I want.
A coworker figured it out - you can get this by creating a pivot table with the correct columns and rows, and setting the value to item_id summarized by COUNTUNIQUE.

Query a count of unique attributes in a table

I have a table like this:
+--------+-------+--------+-------+
| attr1 | attr2 | attr3 | attr4 |
+--------+-------+--------+-------+
| purple | wine | clear | 10.0 |
| red | wine | solid | 20.0 |
| red | beer | cloudy | 10.0 |
| purple | ale | clear | 34.0 |
| blue | ale | solid | 16.0 |
+--------+-------+--------+-------+
that i want to transform like this:
+--------+-------+-------+-------+-------+
| | attr1 | attr2 | attr3 | attr4 |
+--------+-------+-------+-------+-------+
| purple | 2 | | | |
| red | 2 | | | |
| blue | 1 | | | |
| wine | | 2 | | |
| beer | | 1 | | |
| ale | | 2 | | |
| clear | | | 2 | |
| solid | | | 2 | |
| cloudy | | | 1 | |
| 10.0 | | | | 2 |
| 20.0 | | | | 1 |
| 34.0 | | | | 1 |
| 16.0 | | | | 1 |
+--------+-------+-------+-------+-------+
This pivoted or cross-table will show me the count of each attribute value in their respective columns.
How do i use the Google Query language to display such a cross-table?
Well if the data were laid out in two columns it would be straightforward e.g. for something like this
Attrib Column
Red 1
Red 1
Green 1
Blue 1
Beer 2
Ale 2
Ale 2
you could use a query like
=query(A:B,"select A,count(A) where A<>'' group by A pivot B")
So the problem is to organise OP#s data into two columns.
This can be done by what is by now a fairly standard split/join/transpose technique
=ArrayFormula(split(transpose(split(textjoin("|",true,if(A2:D="","",A2:D&" "&column(A2:D))),"|"))," "))
Giving
You could either run the query on the result of this or combine the two like this
=ArrayFormula(query({"Attrib","Number";split(transpose(split(textjoin("|",true,if(A2:D="","",column(A2:D)&"-"&A2:D&" "&column(A2:D))),"|"))," ")},"Select Col1,count(Col1) group by Col1 pivot Col2"))
I have joined the column number to the attribute e.g. 1-blue so that it sorts into the right order. If you don't like it, you could get rid of it using regexreplace.
Edit
Slightly shorter formula - I didn't need to put the headers in separately:
=ArrayFormula(query(split(transpose(split(textjoin("|",true,if(A2:D="","",column(A2:D)&"-"&A2:D&" Attr"&column(A2:D))),"|"))," "),
"Select Col1,count(Col1) group by Col1 pivot Col2",0))
Edit 2
I was being a bit thick there, should have used first row of OP's data as attribute labels instead of column numbers
=ArrayFormula(query(split(transpose(split(textjoin("|",true,if(A2:D="","",column(A2:D)&"-"&A2:D&" "&A1:D1)),"|"))," "),
"Select Col1,count(Col1) group by Col1 pivot Col2",0))
Edit 3
Should have chosen a better pair of delimiters
=ArrayFormula(query(split(transpose(split(textjoin("😊",true,if(A2:D="","",column(A2:D)&"-"&A2:D&"🍺"&A1:D1)),"😊")),"🍺"),
"Select Col1,count(Col1) group by Col1 pivot Col2",0))

Googlesheets - Get the sum of a special string column

How can I sum up a sheet with many columns looks like below (list 3 column for example):
|-------+-----------|
| 1 | HKD 2,010 |
|-------+-----------|
| 2 | HKD 1,010 |
|-------+-----------|
| 3 | HKD 2,020 |
|-------+-----------|
| Total | HKD 5,050 |
|-------+-----------|
Try in B1:
=ARRAYFORMULA(sum(value(REGEXEXTRACT(B2:B, "[^' ']*$"))))

Resources