IF function with 3 conditions - excel

I'm looking to create a formula with 3 conditions. It is currently only working with 2 conditions. Here's what I'm looking for:
E9 has a number
If the number is 21+ then I want it to show Text 1
If the number is between 5 and 21, then I want it to show Text 2
If the number is below 5, then I want it to show Text 3
This is what I currently have:
=IF(E9>21,"Text 1",IF(E9<21,E9>5,"Text 2")
When I try and add the final condition, it gives me an error that I've entered too many arguments for this function. When the number is below 5 it shows False.
I would prefer a solution that does not use VLOOKUP.
I'm not understanding why it's saying this is not allowed, I have another IF function with 5 nested formulas that works just fine.

You can do it this way:
=IF(E9>21,"Text 1",IF(AND(E9>=5,E9<=21),"Test 2","Text 3"))
Note I assume you meant >= and <= here since your description skipped the values 5 and 21, but you can adjust these inequalities as needed.
Or you can do it this way:
=IF(E9>21,"Text 1",IF(E9<5,"Text 3","Text 2"))

Using INDEX and MATCH for binning. Easier to maintain if we have more bins.
=INDEX({"Text 1","Text 2","Text 3"},MATCH(A2,{0,5,21,100}))

=if([Logical Test 1],[Action 1],if([Logical Test 2],[Action 1],if([Logical Test 3],[Action 3],[Value if all logical tests return false])))
Replace the components in the square brackets as necessary.

You can simplify the 5 through 21 part:
=IF(E9>21,"Text1",IF(E9>4,"Text2","Text3"))

Related

How to fix the #SPILL! Error by displaying only the second value?

I have a column with some info displayed like that:
Product Info
I am the 3rd product from 2020
I was created in 1995 and I went public in 2021
I am a not sure if I'm from 2019 2020 2021
I have a formula to extract the year in the above column that is:
=IFERROR(FILTERXML("<k><m>"&SUBSTITUTE([#[Product Name]]," ","</m><m>")&"</m></k>","//m[.=number() and string-length()=4]"),"")
The problem with this formula is that it works fine with the first case, but it gives me a #SPILL! Error on the other two cases. My ideal output would be:
Product Info
Year
I am the 3rd product from 2020
2020
I was created in 1995 and I went public in 2021
2021
I am a not sure if I'm from 2019 2020 2021
Basically, for the first case, just return the 4 digits. EVERY time that I only have one sequence of 4 digits, I want to return that sequence.
For the second case, I want to return ONLY the second year. EVERY time I have 2 sequences of 4 digits, I want to return ONLY the second year.
For the third case, I want to return nothing. EVERY time I have more than 2 sequences of 4 digits, I want to return blank.
The last thing I tried to add was position()>5 and that would cut off the 1995 in the second example, but I would continue having the Error on the third example. Also, my list is quite huge, and I am not sure if the position()>5 thing would work for ALL products that fall in the same second example.
I am not very good with XPATH, so any help would be greatly appreciated.
Thank you!
Disclaimer: Below solution is written on the assumption that when 'count of years < 3', return the last given year. If 'count >= 3' then only return the last year if years come in pairs of two. Hence the use of 'modulus 2 == 0'.‡
You can expand the xpath for sure if you so desire. However, I'd rewrite it a little bit. Each predicate, the structure between the opening and closing square brackets, is a filter of a given nodelist. To write multiple of these structures is in fact anding such predicates. To get a better understanding of what most common xpath 1.0 functions can do within FILTERXML(), I'd like to redirect you to this post.
So to write a consecutive pattern of predicates I'd opt for:
[.*0=0] - First return a filtered nodelist of all numbers where a node multiplied by zero equals zero;
[string-length()=4] - Then return only those that are 4 characters long‡‡;
[position() = last() and (position() = 1 or position() mod 2 = 0)] - The 3rd and last predicate is the trickiest for your query. This is done with a first check that position() = last() meaning the node needs to be the last node in the filtered nodelist of step 2 and (position() = 1 or position() mod 2 = 0) means we want to check that this node is also at the 1st index or the modulus 2 of the indexed position equals 0‡‡‡.
Formula in B2:
=IFERROR(FILTERXML("<t><s>"&SUBSTITUTE(A2," ","</s><s>")&"</s></t>","//s[.*0=0][string-length()=4][position() = last() and (position() = 1 or position() mod 2 = 0)]"),"")
Whilst the above would work for Excel 2013 and higher‡‡‡‡, you do talk about spilled behaviour. If you happen to work with the current channel in ms365 you could also try:
=LET(x,TEXTSPLIT(A2," "),y,--FILTER(x,ISNUMBER(-(x&"**0"))*(LEN(x)=4),{1,2,3}),z,COUNT(y),IF(OR(z=1,MOD(z,2)=0),TAKE(y,,-1),""))
‡ If you need to simply return the last year if 'count < 3' then you can use xpath "//s[.*0=0][string-length()=4][position()<3 and position() = last()]" or ms365 formula =LET(x,TEXTSPLIT(A2," "),y,FILTER(x,ISNUMBER(-(x&"**0"))*(LEN(x)=4),""),IF(COUNTA(y)>2,"",TAKE(y,,-1))).
‡‡ Note that you can be more strict about this if you'd wish to validate that a year is between say 1900-2050 or so. One could replace the 1st and 2nd predicate with [.*1>1899][.*1<2051].
‡‡‡ Note that the order or writing your and/or statements in xpath do matter. We need to use explicit parentheses to control the precedence. See this
‡‡‡‡ This is not true for Excel Online or Excel for Mac
Just add a simple clause to determine the number of returns, for example using ROWS (since by default FILTERXML returns a vertical array):
=LET(
ζ, FILTERXML(
"<k><m>" &
SUBSTITUTE(
[#[Product Name]],
" ",
"</m><m>"
) & "</m></k>",
"//m[.=number() and string-length()=4]"
),
ξ, ROWS(ζ),
IF(ξ > 2, "", INDEX(ζ, ξ))
)
Edit: I might prefer to avoid FILTERXML here:
=LET(
ζ, TEXTSPLIT([#[Product Name]], " "),
ξ, -(ζ & "**0"),
IF(COUNT(ξ) > 2, "", IFERROR(-LOOKUP(1, FILTER(ξ, LEN(ζ) = 4)), ""))
)
You can try the following using TEXTAFTER function. Assuming you have years at the end delimited by space. If that is not the case, the formula can be adapted to have additional checks (it is a number and four-digit, but strictly speaking a year can have less or more than 4 digits). Let me know if the previous assumption doesn't apply so I can try to adapt it. The following is an array version, so you can use the entire table column in case you are using excel tables:
=LET(in,A2:A4,last,TEXTAFTER(in," ",-1),
IF(ISNUMBER(1*TEXTAFTER(SUBSTITUTE(in," "&last,"")," ",-1)),"",last))
For the case of more than one year, it removes the last year found, and if the second search is a number, then it returns empty, otherwise returns the previous year found.

IF Statments in EXCEL

I am trying to calculate different price decreases when each statement is true for example
Statement 1 - Y
Statement 2 - N
Statement 3 - N
Statement 4 - N
I have been able to get it to work with one of the statements being true using
=IF(EXACT(L9,"Y"),I29-I9*C21,I29)
However, I don't know how I would be able to add that together if all 4 of the statements were Y as each of the statements is taking away different amounts of money if True.
My guess for it was
=IF(EXACT(L9,"Y"),I29-I9*C21,I29,IF(EXACT(L8,"Y"),I32-I8*C20,I32))
however, to many arguments were being made for the function
any help would be much apricated :)
This is what my excel document looks like at the moment. Don't think I did very well trying to explain it :/.
Excel Document
So more or less what I have been trying to do is when bought is changed to Y it would change that total however I've only been able to find out how to do it with one statement which has been changed to Y using:
=IF(EXACT(J6,"Y"),H18-H6*I6,H18)
sorry for the lack of being to explain much :/
This is how you can build more complex logical functions with IF statements.
If all four statements equal "Y"....
=if(and(A1="Y",B1="Y",C1="Y",D1="Y"),[Calculation if True],[Calculation if False])
So, here is an example of multiple tests nested in a statement.
Test 1: Are all 4 cells equal to "Y"?
If yes, then "Test 1 equals True".
If not,
Test 2: Are the first 3 cells equal to "Y" and the 4th cell equal "N"?
If yes, then "Test 2 equals True"
If not, "Neither Test 1 or Test 2 equal True"
This is the formula:
=if(and(A1="Y",B1="Y",C1="Y",D1="Y"),"Test 1 equals True",if(and(A1="Y",B1="Y",C1="Y",D1="N"),"Test 2 equals True","Neither Test 1 nor Test 2 equal True"))

Excel Statement with 4 conditions and 4 answers

All of the methods that I've used have 2 answer values (True or False).
How can I get the following with a formula?
If A1=1 then it's 11, if A1=2 the answer is 22, if A1=3 then it's 33, if A1=4 it's 44.
If the value your are evaluating is in cell A1, then the nested function would be as follows:
IF(A1=1,11,IF(A1=2,22,IF(A1=3,33,IF(A1=4,44,""))))
I put the 2 double commas at the end so the formula returns a blank instead of false.
I don't know if that's what you are asking about, but you can make multiple (nested) IF statements in one. For example:
IF(1=2;TRUE;IF(2=2;TRUE;FALSE))
You just put another IF in the FALSE part of IF statement. If that's not it, can you give a piece of the statement you tried and precise more what do you want?
=IF(AND(INT(A1)=A1,A1<=4,A1>=1),A1*11,"")
Now the above works for the condition you placed in your example, however if one were to go by your title alone you have a couple of options you could go with.
You first Option would be nested IF statements. Like you said each IF function has TRUE or FALSE. The trick is to put another IF function in for the TRUE result and another in for the FALSE results
IF(CHECK1, IF(CHECK2, TRUE2, FALSE2),IF(CHECK3, TRUE3, FALSE3))
The above give 4 potential results based on only 3 checks. Another option would be to do a check and supply a value for a TRUE result and another IF for a false result. Keep repeating the process. Conversely you could go the same route flipping TRUE FALSE option. It might look something like this:
IF(CHECK1, TRUE1, IF(CHECK2, TRUE2, IF(CHECK3, TRUE3, FALSE3)))
FALSE3 would be the result of all previous checks failing.
So for your case, your nested IF could look like (assuming the only valid entries are 1, 2, 3 and 4):
IF(A1=1,11,IF(A1,2,22,IF(A1=3,33,44)))
OR
IF(ISODD(A1),IF(A1=1,11,33),IF(A1=2,22,44))
and there are other options to work through the logic. there are also other checks you could be doing and results being displayed if your entries in A1 were not limited to the integers 1,2,3 and 4.
Now because you example is using the sequential integers 1,2,3 and 4 you could also use the CHOOSE function. Alternatively if you can make your criteria evaluate to sequential integers stating at 1 the CHOOSE function would work as well. Supply choose with an integer as the first argument and it will return the corresponding argument in the list that follows
CHOOSE(ARGUMENT,RESULT1, RESULT2,...,RESULTn-1, RESULTn)
In your case it would look something like:
CHOOSE(A1,11,22,33,44)
If you can not get sequential numbers for whatever reason and the gap is numbers is small and you are in the low integer count, you could leave a gap in results by providing "", or 0). lets say you has 1,3 and 4 as potential arguments, then your choose might look like:
CHOOSE(A1,11,"",33,44)
=IF(A1<>"",INDEX({11;22;33;44},A1),"")
=IF(AND(ISNUMBER(A1),A1<=4),A1*11,"")

Convert number to string in Excel

I'm trying to do some transformation with numbers in excel. First i have that table:
And as you can see, i have Random Digits, which is generated by using RANDBETWEEN. Now i want the Column Type, to be automatically Generated. So for example if Random Digits is:
From 1 - 35 = Good
36 - 80 = Fair
81 - 100 = Poor
I was already trying with IF function, but with if function i'm able to generate only 2 values and not 3.
Thank you for answers.
INDEX and MATCH are a good way to avoid nesting lots of IF statements (generally to be avoided!):
=INDEX({"Good","Fair","Poor"},MATCH(B2,{0,36,81},1))
If you really wanted to use an IF statement, it would look like this:
=IF(B2<36,"Good",IF(B2<81,"Fair","Poor"))
Nest the If so where you get the true value just output what you need but if its false then just write another if statement...
Use one IF inside another IF like this:
=if('From 1 - 35';'thing to do if is true';if('36 - 80';'thing to do if is true';'thing to do when is 81 - 100'))
The excel formula you are looking for is
=IF(B1>100,"error",IF(B1>=81,"Poor",IF(B1>=36,"Fair",IF(B1>=1,"Good","error"))))
This will display the word "error" if you range is >100 or <1. Other answers have failed to address the cases where the number is >100 or <1, as the question specifically bounds the set of responses to be between 1 and 100.
The formula works as a nested if statement. In pseudo code the formula is equivalent to:
if(B1>100)
then "Error"
Else if (B1>=81)
then "Poor"
Else if (B1>=36)
then "Fair"
Else if (B1>=1)
then "Good"
else
"Error"

If-Else ladder in Excel To sort data

I was doing R&D on Excel. And wanted to use If-else ladder in column of excel.
Let's say I have a 2 columns and I calculated the difference between the two columns. Now, If the difference is between a range of (-5 to +5), if should display something or if Difference greater than 5, it should display something and for rest i.e. difference < -5, should display something else.
I tried and came up with this
=IF("H2>5",<something1>, IF("H2<5",<something2>))
How to put the range part in this if-else ladder? Also the above If else is not giving value but the result is turning out to be #VALUE.
Thanks for your help.
Try
=IF(H2<-5,"Negative",IF(H2<=5,"In Range","Positive"))
There could be 3 possibilities only, 1 the answer is between -5 to 5 inculdining -5 and 5. 2 greater then 5. 3 smaller than -5. so this should work
=IF(AND(H2>=-5,H2<=5),"between -5 &5",IF(H2<-5,"Smaller than-5",IF(H2>5,"greater than 5 ")))
let me know if this is what is required.

Resources