How to use a function as a criteria in COUNTIF - google-sheets

I have a bunch of columns that holds scores like 3-1 1-4 1-0 2-2.
I would like to count all columns that are winning, ie. left number is higher than right one.
I have this formula to know if a column is winning : =LEFT(H2; FIND("-"; H2)-1)-RIGHT(H2; FIND("-"; H2)-1)
What I want to do now is to use this formula within COUNTIF, something along the line :
=COUNTIF(A1:A10; "LEFT(CURRENT_COLUMN; FIND("-"; CURRENT_COLUMN)-1)-RIGHT(CURRENT_COLUMN; FIND("-"; CURRENT_COLUMN)-1)")
Is there any way I can do it with a single formula ?

Complex filtering like this can be done with filter, and then the results can be counted with counta. Example:
=counta(filter(A:A, LEFT(A:A, FIND("-", A:A)-1) > RIGHT(A:A, FIND("-", A:A)-1)))
The first argument of filter is the range to be filtered; the second is a formula based on that range (or another range with the same row count) which returns True or False. The rows where the formula evaluates to True are returned.

Related

Rank column with arrayformula and cull duplicates based on another column

I'm struggling with a Google Sheets arrayformula. I want to make an arrayformula to show rank position of Column B values (TOT). With my formula I just achieve a copy of the first position. I don't want duplicate values in the rank, so if there is two equal values the Column A sets the priority.
={"POS";ARRAYFORMULA(IF($B2$B:$B<>"";RANK.EQ(B3;B3:B42;0)))}
Test Document:
https://docs.google.com/spreadsheets/d/1qzs1nvpzG0VgygxbXF9bKxvGaAq-H29oI1aquupYNFc/edit?usp=sharing
If you put this in cell C2, it will rank them as you're trying to do. If you wanted to match the other direction (lowest to highest), then swith the sort parameter from false to true.
=Filter(match(B2:B+(A2:A/1000),
Sort(Filter(B2:B+A2:A/1000,B2:B<>""),1,false),0),A2:A<>"")
You also appear to have duplicates including in column A (example see rows 4,25,35). If you wanted to exclude the duplicates (effectively reducing the number of ranked values), This can be done using the unique function which would create a 17 in each row.
=Filter(match(B2:B+(A2:A/1000),
Unique(Sort(Filter(B2:B+A2:A/1000,B2:B<>""),1,false),0)),A2:A<>"")

ARRAYFORMULA Auto-Increment

