if statement to check other rows - Google Docs - google-sheets

I have a spreadsheet along this format:
------------------------------------------------
| A | B | C | D |
------------------------------------------------
| type | wins | loss | ratio |
------------------------------------------------
| cat | 1 | | 1 |
------------------------------------------------
| dog | 2 | | 2 |
------------------------------------------------
| rabbit | | 1 | -1 |
------------------------------------------------
| dog | 1 | | 1 |
------------------------------------------------
| horse | 1 | | 1 |
------------------------------------------------
| dog | | 2 | -2 |
------------------------------------------------
What I want, is to check if the "A" column contains the word "dog" and if it does count the amount of times it has won in the table.
So, the formula would output:
dog total wins: 2
As it won the first time it appeared in the table and the second time it won too. If there is a winner the loss cell will be empty, and visa versa. I don't need it to add up the wins and give me the total sum.
Then, as a second part, I need it to give me the average ratio for "dog", so the formula needs to check if the "A" column contains "dog", if it does, it needs to add up all the "dog" ratios and divide by how many there are.
I have the formula to add up how many times "dog" appears, but the rest I'm stumped on!
=COUNTIF(A2:A;"dog")
Can anyone advise of the correct formula please?

I would do this:
FIRST PART:
=counta(filter(B:B,A:A="dog"))
SECOND PART:
=sum(filter(D:D,A:A="dog"))/countif(A:A;"dog")
Hope this helps.

Related

Google Sheets Return Any(All) Row Value(s) For MAX-GROUP Query

I am looking to return non-grouped row values from a query of a table sorted by the MAX value of a column, within a group.
DATA TABLE
| NAME | ASSET | ACTION | DATE |
|--|--|--|--|
| JOE | CAR | BOUGHT | 1/1/2020 |
| JANE | HORSE | BOUGHT | 1/1/2021 |
| JOE | HORSE | BOUGHT | 2/1/2021 |
| JANE | HORSE | SOLD | 3/1/2021 |
| JOE | CAR | SOLD | 1/1/2022 |
| JOE | CAR | BOUGHT | 2/1/2022 |
For the table above, I presented the following code.
=QUERY(A1:D5,"SELECT A,B,C,D, MAX(D) GROUP BY A,B",TRUE)
The following TARGET TABLE is output I'm looking for:
| NAME | ASSET | ACTION | DATE |
|--|--|--|--|
| JANE | HORSE | SOLD | 3/1/2021 |
| JOE | HORSE | BOUGHT | 2/1/2021 |
| JOE | CAR | BOUGHT | 2/1/2022 |
However, because 'C' is not included in the GROUP, the formula returns an error. "Unable to parse query string for Function QUERY parameter 2: ADD_COL_TO_GROUP_BY_OR_AGG: C"
If I were to omit COL C & D, "ACTION" & "DATE" from the SELECT: =QUERY(A1:D5,"SELECT A,B, MAX(D) GROUP BY A,B",TRUE) , I have the correct record rows, but am missing the STATUS.
MAX-DATE TABLE
| NAME | ASSET | max DATE |
|--|--|--|
| JANE | HORSE | 3/1/2021 |
| JOE | HORSE | 2/1/2021 |
| JOE | CAR | 2/1/2022 |
OR, when I add COL C as a "PIVIOT": =QUERY(A1:D5,"SELECT A,B, MAX(D) GROUP BY A,B PIVOT C",TRUE)I have the correct record rows, but do not have the 'current' STATUS within the record row.
PIVOT ACTION TABLE
| NAME | ASSET | BOUGHT | SOLD |
|--|--|--|--|
| JANE | HORSE | 1/1/2021 | 3/1/2021 |
| JOE | HORSE | 2/1/2021 | |
| JOE | CAR | 2/1/2022 | 1/1/2022 |
Still I have not found a method to create my TARGET TABLE.
Am I overlooking a method to include a non-grouped field into a query using MAX()? Or is it impossible within Google Sheets Query without JOIN functions?
(I hope it is obvious that I desire to apply this to a large and dynamic dataset.)
Thank you for your insight. Cheers!
It's not that flexible to work with QUERYs with its aggregation requisites and so on.
You can create a filter, by comparing column D with a "fictional" column created with BYROW: = BYROW(A2:A,LAMBDA(each,MAXIFS($D$2:$D,$A$2:$A,each,$B$2:$B,OFFSET(each,,1))))
That would look like this (I highlighted the matches and added extra rows for reference):
Then, you can set this filter (don't create this column, it's just a visualization of what I did):
=FILTER(A2:D,D2:D = BYROW(A2:A,LAMBDA(each,MAXIFS($D$2:$D,$A$2:$A,each,$B$2:$B,OFFSET(each,,1)))))
This way, you're comparing the dates with the maximum for each category

Fill in blank cells from several columns in ={ARRAYFORMULA()}

