IF, COUNTIF and SUMIF
Write logical tests, use IF to label and calculate, combine conditions with AND and OR, and count or total only the rows that match a condition.
Welcome back!
Step 1 of 11Learn
Let the sheet decide
Every Friday, Carla scrolls through her store's inventory list and marks items that need reordering: anything with fewer than 20 units in stock. With 300 products, it takes an hour, and she misses a few every week.
A formula can make that decision for her:
=IF(C2<20, "Reorder", "OK")
IF checks a condition and returns one value if it's true and another if it's false:
=IF(logical_test, value_if_true, value_if_false)
The logical test is a comparison that is either TRUE or FALSE. Sheets uses these comparison operators:
| Operator | Meaning | Example |
|---|---|---|
| = | Equal to | B2="Paid" |
| <> | Not equal to | B2<>"Paid" |
| > / < | Greater / less than | C2<20 |
| >= / <= | Greater or equal / less or equal | D2>=1000 |
Copy Carla's formula down 300 rows and every product is labeled in a second. When stock changes, the labels update by themselves.
The results don't have to be text. IF can calculate: =IF(D2>=1000, D2*0.05, 0) pays a 5% bonus on sales of $1,000 or more and nothing otherwise. Text values go in double quotes; numbers and formulas don't.
Step 2 of 11Practice · Choose one
Cell B2 contains 65. What does =IF(B2>=70, "Pass", "Retake") return?
Step 3 of 11Learn
AND, OR and nested IFs
Real decisions often depend on more than one thing. Two helper functions combine tests:
- AND(test1, test2, …) is TRUE only if all tests are true.
- OR(test1, test2, …) is TRUE if at least one test is true.
A delivery is free if the order is at least $50 and the customer is in the local zone: =IF(AND(D2>=50, E2="Local"), 0, 7.99)
A customer gets priority support if they're a Premium member or have spent over $5,000: =IF(OR(F2="Premium", G2>5000), "Priority", "Standard")
For more than two outcomes, you can put an IF inside another IF. A grade scale:
=IF(B2>=90, "A", IF(B2>=80, "B", IF(B2>=70, "C", "Below C")))
Sheets checks from left to right and stops at the first true test, so the order matters: check the highest threshold first. If you started with B2>=70, a score of 95 would stop there and get a C.
Nested IFs get hard to read beyond three or four levels. Google Sheets offers IFS, which lists test-result pairs in order: =IFS(B2>=90, "A", B2>=80, "B", B2>=70, "C", TRUE, "Below C"). The final TRUE acts as "everything else."
Step 4 of 11Compare
AND vs. OR in four cases
| Aspect | AND(test1, test2) | OR(test1, test2) |
|---|---|---|
| Both tests true | TRUE | TRUE |
| Only test1 true | FALSE | TRUE |
| Only test2 true | FALSE | TRUE |
| Both tests false | FALSE | FALSE |
| Example from the lesson | Free delivery: at least $50 AND local zone | Priority support: Premium OR spent over $5,000 |
Step 5 of 11Note
Step 6 of 11Practice · What would you do?
What would you do?
Free shipping applies when an order total (C2) is over $75 or the customer has a coupon (D2 = "Yes"). Which formula should you use?
Step 7 of 11Learn
COUNTIF and SUMIF: totals with a condition
IF works row by row. To summarize a whole column by condition, use the conditional aggregate functions.
COUNTIF(range, criterion) counts cells that meet a condition:
=COUNTIF(B2:B200, "Paid")counts how many invoices are marked Paid.=COUNTIF(C2:C200, ">1000")counts amounts over 1,000. Comparison criteria go in quotes.=COUNTIF(C2:C200, ">"&F1)uses a threshold stored in F1. Join the operator to the cell with &.
SUMIF(range, criterion, sum_range) adds the values in sum_range where range meets the condition:
=SUMIF(A2:A200, "East", C2:C200)totals sales in column C for rows where the region in column A is East.
For several conditions, use COUNTIFS and SUMIFS, which take pairs of ranges and criteria. Note that SUMIFS puts the sum range first:
=SUMIFS(C2:C200, A2:A200, "East", B2:B200, "Paid") totals paid sales in the East.
A worked example: a small café's sales log lists Item in column A and Amount in column B. With rows Coffee $4, Tea $3, Coffee $5, Muffin $3.50 and Coffee $4.50, then =COUNTIF(A2:A6, "Coffee") returns 3 and =SUMIF(A2:A6, "Coffee", B2:B6) returns $13.50.
Watch for text mismatches. COUNTIF and SUMIF ignore capitalization, so "east" matches "East," but "East " with a trailing space or the abbreviation "E" will not match and simply drops out of the total, with no warning. Consistent data entry, which you'll set up with dropdowns later, prevents this.
Step 8 of 11Practice · Work it out
Column A lists regions and column B lists sales: North 200, South 150, North 300, East 120, North 80. What does =SUMIF(A2:A6, "North", B2:B6) return?
Step 9 of 11Note
Step 10 of 11Practice · Match the pairs
Match each question with the function that answers it.
Tap an answer, then tap the place where it belongs (or drag it there).
- Which label should this one row get?
- How many orders are marked "Late"?
- What is the total of all orders from Texas?
- What is the total of paid orders from Texas in March?
Step 11 of 11Practice · Fill in the blanks
Fill in the blanks
To count how many cells in C2:C50 contain values greater than 500, write =COUNTIF(C2:C50, ).
Lesson complete!