I'm trying to understand this auto-increment formula, that my colleague has written. I understand how arrayformula usually works and also countifs.
Formula from the screen: =ARRAYFORMULA(COUNTIFS(ROW(B2:B), "<="&ROW(B2:B)))
I'm stacked about why ROW(B2:B) (1param in COUNTIFS) as a range works fine. It should be a range, not just a number that ROW function returns.
I have been trying to find an answer, read documentation, but nothing helped.
I think that, for example, for 4th line the formula would look like this (if we seperate from ARRAYFORMULA):
COUNTIFS(ROW(B4:B), "<="&ROW(B4:B)),
COUNTIFS(4, "<=4")
I need to understand this code, not other solutions.
The best way to understand how an ARRAYFORMULA works is to write down the equivalent drag-down formula.
The equivalent drag-down formula for:
=ARRAYFORMULA(COUNTIFS(B2:B, B2:B, ROW(B2:B), "<="&ROW(B2:B)))
is
=ARRAYFORMULA(COUNTIFS($B$2:$B, B2, ROW($B$2:$B), "<="&ROW(B2)))
=ARRAYFORMULA(COUNTIFS($B$2:$B, B3, ROW($B$2:$B), "<="&ROW(B3)))
=ARRAYFORMULA(COUNTIFS($B$2:$B, B4, ROW($B$2:$B), "<="&ROW(B4)))
...
(We only need ARRAYFORMULA because ROW($B$2:$B) is an array formula, which means that if you type it in a cell without wrapping it an an array-enabling function it will only evaluate ROW($B$2))
If we recall the COUNTIFS parameters:
=COUNTIFS(criteria_range1, criterion1, criteria_range2, criterion2)
We can see that in the drag-down formula, every parameter that by default expects a range, becomes an absolute reference and every parameter that by default does not expect a range, is just a single value that increments each row.
This is true for any other function: if a function has a parameter that by default expects a range, when we wrap it in ARRAYFORMULA(), that range stays the same throughout the entire computation, which means that every single value in that range is seen by the array formula at any time. What increments, and therefore is only seen by the array formula on that specific row, are the parameters that do not natively expect a range.
This seems like an obvious observation but I'm sure it's the reason many people are confused about how that formula works.
If you understand this concept, then you can also understand how the other variants of the formula work:
=ARRAYFORMULA(COUNTIFS(B2:B, B2:B, ROW(B2:B), "<"&ROW(B2:B)))
=ARRAYFORMULA(COUNTIFS(B2:B, B2:B, ROW(B2:B), ">="&ROW(B2:B)))
=ARRAYFORMULA(COUNTIFS(B2:B, B2:B, ROW(B2:B), ">"&ROW(B2:B)))
every next row is by logic an incrementation of the previous row +1. what this formula does it checks the given row number against row number. for example, for row 4, COUNTIFS checks if ROW(A4) is smaller or equal to ROW(A4). then the evaluation is "yes, row 4 is equal to row 4" and the output is TRUE. what COUNTIF actually does is counting these TRUE outputs up to every row summing all the previous rows. something like:
rows
COUNTIFS processing
output
counting TRUEs
final output
row 1
row 1 equal to row 1?
TRUE
1st TRUE
1
row 2
row 2 equal to row 2?
TRUE
2nd TRUE
2
row 3
row 3 equal to row 3?
TRUE
3rd TRUE
3
row 4
row 4 equal to row 4?
TRUE
4th TRUE
4
row 5
etc
etc
etc
etc

How can I find the name of the person ranked 1?

What I'm trying to do is find the name of the person who is ranked number 1 in the table shown below. I have tried =LOOKUP and =VLOOKUP but I get an error saying that a result can't be found, even though it's obviously there. I assume that I'm either using the wrong function or just not using it right.
I tried =VLOOKUP(1;D2:H19;1) and =LOOKUP(1;D2:H19;1) but neither seems to work.
Answer
The following formula should produce the behaviour you desire:
=INDEX(D2:D,MATCH(1,H2:H,0))
Explanation
=VLOOKUP can only be used to find values to the right of a search key. To find values to the left of a search key, use a combination of =INDEX and =MATCH.
The =MATCH function searches a specified range for a specified value and returns the relative position of the value in that range. In this case, the value to search for is 1, the range to search through is H2:H, and the 0 at the end specifies that the range is not sorted in any way.
The =INDEX function returns the contents of a cell within a range having a specified row and column offset. In this case, the range is D2:D, and the row is whichever row is returned by =MATCH. =INDEX could take a third argument if it was desired to specify a row offset as well, but that is not necessary here.
Functions used:
=INDEX
=MATCH
You sort your ascending order based on rank then return your desired data using INDEX() function. Try-
=INDEX(SORT(D2:H500,5,1),1,1)
=vlookup(1,{H2:H19, D2:D19},2)
Since vlookup only searches the 1st column of the input range, to use it, you need to flip the columns by composing a local array: {H2:H19, D2:D19}.
{} means concatenation. , in it means horizontal concatenation. With that, the rank column is now the 1st column in the input of vlookup and now vlookup works.
With our local array, the 2nd column are the names and therefore index should be 2.
Also note the use of comma to separate function inputs.
your VLOOKUP formula should look like:
=VLOOKUP(1, {H2:H19, D2:D19}, 2, 0)
also try just:
=FILTER(D:D; H:H=1)
or:
=SORTN(D:D; 1; 1; H:H; 1)
You can use query (usefull in case of ex aequo)
=query(D2:H,"select D where H=1",0)

How to print different values to column based on value in another column and other conditions met?

