Increment cell value by 1 each time condition is met - google-sheets

I'm trying to check the value of multiple cells containing integers within a single formula, each time the condition is true, increment the cell value by 1 e.g. IF C11 is greater than 300 THEN add 1 to the cell value and IF D11 is greater than 500 THEN add another 1 to the value so I end up with a final value of 2.
I've tried using IFS and an ARRAYFORMULA but neither seem to work e.g. =IFS(F11>300,+1,E11>500,+1)

If you consider that each met condition is 1, you can use the equivalence of TRUE and FALSE with 1 and 0:
=(E11>300)+(F11>500)
Even with arrayformula:
=ArrayFormula((E11:E>300)+(F11:F>500))

Related

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 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.

Using nested IF/AND statements in Sheets in a single cell

I'm trying to create a formula that looks at a cell and determines what range the cell is between and then fills another cell with a specific value depending on the target cell's value. This is the formula I've created:
=IF(AND(C4>0,C4<5,1,IF(AND(C4>=5,C4<8,2,IF(C4>=8,3)))))
Where C4 is the number of hours an employee worked in one day. I'm trying to determine if they were eligible for 1, 2 or 3 10 minute breaks and assign that value to C6.
What I'm getting is this message:
Error
Wrong number of arguments to IF. Expected between 2 and 3 arguments,
but got 1 arguments.
How do I fix this?
IF() function should have two parameters and optionally a third one.
IF(logical_expression, value_if_true, value_if_false)
The IF statement works as follows:
IF(logic_test, value_if true, value_if_false)
Your first IF statement has only the logic_test part (the output of AND). so you have to provide the other parameters.

Using COUNTIFS or dcounta instead of multiple COUNTIF

In our Staff timetable, employees can have an "A"shift (which starts at 9am) a "B" shift (which starts at 10:30am) etc.
I need to know how many shifts in total the employees make so what I use now is a multiple times COUNTIF to count the presents of a few arguments in a range of cells
=countif(D8:BM8;A43)+countif(D8:BM8;A44)+countif(D8:BM8;A45)+countif(D8:BM8;A46)++countif(D8:BM8;A47)
Where field A43 contains "A" field A44 cointains "B" etc.
This works perfect, however, I want to have a smarter way to do this, so I tried to use "COUNTIFS" but the result is always 0 and I can't find why
=COUNTIFS(D8:BM8;A43;D8:BM8;A44;D8:BM8;A45;D8:BM8;A46;D8:BM8;A47)
if looks like all arguments are checked with a logical and, but I need a logical and in this case or a solution with dcounta
You are getting a 0 because there is no cell that will meet ALL conditions.
Instead, maybe try something like
=sum(ArrayFormula(--regexmatch(D8:BM8; textjoin("|"; 1; A43:A47))))
Regexmatch returns a boolean, for all the cells in D8:BM8 (true if a match is found, false otherwise). These booleans are converted into 1 and 0 (see the -- in front of the regex). Then all those values are summed.
Copy and paste the code in a module.(VBA)
Select the cell you want to have the results
Use COUNTIFMATCH Function as the usally functions.
The first Argument is the range you want to count. The second Argument is the range with your criteria. e.g
=COUNTIFMATCH($A$1:$A$20,$C$1:$C$3) or =COUNTIFMATCH($A$1:$A$20,($B$1,$D$1))
whatever you need based on your needs.
Option Explicit
Function COUNTIFMATCH(Range As Range, Criteria As Range) As Integer
'counts the number of cells in one range by the values ​​of another range.
Dim datax As Range
Dim rslt As Range
Dim i As Integer
Set rslt = Range
For Each datax In Criteria
i = i + WorksheetFunction.CountIf(rslt, datax)
COUNTIFMATCH = i
Next datax
End Function

How does this formula to return the last value in a column work?

This is a formula which returns last value in a column:
=INDEX(A:A;MAX((A:A<>"")*(ROW(A:A))))
Can't understand how it works because the part MAX((A:A<>"")*(ROW(A:A))) returns 0.
Any ideas?
For the sake of an answer. The segment:
=MAX((A:A<>"")*(ROW(A:A)))
returns 0 if the row it is in has a blank cell in ColumnA, otherwise 1.
As an array formula (Ctrl+Shift+Enter before Enter) to get:
=ArrayFormula(MAX((A:A<>"")*(ROW(A:A))))
returns the row number of the last populated cell in ColumnA.
Simplifying this to an example of three rows with the first (Row1) blank:
the 'not blank' part returns an array of: FALSE,TRUE,TRUE
and the ROW part of 1,2,3
so multiplying (the *) these together gives an array 0,2,3 (multiplying by FALSE is equivalent to multiplying by 0 and by TRUE equivalent to times 1).
The MAX function then selects the largest value (3).
The INDEX function then 'reads' the above to return the value in ColumnA in that last (third) row.

Resources