Mastering the IF Function in MS Excel


Nested Functions - IF, AND, OR and IFERROR

Nested functions allow you to use multiple functions inside each other to perform more complex tasks. This is particularly useful when you need multiple conditions to be met before performing a calculation or returning a result. It is a powerful tool for decision-making, especially when combining logical functions like IF, AND, OR, and IFERROR. Before diving into how the IF function works, it’s important to understand the Comparison Operators. These are used to compare values in cells and help determine whether certain conditions are true or false.

Comparison Operators in Excel

">" Greater Than Checks if the value on the left is greater than the value on the right. =IF(A1 > 100, "Yes", "No") returns "Yes" if A1 is greater than 100, otherwise "No"
"<" Less Than Checks if the value on the left is less than the value on the right. =IF(A1 < 100, "Below", "Above") returns "Below" if A1 is less than 100, otherwise "Above"
"=" Equal To Checks if the value on the left is equal to the value on the right. =IF(A1 = 100, "Exact Match", "No Match") returns "Exact Match" if A1 is equal to 100, otherwise "No Match"
">=" Greater Than or Equal To Checks if the value on the left is equal to the value on the right. =IF(A1 >= 50, "Pass", "Fail") returns "Pass" if A1 is 50 or more, otherwise "Fail"
"<=" Less Than or Equal To Checks if the value on the left is less than or equal to the value on the right. =IF(A1 <= 20, "Low", "Normal") returns "Low" if A1 is 20 or less, otherwise "Normal"
"<>" Not Equal To Checks if the value on the left is not equal to the value on the right. =IF(A1 <> 0, "Not Zero", "Zero") returns "Not Zero" if A1 is not equal to 0, otherwise "Zero"

When to Use These Operators:

These comparison operators are often used in logical tests (like in the IF function) to help you compare values in cells and return results based on whether the condition is true or false.

Now that you have good understanding of Comparison Operators lets dive into IF Functions

1. IF Function

The IF function allows Excel to make decisions. It checks whether a condition is met and returns one value if true, and another if false. It's one of the most fundamental and widely-used functions in Excel.

Syntax:

=IF(logical_test, value_if_true, value_if_false)

Use Case:

Scenario: You want to determine whether sales targets were met. If the sales are greater than or equal to $10,000, return "Target Met". Otherwise, return "Target Not Met".

=IF(B2 >= 10000, "Target Met", "Target Not Met")

If Target Met

Explanation: The formula checks the value in the "Sales Amount" column:

  • For Item 1, the sales are $5,000, which is less than $10,000, so the result is "Target Not Met".
  • For Item 2, the sales are $12,000, which is greater than or equal to $10,000, so the result is "Target Met".
  • For Item 3, the sales are $9,000, which is less than $10,000, so the result is "Target Not Met".
  • For Item 4, the sales are exactly $10,000, so the result is "Target Met".
  • For Item 5, the sales are $16,000, which is greater than $10,000, so the result is "Target Met".

2. AND Function

The AND function allows you to test multiple conditions at once. It returns TRUE if all conditions are met, and FALSE if any condition fails.

Syntax:

=AND(condition1, condition2, ...)

Use Case:

Scenario: You are evaluating employees for a bonus. The employee will only receive the bonus if they have worked more than 40 hours AND achieved sales over $10,000.

=AND(B2 > 40, C2 > 10000)

AND Function

Explanation: The formula checks the value in the "Hours Worked" and "Total Sales" columns:

  • For Salesman-1, hours worked are 42 and total sales are $5,000, so the result is False because both conditions are not met, worked more than 40 hours but total sales is not above $10,000.
  • For Salesman-2, hours worked are 41 and total sales are $12,000, so the result is "True" because both conditions are met, worked more than 40 hours and had total sales above $10,000.
  • For Salesman-3, hours worked are 45 and total sales are $9,000, so the result is False because both conditions are not met, worked more than 40 hours but total sales is not above $10,000.
  • For Salesman-4, hours worked are 38 and total sales are $10,000, so the result is False because both conditions are not met. worked less than 40 hours and total sales is not above $10,000.
  • For Salesman-5, hours worked are 52 and total sales are $16,000, so the result is "True""True" because both conditions are met. worked more than 40 hours and had total sales above $10,000.

OR Function

The OR function is similar to AND, but it only requires one of the conditions to be true to return TRUE. If all conditions are false, it will return FALSE.

Syntax:

=OR(condition1, condition2, ...)

Use Case:

Scenario: You want to track whether any sales rep has reached their quarterly target in either Q1 or Q2. If they hit their target ($10,000) in either period, return TRUE, meaning the target is met for at least one quarter.

=OR(B2 > 10000, C2 > 10000)

OR Function

Explanation: The formula checks the value in the "Sales - Q1" and "Sales - Q2" columns:

  • For Salesman-1, Q1 and Q2 sales are not above $10,000, so the result is False, none of the conditions met.
  • For Salesman-2, Q1 sales are above $10,000 and Q2 sales are not above $10,000, so the result is True because one condition met.
  • For Salesman-3, Q1 sales are not above $10,000 but Q2 sales are above $10,000, so the result is True because one condition met.
  • For Salesman-4, Q1 and Q2 sales are exactly $10,000, the test is for above $10,000, so the result is False none of the conditions met.

