Functions Making Decisions in Spreadsheets
Spreadsheets have functions that allow varying calculations to happen. For instance, in January, you are unlikely to want to calculate the heat index, but you may wish to calculate a wind chill. Most computer programming, including spreadsheets relies on something called a Boolean expression, named after a mathematician named George Boole. Technically, a Boolean expression is something that evaluates to either TRUE or FALSE. We might view it as an answer to a yes-or-no question, with no ambiguities. FactCheck.org views it as a judgement about a particular statement.
Generally, when involving decisions, computer programming involves separating the possibilities into two paths, and a question that can be asked to determine which path to take.
For instance, someone may say "I am going to go buy potato chips. If Walmart has bags over 72 ounces, I will buy them there. If not, I will go buy chips at Food Lion." There is a clear situation that will determine this person's course of action. Somehow they need to ascertain whether Walmart sells bags of chips over 72 ounces. He/she may do this by calling and asking, or going there and finding out themselves, or asking a friend who has been there recently. In any case, this person will then know whether to buy at Walmart or at Food Lion.
The question being asked must have a clear answer. For instance, in English, we might say "If it is supposed to rain today, I will take an umbrella." Often implied with this is that the person will NOT take an umbrella if the forecast calls for sun. The "question" in this case: "Is it supposed to rain today?" We can answer this question by looking online for a weather forecast. However, if the forecast calls for a 50% chance of rain, what do you do? It must be assumed that there is a threshhold at which the person says "Yes, it is supposed to rain today." In the computer, there is no room for uncertainty. It is either true or false.
So what are the questions we can ask about values in spreadsheets? The most important ones are equality and inequality; they are defined as you learned them in math class, although the limitations of the keyboard means we sometimes write them a little differently:
|Comparison||Math symbol||Spreadsheet operator|
|Less than or equal to||≤||<=|
|Greater than or equal to||≥||>=|
|Not equal to||≠||<>|
If you use one of the spreadsheet operators in a cell's expression, the spreadsheet will display the boolean result. For instance, if you put =A4>=7 in a cell, that cell would show TRUE whenever the value in cell A4 is a number 7 or greater, and FALSE otherwise.
In addition, there are a number of functions whose computed result is boolean. Some of the more important to us are:
ISNUMBER(...) This function evaluates as TRUE if the argument is a number, and FALSE otherwise. For instance, ISNUMBER(A1) is TRUE if cell A1 contains the number 14, say, and false if it contains the word "fourteen."
ISBLANK(...) This function evaluates as TRUE if its argument is a blank cell, and FALSE otherwise. Note that a cell is not considered truly "blank" if it contains anything; if you go to (say) cell C2 and press the spacebar, then a cell with the expression =ISBLANK(C2) would now say FALSE.
Conditional computation with IF
IF(...) The IF function tests a condition and returns one of two values, depending on the result of the test. The condition must evaluate to true or false. The IF function takes three arguments. Its general form is
If the condition is true, the function evaluates the first value. If the condition is false, it evaluates the second value.
Side note: The third argument may be omitted. If so, the statement returns FALSE if the condition is false.
A sample use of IF() is to prevent division by zero. Consider the expression
If cell A4 contains 0, the expression results in a error, because division by 0 is undefined. Instead, write
Now, if A4 does not contain 0, the division is done. If it is 0, the division is skipped, and the word Error is displayed.
Example: Consider the scenario of a vendor near an performance stage. Below are 4 upcoming shows with the number of tickets sold and the length (in minutes) of each show.
Past experience shows that if a show is longer than an hour, the vendor will sell an average of $5 of food per ticket sold, but if the show is not longer than an hour, the vendor will only sell an average of $3 of food per ticket. We can have the spreadsheet help us figure out how much income to predict.
In Cell C2, type = IF( B2 > 60, 5*A2, 3*A2) and fill down to cell C5. The result:
|1||Tickets||Show length||Anticipated income|
Example: Now suppose a spreadsheet contains a list of employees and their annual salaries. Employee names are in column A, last year's salaries in column C, and this year's salaries in column D. However, some of the employees are new, and their old salary fields are blank. Here is the data.
|1||Name||Old salary||New salary|
The problem is to calculate the percent of increase in salary for the continuing employees and put the results in column E. Leave the cell in column E blank if the employee is new.
To calculate the percent of increase,
- Find the increase by subtracting the old salary from the new: increase = new_salary – old_salary.
- Divide the difference by the old salary: increase / old_salary.
- Convert to a percent by multiplying by 100 .
For example, consider Ann Aaron's figures.
Proportion of increase = 1200 / 25000 = 0.048 ,
Percent of increase = 0.048 * 100 = 4.8 .
We can combine these into one line.
Using cell references, Ann's percent of increase is (D3-C3) / C3 * 100 . However, we should do this calculation only if cell C3 (the old salary) actually contains a number.
So we guard the calculation with an IF(). Put ISNUMBER(C3) as the condition, the expression for calculating the percent increase in the second argument, and "" in the third. If C3 contains a number, the calculation will be done, and the result displayed. Otherwise, a blank will be displayed. That is, put the following expression in cell E3.
Then click and drag this expression down column E. Here are the results.
|1||Name||Old salary||New salary||Pct increase|
More complicated questionsThere are times when the situation calls for a more complicated question. Some examples:
AND(...) The AND function allows us to see if all specified conditions are met. Consider the following sheet, where each row represents a coupon. Column A is the type of product, and Column B is the discount amount for the coupon.
OR(...) The OR function allows us to see if at least one condition is met. For instance, imagine a climate-controlled building, where each room is its own "zone" with its own system of thermostat and sensors. The policy is that the A/C will be on if the temperature is high or the humidity is high. The temperature threshhold is 72, while the humidity threshhold is 60.
Consider the following small spreadsheet indicating the information on 5 rooms in the building.
In cell D2, we can put IF( OR(B2>72, C2>60), "ON", "OFF") and fill down to D6. The result would be:
For rooms 100 and 205, neither condition is met, so the AC is off.
For rooms 105 and 200, one of the two conditions is met, so the AC is on.
For room 210, the temperature is above threshhold and the humidity level is also above threshhold. This is okay, because AT LEAST ONE is true.
Exercises 1 through 4 use the following block of a spreadsheet.
What is displayed by each function?
1. =IF(B3 <> 0, B3*6, "")
2. =IF(D3 <> 0, D3*6, "")
3. =AND( C3 > 10, G3 < 20)4. =OR( C3 > 10, G3 < 20)
5.Cell A1 contains a number. Write a expression for cell A2 that writes "positive" if the number in A1 is greater than 0 and "not positive" otherwise.
6.A number is in cell B1. Write an expression for cell B2 that puts half of B1's number into B2 if the number is greater than or equal to 0 and puts 0 there otherwise.
7.Cell A1 contains an employee's salary. Write an expression that will calculate the employee's parking fee as follows: For salaries less than $50,000, the parking fee is 1% of the salary. For salaries greater than or equal to $50,000, the fee is $500 plus 1.25% of the amount over $50,000.
8.Cell D5 contains a person's income. Write an expression that calculates a tax on the income, computed as follows. If the income exceeds $40,000, the tax is $800 plus 3% of the amount over $40,000. Otherwise, the tax is 2% of the income.
Credits and licensing
This article is by Robert P. Webber, Scott McElfresh, and Don Blaheta licensed under a Creative Commons BY-SA 3.0 license.
Version 2017-Jan-30 05:00