Find a course

Module 3: Logic, Lookups and Clean DataLesson 6 of 1212 min

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.

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:

OperatorMeaningExample
=Equal toB2="Paid"
<>Not equal toB2<>"Paid"
> / <Greater / less thanC2<20
>= / <=Greater or equal / less or equalD2>=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.

How IF decides: the test C2 less than 20 is checked; if TRUE the cell shows Reorder, if FALSE it shows OKIs C2 < 20?logical_testTRUEFALSE"Reorder"value_if_true"OK"value_if_false=IF(C2<20, "Reorder", "OK")

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

AspectAND(test1, test2)OR(test1, test2)
Both tests trueTRUETRUE
Only test1 trueFALSETRUE
Only test2 trueFALSETRUE
Both tests falseFALSEFALSE
Example from the lessonFree delivery: at least $50 AND local zonePriority 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.

Café sales log: three Coffee rows are highlighted; COUNTIF of Coffee returns 3 and SUMIF of Coffee adds 4, 5 and 4.50 to give 13.50AB123456ItemAmountCoffee4.00Tea3.00Coffee5.00Muffin3.50Coffee4.50=COUNTIF(A2:A6, "Coffee")→ 3 matching rows=SUMIF(A2:A6, "Coffee", B2:B6)→ 4 + 5 + 4.50 = 13.50The condition picks rows; SUMIF then adds the matching amounts

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).

  1. Which label should this one row get?
  2. How many orders are marked "Late"?
  3. What is the total of all orders from Texas?
  4. 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, ).