Multiple Criteria - excel

Is it possible to use an IF statement for multiple criteria? or what would I be best using... I have tried to amend the question with what I'm looking to do the code isnt correct in this but it may highlight more what Im looking to achieve.
If(A3="Dog","Dog Green","Dog Blue"),Matches,doesn’t match)
For some reason it wont let me upload a picture....
Any help/advice would be greatly appreciated

This will definitely work:
=IF(OR(A3="Dog",A3="Dog Green",A3="Dog Blue"),"Matches","doesn’t match")
just ad OR statement inside IF Statement
Don't forget to use Quotation marks also

There are many ways to test for multiple criteria in Excel. Probably the easiest would be either to:
Use the AND / OR BOOLEAN operaters. AND(test1,test2...) checks to see whether each of test1 & test2 are TRUE (can hold as many arguments as you need). ie:
=if(And(A1="dog",B1="cat"),"there is a cat and a dog", "there is not both a cat and a dog")
OR(test1,test2) checks to see whether either test1 is TRUE, or test2 is TRUE, or if both are true. ie:
=if(Or(A1="dog",B1="cat"),"There is either a cat, or there is a dog, or both","there is neither a cat nor a dog")
Another broad option is to 'nest' one IF statement inside of another. ie:
=if(A1="dog","There is a dog. Have not checked for cats",if(B1="cat","A1 is not a dog, and also B1 is a cat","There is neither a cat nor a dog"))
If you have more specific questions you should update your question with all possible details you have.

It's a simple formula you could have found on google:
=ISNUMBER(FIND("dog",A1))
Will return True if dog is in A1 and false if it is not.
For further reading (well explained) https://exceljet.net/formula/cell-contains-specific-text
Also, it seems to me like this is a single-criteria. What do you mean by multiple criteria?

Related

How to simplify 4 nested IF functions in EXCEL

This might be a simple fix I'm unsure. I have the following formula which I would like to be able to drag up and down =IF(C311>B311,IF(C310>B310,IF(B309>C309,IF(C312>B312,2,1),0),0),0)
I'm guessing AND can be implemented in some way but I'm not sure. The logic behind my goal is if C311>B311 AND C310>B310 AND B309>C309 then check to see if C312>B312 if it is then put a 2 else put a 1. Perhaps there is another solution as well I'm not thinking of.
Try this: =IF(AND(C311>B311,C310>B310,B309>C309),IF(C312>B312,2,1),"")
Last argument I left it as "" but replace it to whatever you need.
Just another approach-
=IF((C311>B311)*(C310>B310)*(B309>C309),IF(C312>B312,2,1),"")

Excel: Multiple If with Countif Statements

In Excel, I have three formulas/statements I'd like to merge into one. I've listed the statements below. Is there a way to merge all three formulas into one? I'd like to learn how to write the logic to do this. Thanks.
=IF(COUNTIF($B:$B,$A2)=1, "MATCH")
=IF(COUNTIF($B:$B,$A2)>1, "DUPLICATE")
=IF(COUNTIF($B:$B,$A2)<1, "NO MATCH")
The syntax of the IF function is IF(true-false-condition, value-if-condition-true, value-if-condition-false) (where either missing value-if defaults to 0).
3-way (or more) IF conditionals can be written by nesting multiple boolean IF's, for example:
=IF(COUNTIF($B:$B,$A2)>1, "DUPLICATE", IF(COUNTIF($B:$B,$A2)=1, "MATCH", "NO MATCH"))
Also consider good practice of trapping the default 'else' case. The above answer does not do this, and it assumes your input will always be one of your stated cases (=1, <1 or >1) so omits the final IF condition. In that example this will not error because the evaluations will always return true or false, but this may not behave as expected if an input is a different data type, null set, etc. In the stated answer, this unexpected behaviour will "fail silently" which is a potential issue.
Instead, try:
=IF(COUNTIF($B:$B,$A2)>1, "DUPLICATE", IF(COUNTIF($B:$B,$A2)=1, "MATCH", IF(COUNTIF($B:$B,$A2)<1, "NO MATCH", "ERROR")))
In implementation you would replace "ERROR" with your chosen error handling method.
You could also look at the following Excel functions, as an alternative to nesting multiple IF functions:
SWITCH:
=SWITCH(expression, value1, result1, [default or value2, result2],…[default or value3, result3])
IFS:
=IFS (test1, value1, [test2, value2], ...)
Some of these are newer than others so be sure to test to your version of Excel (and consider compatibility with any other possible users of your file).