4. IFERROR Function

The IFERROR function is helpful when you expect that an error might occur, such as when dividing by zero or referencing a non-existent value. IFERROR will return a specified value when an error is encountered, instead of showing the error message.

Syntax:

=IFERROR(value, value_if_error)

Use Case:

Scenario: You are calculating the average sales per representative, but some rows have no data, which could result in a #DIV/0! error. You want to return “No Data” instead of showing the error.

=IFERROR(A2/B2, "No Data")

If there’s an error in the division (e.g., B2 is zero), the formula will return "No Data" instead of showing the error.


Using IF and AND combined

Let's take the example of one of the scenarios explained above, "Evaluation of employees", how about instead of having the result as True or False get the results as Eligible for Bonus or Not Eligible, this can be acheived using IF combined with the Operator

Syntax:

=IF(AND(Logic1,Logic2),"Value if True","Value if False")

Applying it on evaluating employees for a bonus. The employee will only receive the bonus if they have worked more than 40 hours AND achieved sales over $10,000 in sales.

=IF(AND(B2>40,C2>10000),"Eligible for Bonus","Not Eligible")

IF AND Function

So now instead of True or False the results are more readable and understandable mentioning clearly Eligible for Bonus or Not Eligible.

Using IF and OR combined

Similar to above, IF combined with OR results in more readable results. lets apply it on the use case scenario explained above, to track whether any sales rep has reached their quarterly target in either Q1 or Q2, but instead of having results as True or False, will have the results as Target Acheived or Not Acheived.

Syntax:

=IF(OR(Logic1,Logic2),"Value if True","Value if False")

Applying it on our worksheet:

=IF(OR(B2>10000,C2>10000),"Target Achieved","Not Ahieved")

IF OR Function

Now that you have good understandig of the basics of IF, OR, AND, IFERROR lets create a more interesting formula.. or you can also visit Basics of MS Excel to get the basics.


Nested Functions

Nested functions allow you to use multiple functions inside each other to perform more complex tasks. This is particularly useful when you need multiple conditions to be met before performing a calculation or returning a result.

Scenario: You want to assign Grades to students based on their score. If the score is greater than 90, the student gets an "A", otherwise, if it’s greater than 70 but less than or equal to 90, they get a "B", if it’s greater than 50 but less than or equal to 70, they get a "C" , or else the result will be Fail.

=IF(B2>90, "A", IF(B2>70, "B", IF(B2>50, "C","Fail")))

IF OR Function

IF with AND & OR for Complex Decision Making

By nesting IF with AND and OR functions, you can create advanced logical tests that mirror real-life decision-making.

Use Case:

Scenario: A company evaluates employee bonuses based on Sales and Customer Satisfaction. The bonus is given if:

  • Sales are greater than $50,000 AND customer satisfaction is above 90%.
  • OR if sales are greater than $70,000, regardless of satisfaction.
  • If either of these conditions is met, return "Bonus"; otherwise, return "No Bonus".

Example:

=IF(OR(AND(B2 > 50000, C2 > 90), B2 > 70000), "Bonus", "No Bonus")

IF AND OR Function Combined
  • The AND condition checks if both Sales (B2) are greater than $50,000 and Customer Satisfaction (C2) is above 90%.
  • The OR condition checks if either the AND condition is true, or if sales are above $70,000.
  • If either condition is met, the employee gets a "Bonus", otherwise, "No Bonus".

Summary:

  • IF function is used to make basic decisions based on a condition.
  • AND function helps to check multiple conditions, all of which must be true.
  • OR function checks multiple conditions, any one of which must be true.
  • IFERROR handles errors gracefully and lets you return custom values in case of errors.
  • Nested IF with AND/OR enables you to solve complex decision-making problems by combining multiple conditions.

Practical Tip:

When dealing with complex nested formulas, it’s helpful to break the formula into smaller pieces and test each part to ensure correctness. This step-by-step breakdown can help ensure that the logic works as intended, particularly when working with multiple layers of nested functions. Mastering this foundational concept will give you the tools to handle even the most complex decision-making tasks in Excel, enabling you to solve real-world problems with confidence and precision!
Remember Practice makes you better the only way to perfection is Practice more and more, if you encounter errors it is totally fine because with each error you will learn something new!


Hands-On Assignment: Test Your Skills

Download Assignment

Objective:Calculate Employee Salaries Using IF Conditions

Instructions

  1. Calculate the Basic Salary
  2. Use the IF function to assign the Basic Salary based on each employee's grade by referring the Salary Criteria shown in the image below and available in the assignment file

  3. Calculate Allowances
  4. For each allowance (House, Medical, Travel), use the IF function to determine the percentage based on the employee's grade, then calculate the amount by applying this percentage to the Basic Salary.

  5. Calculate Total Salary
  6. In the Total Salary column, calculate the total by adding the Basic Salary, House Allowance, Medical Allowance, and Travel Allowance.

  7. Dynamic Update Requirement
  8. Ensure that if the grade or salary criteria are updated, the calculations automatically reflect the changes. This will verify if your formulas are correctly referencing the appropriate cells and ranges.

    Salary Criteria

    Submit Your Completed Assignment

    Please save your completed assignment file, and email it to: assignments@muamlati.com