I have a spreadsheet with several columns and I want to return a different value based on the value in Column A and if any of the other columns show a true or 1.
For example
If column A has the value "A" and any column B-N is either TRUE or 1 then I want to return "Good" to column O
If column A has the value "B" and any column B-N is either TRUE or 1 then I want to return "Best" to column O
Link to spreadsheet: https://docs.google.com/spreadsheets/d/12k9usKsOgrOUhtW5WBvfY7WB5hbfnTMSPgXtrqcRFjM/edit?usp=sharing
Try this in cell O2 (where your sample sheets only has values of TRUE, FALSE, Y or N in B2:N):
=arrayformula((trim(transpose(query(transpose(iferror(regexreplace(regexreplace(text(B2:N,),"(FALSE)|(N)",),"(Y)|(TRUE)",if(A2:A="A","Good",if(A2:A="B","Best",))),)),,columns(B2:N))))))
Alternative where values in B:N are either 0 or 1:
=arrayformula((trim(transpose(query(transpose(iferror(substitute(substitute(B2:N,0,),1,ifs(A2:A="A","Good",A2:A="B","Best")),)),,columns(B2:N))))))
B2:N is the range of cells to process.
The inner SUBSTITUTE clears all 0 values.
The outer SUBSTITUTE swaps 1 values for a test to see if values in A contain "A" or "B".
IFS does the test and returns either "Good" or "Best".
IFERROR hides any #N/A values down the sheet where the rows are empty.
TRANSPOSE transposes the data for QUERY.
QUERY is used to collapse empty cells (vertically). columns(B2:N) is used in the header part of QUERY. This is a quirk of QUERY, where the header number >= the columns of data, QUERY does the collapse.
TRANSPOSE reverts the dataset to the previous orientation.
TRIM removes leading or trailing space.
ARRAYFORMULA allows the formula to automatically cascade down the sheet, rather than you needing to drag the formula down (like with =IF(AND({B2:N2}>0, A2="A"),"Good", "")).
So, you already have this partial solution. To get a second condition to be met before printing something to your cell, just add an AND() with a new COUNTIF(), comparing A column with A, and then at the else argument, repeat your original IF(), just changing A for B and the output for each case. I will look like this:
=IF(AND(countif(A2;"A");OR(countif(A2:N2;"Y");countif(A2:N2;TRUE)));"Good";
IF(AND(countif(A2;"B");OR(countif(A2:N2;"Y");countif(A2:N2;TRUE)));"Best";"BAD"))
To use it on every row, just autofill the column O. The row numbers will change accordingly and work on its own.
If you need a new if() statement for a third or fourth case, just repeat it, nesting one IF() inside the other, leaving room for a default error message at the end.

Third Parameter in Array Formula to check for highest(max) occurence but only if the other conditions are true first

Ok, this is the updated linkI have multiple criteria to look through in my arrayformula(index(match())). The first two are simple as they reference the row the formula is calculating on. The last conditional I have is to find the highest occurrence in a given range, but ONLY if the other conditions are met...something like a filtered maxifs..any ideas?
Here is my code in column P =iferror(ArrayFormula(index($F:$F,match(1,("Fee Taken"=$C:$C)*(H12=$H:$H)(maxifs($M:$M,$H:$H,H2,$C:$C,"Fee Taken"),0))),""))
The result that I would like is to return from column F if the name matches that rows name, the transaction type is "Fee Taken" from column C, and THEN if those conditions are true I want it to find the max value from column M based on those two criterias and return the column F value for that max value row..
Ive attached some pictues to show my data.
The last part of the Match function where I have the Maxifs equaling to eachother is where I am confused; my thoughts were to see if the maxifs for the item in Column "M" can be used as a criteria..but I do not think so....I only want the highest occurence F:F if both conditions are met and it is the highest value for both those criteria in column M..
Please let me know if you need anymore info..Thanks![
Working formula will be:
=ArrayFormula(index($F:$F,match(1,--(M:M=(maxifs($M:$M,$H:$H,H2,$C:$C,"Fee Taken"))),0)))

Resources