INDEX MATCH with Nested IF Statements

Here is my statement with just one IF Statement:
=IF(AF2="Consultant",IF(C2=INDEX(JIRA!F:F,MATCH('RFO Checks'!M2,JIRA!A:A,0)),1,0),"N/A")
This works great, but now I need to add two more IF Statements.
AF2 will either contain "Consultant", "Retailer", or "PC".
Each one will be directed to a different price column:
for "Consultant" it's JIRA!F:F
for "Retailer" it's JIRA!D:D
for "PC", it's JIRA!E:E.
I've been wracking my brain for two days now and haven't gotten no where.
Suggestions?
Thank you in advance!
Use CHOOSE()
=IFERROR(--(C2=INDEX(CHOOSE(MATCH(AF2,{"Consultant", "Retailer", "PC"},0),JIRA!F:F,JIRA!D:D,JIRA!E:E),MATCH('RFO Checks'!M2,JIRA!A:A,0))),"N/A")
Aaron,
I have provided you the high level structure for the change of logic.
If this is what you want, then you can substitute the phrases with the appropriate logic.
=IF(AF2="Consultant",
IF(C2=INDEX(JIRA!F:F,MATCH('RFO Checks'!M2,JIRA!A:A,0)),1,0),
IF(AF2="Retailer",
<insert_logic_for_retailer>,
IF(AF2="PC",<insert_logic_for_PC>,"N/A")
)
)
Logic for Consultant -
IF(C2=INDEX(JIRA!F:F,MATCH('RFO Checks'!M2,JIRA!A:A,0)),1,0)
Logic for Retailer, use this to replace -
IF(C2=INDEX(JIRA!D:D,MATCH('RFO Checks'!M2,JIRA!A:A,0)),1,0)
Logic for PC, use this to replace -
IF(C2=INDEX(JIRA!E:E,MATCH('RFO Checks'!M2,JIRA!A:A,0)),1,0)
Let me know in case you still have any issues!

Using IF with OR, AND and Wildcard in same statement