I have a human-friendly sheet with sparse hierarchical data:
SEASON | FRUIT | LETTER
-----------------------------
Winter | |
| Lemons |
| | Delta
Summer | |
| | Alpha
| | Beta
| Pears |
| | Gamma
(Note how Alpha and Beta don't have a FRUIT entry.)
I want to generate a new column, using ARRAYFORMULA(), to contain a full "path" to the LETTER:
SEASON | FRUIT | LETTER | PATH
------------------------------------
Winter | | | Winter//
| Lemons | | Winter/Lemons/
| | Delta | Winter/Lemons/Delta
Summer | | | Summer//
| | Alpha | Summer//Alpha
| | Beta | Summer//Beta
| Pears | | Summer/Pears/
| | Gamma | Summer/Pears/Gamma
Please help me to understand how to write such ARRAYFORMULA().
I'm trying approach, based on answers in Fill in blank cells in ={ARRAYFORMULA()}, but I'm stuck at resetting FRUIT to empty string for a new SEASON. I.e. this naïve implementation would yield Summer/Lemons/Alpha instead of Summer//Alpha:
={ ARRAYFORMULA(
IFERROR(VLOOKUP(ROW(SEASON), IF(SEASON<>"", { ROW(SEASON), SEASON }), 2, 1), "")
& "/" & IFERROR(VLOOKUP(ROW(FRUIT), IF(FRUIT<>"", { ROW(FRUIT), FRUIT }), 2, 1), "")
& "/" & LETTER
) }
Here is a sample spreadsheet created specifically to answer this question.
you will find this formula in cell E1 on a tab called Possible Solution.
=ARRAYFORMULA(IF(LEN(A:A&B:B&C:C),VLOOKUP(ROW(A:A),FILTER({ROW(A:A),A:A},LEN(A:A)),2,1)&"/"&VLOOKUP(ROW(B:B),FILTER({ROW(B:B),B:B},LEN(A:A&B:B)),2,1)&"/"&C:C,))
It uses the VLOOKUP(ROW(),FILTER(),[index],TRUE) technique to append the relevant parts of the path to one another.
Note the portion of the formula in the image which i believe was the crux of the trouble with the strategy you were trying...

How do I collect the column headers that have non-empty cells for each row?

I have a table that lists who is working on what, with numbers to indicate how long they think it will take (the other cells are left blank).
| Alex | Betty | Charlie |
---------------------------------
Task A | 1 | 2 | |
Task B | | | 2 |
Task C | 1 | | 3 |
I would like to generate a column called "Owners" that is a comma-separated list of who is working on each task, based on the non-empty values in the Alex, Betty, and Charlie columns. For example, Task A is being worked on by Alex and Betty, so the value in "Owners" for that row would be "Alex, Betty".
| Alex | Betty | Charlie | Owners
------------------------------------------------
Task A | 1 | 2 | | Alex, Betty
Task B | | | 2 | Charlie
Task C | 1 | | 3 | Alex, Charlie
What's the best way to do this in Google Sheets? Note that my headers may not be in row 1.
use:
=ARRAYFORMULA(REGEXREPLACE(TRIM(TRANSPOSE(QUERY(TRANSPOSE(
IF(B2:D="",,B1:D1&",")),,9^9))), ",$", ))

Count if a cell has the same value as another one in the same column

I'm trying to find a formula to count the number of times, in a given row, a cell has the same value as another one in another row (same column)
Basically, I have something like
-----------------
| 1 | S | M | M |
-----------------
| 2 | M | M | M |
-----------------
| 0 | S | M | S |
-----------------
| 1 | S | M | M |
-----------------
| 3 | M | S | M |
-----------------
....
| | M | S | M | // <--- the reference row
-----------------
The numbers 1, 2, 0, 1, 3 are to be calculated with the formula.
1 -> Only one matches the reference row, which is the last M
2 -> There are two matches, first cell and last cell
Etc...
I have tried stuff like =COUNTIF(E5:Z5, "="&INDIRECT(ADDRESS(21,COLUMN(),2))).
But the COLUMN() has the value of the column where this is written and not the value of the column that is being evaluated in the COUNTIF().
If a general way, I can't seem to find a way to refer to the cell being currently evaluated in the COUNTIF.
May someone has the answer :).
Regards
Should be easier with Sumproduct:
=SUMPRODUCT((B1:Z1=B$21:Z$21)*(B1:Z1<>""))

How to add the content of a certain number of rows just below the VLookup output (Google spreadsheet)?

For instance:
________A_____|__B__|__C__|
1 | Mouse | 1 | a |
2 | Keyboard | 2 | e |
3 | Headset | 3 | i |
4 | HDD | 4 | o |
=Arrayformula(VLOOKUP("Mouse",A1:C4,{2,3},FALSE) --would return "1" & "a".
Is there a way to make it return the content of 1 row below as well, that "2" & "e" along with "1" & "a"? i.e., the desired final output should look like :
________C_____|__D__|__E__|
1 | | 1 | a |
2 | | 2 | e |
Thanx!
Try offset:
=OFFSET(A1,MATCH("Mouse",A:A,0)-1,1,2,2)
Offset in Google sheets is an arrayformula, so it can return multiple rows and columns.
Match function gives the number of row with matched name.

Resources