Functions (continued)
We continue our look at functions with logic.
Logical Functions
IF gives you alternative results depending on a condition (also SUMIF and COUNTIF)
AND allows you to check that several criteria are all true
OR checks whether any criteria are true
NOT changes true to false and vice-versa
The IF Function
The IF function lets you test whether something is true or not and take different action as
appropriate.
1. Open the profitloss.xlsx file, found in Canvas.
What would happen if, in 2014, there was an exceptional Costs bill of $35,000 (for new
equipment, for example)? Income is only $34,650, so the business would have a loss.
2. Type a new Costs figure of 35000 into cell E5 and press [Enter].
Note that the Profit Before Tax is correctly shown as negative. However, the Tax figure is also
negative. You don’t usually pay taxes on a loss, and the IRS doesn’t give you money for having
a loss either! To make the worksheet behave correctly you need to alter the formula in the
2014 Tax cell E7:
3. Move to cell E7 and press [Delete] to clear the cell.
4. On the Formula Bar, click on the [Insert Function] button and choose IF, then click
[OK].
Tip: Rather than clicking on the [Insert Function] button, you can alternatively type in the
name of a function, along with its opening bracket. This will also display the function palette
and help you with the arguments.
5. In the Function Arguments dialog box which appears set Logical Test to E6 >= 0 (and
press [Tab])
6. Set Value_if_true to E6*30% (press [Tab]).
7. Set Value_if_false to 0.
8. Finally, press [Enter] or click [OK].
The function calculation results translate to: if the contents of E6 (Profit Before Tax) are more
than or equal to (>=) 0, the tax paid is equal to 30% of Profit Before Tax, otherwise, the tax is
zero. This produces a zero tax figure when the Profit Before Tax is in fact a loss. We calculate
thusly because spreadsheet models, even if they behave correctly in most circumstances, may
break down under special conditions.
9. Copy the new formula from E7 into D7 to B7 by dragging the cell handle backwards.
Close the file without saving.
A bit more intricate, but also much more powerful are the final counting functions, COUNTIF
and COUNTIFS. COUNTIF works similar to the regular IF function.
10. Open the exceldata.xlsx file, found in Canvas.
11. Click on the ‘Accounts’ tab.
12. Click in cell E1 and type ‘Plus Tax’.
13. In cell E2, enter an IF function that translates to: if the content of C2 (Category) is
equal to “food”, the tax paid is equal to zero, otherwise, the tax is equal to 20% of the
Amount in D2. (make sure to include quotation marks around “food”)
14. Make sure that cell E2 totals the amount PLUS the tax.
15. Set the format of cell E2 to Accounting.
16. Autofill the formula in cell E2 to the rest of column E.
Next, let’s look at COUNTIF:
17. Move to cell C62 and type ‘Food’.
18. Move to D62 and type =countif( then press [Ctrl-A] to open the Function Arguments
dialog box.
19. Set the Range to C2:C61 and Criteria to "food" (make sure to include the quotation
marks - when counting words you must enclose the text in quotation marks)
20. Click [OK]
21. Format cell D62 to number with no decimal places. You don’t want Accounting on this
cell because it contains a count of how many fields in column C contain ‘food’.
There's an equivalent function for adding up cells (instead of counting) matching certain
criteria, namely, SUMIF:
23. Move to E62 and type =sumif( then press [Ctrl-A] (or click on [Insert Function]).
24. Set the Range: to C2:C61 and Criteria to C62.
25. Finally, set the Sum_range to D2:D61 then press [Enter] to complete the formula.
You now know that the total amount spent on food.
Now let’s look at multiple criteria by looking at COUNTIFS and SUMIFS.
26. Click in cell C63.
27. Type ‘Mike Food’ and press [Enter].
28. Go to cell D63.
22. Type =countifs( then press [Ctrl-A] to open the Function Arguments dialog box.
29. Set the Criteria_range1 to C2:C61.
30. Set the Criteria1 to “food”.
31. Set the Criteria_range2 to B2:B61.
32. Set the Criteria2 to “Mike”.
33. Click [OK].
34. Go to cell E63.
23. Type =sumifs( then press [Ctrl-A] to open the Function Arguments dialog box.
24. Note that on SUMIFS, the first selection is Sum_Range. Set yours to D2:D61.
35. Set the Criteria_range1 to C2:C61.
36. Set the Criteria1 to “food”.
37. Set the Criteria_range2 to B2:B61.
38. Set the Criteria2 to “Mike”.
39. Click [OK].
40. In row 64, complete the same steps to find the Food total for any of the other people in
Column B except Mike (i.e. repeat the steps you've just completed for any single
individual other than Mike).
AND, OR, NOT
There are 3 more logical functions to consider - AND, OR and NOT:
41. Click on cell F2.
42. Type =AND( then press [Ctrl-A].
43. Set Logical1 to B2="Liz" and Logical2 to C2="Food". Note that both criteria would
have to be true to give that result.
44. Press [Enter] or click [OK] to complete the formula then double click on the cell
handle to autofill down the column.
45. Click in cell G2.
46. Using the same criteria (B2 = “Liz” and C2 = “food”), this time use the OR function.
47. Press [Enter] to complete the formula then double click on the cell handle to autofill
down the column.
48. Click in cell H2.
49. Type the formula ‘=NOT(G2)’.
50. Press [Enter] to complete the formula then double click on the cell handle to autofill
down the column.
Click on the File tab, save the file to your computer, then answer the functions B assessment
based on your file and/or comprehension of the lecture.
For some other function tips, watch the lecture video.