I'm trying to create an IF statement that will basically look at one cell which currently has a macros enabling multi-select drop down from a lookup list.
The field the IF statement is based on is a drop down containing the following information:
Android
iOS
PC/Mac
All Devices
Mobile Devices
The user can select one value or multiple values.
The IF statement will be true IF, the field contains All Devices OR Mobile Devices OR Android AND iOS together. It must also be able to check wildcard as you can select multiple items therefore could be Android, PC/Mac, iOS in which case it should be true because both Android and iOS has been selected.
What should be the actual statement as this is the current one I'm using but I know you can't have wildcard string within this statement:
=IF(OR(C7="*-iOS*",C7="*-Android*",C7="Mobile Device",C7="All Delivery Methods",AND(C7="*-iOS*",C7="*-Android*")),"Mobile DTC/ALL","STB (VRP) HD, DEVICE (UVP) HD")
Thanks
Edit: If any of the following Scenarios is met and exist in Cell C7, then return the True statement.
Scenario 1: All Delivery Methods
Scenario 2: Mobile Devices
Scenario 3: Android AND iOS
Otherwise return the False statement.
Excel does not support wildcard statements on all formulas (see supported formulas), but you can achieve similar result, if you build a bit your formula.
Solution 1
E.g., try using FIND() with IFERROR(). FIND() will give you a number, telling you where the searched string is located.
Thus, =FIND("-Android","tell me more about -Android"), will give you a number.
If it does not find anything, it will give you a value error, which you may catch with IFERROR and return a specific value e.g. -1.
Then build the OR checking for values bigger than -1 and you will have what you need.
Solution 2
Another option is to use SEARCH(), which supoports wildcards the way you expect it:
=SEARCH("test*1";"aaaaaaaatestaaaa1")
will return 9. SEARCH in MSDN
Solution 3
In general, there are many ways to build this. As suggested in the comments by # ImaginaryHuman072889, you may use ISNUMBER(). It has the advantage, that it works quite ok with errors, thus you do not need an error catcher for it. Something like this will work ok:
=OR(ISNUMBER(SEARCH("a","b")),ISNUMBER(SEARCH("b","b")))
However, if you change the method from the first solution with an option returning 0 in case of error you would have something like this working as well:
=OR(IFERROR(SEARCH("a","b"),0),IFERROR(SEARCH("b","c"),0))
which is quite the same amount of formulas.
Solution 4
A really interesting solution, avoiding both OR() and IFERROR(), proposed by #barry houdini. It simply returns 0, if nothing from the results is found:
=COUNT(SEARCH({"a*a";"b*b";"c*c"},"I used to be abba fan."))
will return 2, because of a*a and b*b being found in abba. This is how to include the AND() of the original question of the OP:
=AND(
COUNT(SEARCH({"a*a";"b*b";"c*c"},"I used to be abba fan.")),
COUNT(SEARCH({"party*so";"am not"},"but now I am not."))
)

With data.table, return between certain characters into a new column

I have a feeling this might be a simple question, but I've searched through SO for a bit now and found many interesting related Q/A, I'm still stumped.
Here's what I need to learn (in honesty, I'm playing with the kaggle Titanic dataset, but I want to use data.table)...
Let's say you have the following data.table:
dt <- data.table(name=c("Johnston, Mr. Bob", "Stone, Mrs. Mary", "Hasberg, Mr. Jason"))
I want my output to be JUST the titles "Mr.", "Mrs.", and "Mr." -- heck we can leave out the period as well.
I've been playing around (all night) and discovered that using regular expressions might hold the answer, but I've only been able to get that to work on a single string, not with the whole data.table.
For example,
substr(dt$name[1], gregexpr(",.", dt$name[1]), gregexpr("[.]", dt$name[1]))
Returns:
[1] ", Mr."
Which is cool, and I can do some further processing to get rid of the ", " and ".", but, the optimist(/optimizer) in me feels that that's ugly, gross, and inefficent.
Besides, even if I wanted to settle on that, (it pains me to admit) I don't know how to apply that into the J of data.table....
So, how do I add a column to dt called "Title", that contains:
[1] "Mr"
[2] "Mrs"
[3] "Mr"
I firmly believe that if I'm able to use regular expressions to select and extract data within a data.table that I will probably use this 100x a day. So thank you in advance for helping me figure out this pivotal technique.
PS. I'm an excel refugee, in excel I would just do this:
=mid(data, find(", ", data), find(".", data))
Umm.. I may have figured it out:
dt[, Title:=sub(".*?, (.*?)[.].*", "\\1", name)]
But I'm going to leave this here in case anyone else needs help, or perhaps there's an even better way of doing this!
You can use the stringr package
library(stringr)
str_extract(dt$name, "M.+\\.")
[1] "Mr." "Mrs." "Mr."
Different variations on the regular expression will let you extract other titles, like Dr., Master, or Reverend which may also be of interest to you.
To get all characters between "," and "." (inclusive) you can use
str_extract(dt$name, ",.+\\.")
and then remove the first and last characters of the result with str_sub (also from stringr package).
But as I think about it more, I might use grepl to create indicator variables for all the different titles that are in the Titanic dataset. For example
dr_ind <- grepl("Dr|Doctor", dt$name)
titled_ind <- grepl("Count|Countess|Baron", dt$name)
etc.

Resources