Spreadsheet functions
How to use logic functions in Sheets
Please do not copy without permission. © ALX 2024.
“Logic is not a body of doctrine, but a
mirror-image of the world. Logic is
transcendental.”
– Ludwig Wittgenstein
2
Spreadsheet functions
Data overview
| To investigate how we can use spreadsheet functions in data analytics, we will use two
datasets: one on food crops and the other on their subsidies.
1. Average retail market prices of
selected food crops, 2014-2018 Dataset 1
A Kenyan dataset containing prices for
selected food crops during the months of
March and September for the years 2014-2018.
2. Food crops subsidies 2014-2018
A dataset indicating the subsidies the Kenyan
government provided for all food crops during
the months of March and September for the Dataset 2
years 2014-2018.
3
Spreadsheet functions
The IF and IFS functions
|
IF is used to return one value if a logical expression evaluates to TRUE and another if it evaluates
to FALSE. IFS evaluates multiple conditions and returns a value that corresponds to the first TRUE
condition.
=IF(logical_expression, value_if_true, value_if_false)
=IFS(condition1, value1, [condition2, value2, …])
● logical_expression – An expression or cell ● condition1 – The first condition to be
reference containing an expression that evaluated. This can be a boolean, a number, or
represents some logical value (TRUE or FALSE). a reference to any of those.
● value_if_true – The value the function ● value1 – The returned value if condition1 is
returns if logical_expression is TRUE. TRUE.
● value_if_false – [OPTIONAL] The value the ● [condition2, value2, …] – Additional
function returns if logical_expression is conditions and values if the previous ones are
FALSE. evaluated as FALSE.
4
Spreadsheet functions
The IF and IFS functions
01.
Example use:
01. For March 2014 prices less than KSh 50, Start
categorize as “cheap”. Otherwise, categorize
as “expensive.”
Our task is to separate our list of food crops into two
categories based on their prices. Food crops that False True
Price
are less than KSh 50 we will tag as “cheap”, while < 50
those equal to or more than KSh 50 will be tagged
as “expensive”.
“Expensive” “Cheap”
01.
If price < 50
- Output = “Cheap”
Else
- Output = “Expensive”
5
Spreadsheet functions
The IF and IFS functions
02.
Example use:
Start
02. For March 2014 prices less than KSh 50,
categorize as “cheap”, more than KSh 70,
categorize as “expensive” and the rest,
True
categorize as “affordable”. Price
“Cheap”
< 50
Our second task has three categories, which means
False
that we will have to expand the pseudocode for our If
statement by adding an Else if.
02. True
Price
“Expensive”
If price < 50 > 70
- Output = “Cheap”
Else if price > 70 False
- Output = “Expensive”
Else
- Output = “Affordable” “Affordable”
6
Spreadsheet functions
The IF and IFS functions
Example use:
01. Since the first task has only two conditions
(“cheap” or “expensive”), we will use an IF
01. 02.
function which evaluates Boolean expressions.
02. The second task has more than two
conditions (“cheap”, “affordable”, or
“expensive”) and will therefore be better
evaluated with an IFS function.
01. Enter the formula =IF(B3<50, "Cheap",
"Expensive") on cell D3.
02. Enter the formula =IFS(B3<50, "Cheap",
B3>70, "Expensive",B3<70,
"Affordable") on cell E3.
7
Spreadsheet functions
The alternative IF functions
| IFERROR and IFNA functions are used to catch and handle errors in a formula.
=IFERROR(value, [value_if_error]) IFERROR function returns the first argument
=IFNA(value, value_if_na) if it is not an error value; otherwise, it returns
the second argument if present or a blank if
● value – The value to return if value itself is not
an error (IFERROR) or the value to check if it is the second argument is absent.
an #N/A error (IFNA).
● [value_if_error] – [OPTIONAL] The value IFNA function evaluates a value and returns
returned if value is an error. the specified value if the value is an #N/A
● value_if_na – The value to return if value is error.
an #N/A error.
8
Spreadsheet functions
The alternative IF functions
Example use:
01. Detect price values that have an error. Start
If value = ERROR
- Output = “Error detected”
Else
False True
- Return value value =
ERROR
“Error
Return value
detected”
9
Spreadsheet functions
The alternative IF functions
Example use:
02. Detect price values that have values that are Start
not applicable.
If value = NA
- Output = “Not applicable”
Else False True
- Return value value = NA
“Not
Return value
applicable”
10
Spreadsheet functions
The alternative IF functions
01.
Example use:
01. IFERROR can detect any type of error and will
01.
therefore detect #N/A errors among other
errors.
02. IFNA, however, only detects #N/A errors and
will not pick up on any other types of errors.
01. Enter the formula =IFERROR(B3:C10,
"Error detected.") in cell D3 and press 02.
ENTER.
02. Enter the formula =IFNA(B3:C10, "Not 02.
applicable.") in cell D3 and press ENTER.
11
Spreadsheet functions
The alternative IF functions
| SUMIF is used to add up the value of cells within a range that meet a certain condition
while SUMIFS adds up the value of cells within a range that meet multiple conditions.
=SUMIF(range, criterion, [sum_range])
=SUMIFS(sum_range,criteria_range1,criterion1,[criteria_range2,criterion2,...])
● range – The range that is tested against the ● criteria_range1 – The range to check against
criterion. criterion1.
● criterion – The pattern or test to apply to ● criterion1 – The pattern or test to apply to
range. criteria_range1.
● sum_range – [OPTIONAL for SUMIF if different ● criteria_range2, criterion2, ... – [OPTIONAL]
from range] The range to be summed. Additional ranges and criteria to check.
12
Spreadsheet functions
The alternative IF functions
Example use:
Start
01. Sum up the prices of all food crops that were
more than KSh 50 in March 2014. Input
{p1,...,pn}
Input {p1,...,pn}
sum = 0
sum = 0
i = 0
i = 0
False True
While i < n do
i < n i = i + 1
- i = i + 1
- If pi > 50
False
- - sum = sum + pi pi>50
True
Let the set of prices in March 2014 be represented End
by {p1,...,pn} where n is the number of prices in sum = sum + pi
the list. 13
Spreadsheet functions
The alternative IF functions
Example use: Start
02. Sum up the prices of all food crops that were Input
more than KSh 50 but less than KSh 70 in {p1,...,pn}
March 2014.
sum = 0
Input {p1,...,pn}
i = 0
sum = 0
i = 0
False True
i < n i = i + 1
While i < n do
- i = i + 1 False
- If pi > 50 pi>50
- - If pi < 70 True
- - - sum = sum + pi False
End pi<70
Let the set of prices in March 2014 be represented True
sum = sum + pi
by {p1,...,pn} where n is the number of prices in
the list. 14
Spreadsheet functions
The alternative IF functions
01.
Example use: 02.
Our SUMIF’s range will be the same as the
sum_range and, therefore, sum_range will not be
01.
included in our function.
SUMIFS will use the same range of cells for both 02.
criteria_range1 and criteria_range2 as well as
sum_range.
01. Enter the formula =SUMIF(B3:B10, ">50")
in cell E3 and press ENTER.
02. Enter the formula
=SUMIFS(B3:B10,B3:B10,">50",B3:B10,
"<70") in cell E6 and press ENTER.
15
Spreadsheet functions
The alternative IF functions
| COUNTIF function counts cells in a range that meet a single condition while COUNTIFS
function counts cells in a range that meet multiple conditions.
=COUNTIF(range, criterion)
=COUNTIFS(criteria_range1,criterion1,[criteria_range2,…],[criterion2,…])
● range – The range that is tested against ● criteria_range1 – The range to check against
criterion. criterion1.
● criterion – The pattern or test to apply to ● criterion1 – The pattern or test to apply to
range. criteria_range1.
● criteria_range2, criterion2... –
[OPTIONAL] Additional ranges and criteria to
check.
16
Spreadsheet functions
The alternative IF functions
Example use:
Start
01. Count the number of cells containing food
crop prices that were more than KSh 50 in Input
{p1,...,pn}
March 2014.
Input {p1,...,pn} count = 0
count = 0 i = 0
i = 0
False True
While i < n do i < n i = i + 1
- i = i + 1
- If pi > 50
False
- - count = count + 1 pi>50
True
End
We see that our logic remains the same as per our
count = count+1
sum example.
17
Spreadsheet functions
The alternative IF functions
Example use: Start
02. Count the number of cells containing food Input
crop prices that were more than KSh 50 but {p1,...,pn}
less than KSh 70 in March 2014.
count = 0
i = 0
Input {p1,...,pn}
count = 0
False True
i = 0 i < n i = i + 1
While i < n do False
pi>50
- i = i + 1
- If pi > 50 True
False
- - If pi < 70 End pi<70
- - - count = count + 1 True
count = count + 1
18
Spreadsheet functions
The alternative IF functions
01.
Example use:
02.
The range on COUNTIF as well as criteria_range1
and criteria_range2 on COUNTIFS will be the
column that contains prices for the month of 01.
March.
02.
Our statements will be exclusive of the given price
boundaries since we want to count values that are
more than KSh 50 for the first problem and more
than KSh 50 and less than KSh 70 for the second
problem.
01. Enter the formula =COUNTIF(B3:B10,
">50") in cell E3 and press ENTER.
02. Enter the formula
=COUNTIFS(B3:B10,">50",B3:B10,"<70")
in cell E6 and press ENTER.
19
Spreadsheet functions
The SWITCH function
|
SWITCH evaluates a logical expression and matches it with one of the cases available. It then
returns the value defined in that case block. If there is no matching case, it returns the default
value.
=SWITCH(expression, case1, value1, [case2_or_default, …], [value2, …])
● expression – The value(s) to be evaluated. ● case2_or_default… – [OPTIONAL] Additional
● case1 – The first case to be checked against cases to try if the previous ones don't match
expression. expression, or an optional default value to be
● value1 – The corresponding value to be returned if none of the cases match expression.
returned when case1 matches expression. ● value2… – [OPTIONAL] Additional values to be
returned if their corresponding cases match
expression.
20
Spreadsheet functions
The SWITCH function
.Example use:. Since Grain has the most number of crops, we will
make it the default case.
Let’s say we want to categorize the food crops. We
will list cases with the least number of items first SWITCH crop name
and let the default have the most number of items. - CASE “Beans”
- - Output = “Legume”
- CASE “Cabbages”
Based on our list of food crops, our categories are - - Output = “Vegetable”
Legume, Vegetable, Fruit, and Grain. - CASE “Potatoes”
- - Output = “Vegetable”
1. Legume has one crop: beans.
- CASE “Tomatoes”
2. Vegetable has two crops: cabbages and
- - Output = “Fruit”
potatoes.
- CASE “Bananas”
3. Fruit has two crops: tomatoes and bananas.
- - Output = “Fruit”
4. Grain has three crops: maize, sorghum, and
- DEFAULT
finger millet.
- - Output = “Grain”
21
Spreadsheet functions
The SWITCH function
.Example use:.
SWITCH crop
Start
name
True CASE
“Beans”
“Legume” False
False False False False
CASE CASE CASE CASE
“Cabbages” “Potatoes” “Tomatoes” DEFAULT
“Bananas”
True True True True
“Vegetable” “Vegetable” “Fruit” “Fruit” “Grain”
End
22
Spreadsheet functions
The SWITCH function
.Example use:.
The order in which we list our cases will determine
how long our SWITCH statement will be.
01. Enter the formula =SWITCH(A4:A11,
"Beans", "Legume", "Cabbages",
"Vegetable", "Potatoes",
"Vegetable","Tomatoes", "Fruit",
"Bananas", "Fruit", "Grain") in cell
D4.
02. Press Enter.
Try rewriting the statement below with “Tubers” as
your default to see the length difference.
23
Spreadsheet functions
SWITCH versus IFS
Both SWITCH and IFS are logical functions that IFS is often more flexible than SWITCH because it
allow you to perform different actions based on can handle a broader range of conditions and
multiple conditions. However, there are some outcomes.
scenarios where you may prefer to use SWITCH over
IFS, or vice versa. IFS is more flexible than SWITCH because:
1. It allows you to evaluate multiple conditions,
SWITCH is preferred when: whereas SWITCH only allows you to match a
single value with multiple outcomes. This
1. You have a single value or cell that you want means that if you have multiple conditions
to match with multiple possible outcomes. that you need to evaluate, you can use IFS to
2. You want to avoid evaluating unnecessary handle all of them.
conditions. With IFS, all conditions are 2. It allows you to specify different outcomes
evaluated, even if one of the earlier based on the conditions that are met. This
conditions is true. With SWITCH, only the means that you can use formulas to
condition that matches the specified value is calculate outcomes based on the data in
evaluated, which saves processing time. your sheet.
24
Spreadsheet functions
Other logic functions
|
The AND function returns TRUE if all of the provided arguments are logically true and FALSE if any of
the provided arguments are logically false, while the NOT function returns the opposite of a logical
value.
=AND(logical_expression1,(logical_expression2, ...])
=NOT(logical_expression)
● logical_expression/logical_expression1 – Examples:
An expression that represents some logical ● =AND(TRUE,FALSE,TRUE) returns FALSE
value, i.e. TRUE or FALSE, or an expression that ● =AND(FALSE,FALSE,FALSE) returns FALSE
can be coerced to a logical value. ● =AND(TRUE,TRUE,TRUE) returns TRUE
● logical_expression2… – [OPTIONAL] More ● =NOT(TRUE) returns FALSE
expressions that represent logical values. ● =NOT(FALSE) returns TRUE
Note: The number 0 is logically False; all other numbers (including negative numbers) are logically True.
25
Spreadsheet functions
Other logic functions
|
The OR function returns TRUE if any argument is logically true, and FALSE if all arguments are
logically false, while XOR returns TRUE if an odd number of arguments are logically true, and
FALSE otherwise.
=OR(logical_expression1, [logical_expression2, …])
=XOR(logical_expression1, [logical_expression2, …])
● logical_expression1 – An expression that Examples:
represents some logical value, i.e., TRUE or ● =OR(TRUE,FALSE,TRUE) returns TRUE
FALSE, or an expression that can be coerced to ● =OR(FALSE,FALSE,FALSE) returns FALSE
a logical value. ● =OR(TRUE,TRUE,TRUE) returns TRUE
● logical_expression2… – [OPTIONAL] More ● =XOR(TRUE, FALSE, TRUE) returns FALSE
expressions that represent logical values. ● =XOR(FALSE, FALSE, TRUE) returns TRUE
26
Spreadsheet functions
Other logic functions
Example use: Since we have not been told what to do with crops
that don’t fall in the category of dropping or rising
For 2014, tag all food crops as either rising price price trend, we will create a third label called
trend if the price in September is higher than that unidentified trend to cater for this.
in March or dropping price trend if the price in
September is lower than the price in March. Let original retail price in Sept be OSP and in
March, OMP. Let the subsidized retail price in Sept
be SSP and in March SMP. Also, let all original prices
For this problem, we need to identify a trend for
be OP and all retail prices be RP.
each food crop from March to September based on
whether a crop’s price went up or down.
SWITCH OP AND RP
We also have to ensure that the trend is true for - CASE(OSP < OMP AND SSP < SMP)
both the original retail prices and subsidised retail - - Output =“Dropping price trend”
prices. - CASE(OSP > OMP AND SSP > SMP)
- - Output = “Rising price trend”
- DEFAULT
- - Output = “Unidentified trend”
27
Spreadsheet functions
Other logic functions
Example use: ● OP: Original Prices
● SP: Subsidised Prices
● OSP: Original September Price
Start ● OMP: Original March Price
● SSP: Subsidised September Price
● SMP: Subsidised March Price
False False
CASE (OSP<OMP CASE (OSP>OMP
SWITCH OP AND RP DEFAULT
AND SSP<SMP) AND SSP>SMP)
True True
“Dropping price “Rising price “Unidentified
trend” trend” trend”
End
28
Spreadsheet functions
Other logic functions
Example use: 01. Enter the formula
=SWITCH(AND(B3:C10,F3:G10),
Our SWITCH function will evaluate prices in both the AND(C3<B3, G3<F3), "Dropping price
original and subsidized retail prices, so the trend", AND(C3>B3, G3>F3), "Rising
expression will be an AND function. Our first two price trend", "Unidentified price
cases will check for conditions using the original trend") on cell J3.
and subsidized tables and will also be AND
02. Replicate the formula by dragging the fill
functions. Our default case will be crops that didn’t
handle down.
fit into either of the first two cases.
01.
02.
29