1. Lighthouse

Level 1 · Count what's there with COUNTA, COUNTBLANK and COUNTIF

The request

From George King, Sales Associate

“I'm prepping a call with our business accounts about their December 2–4 orders. Before I talk numbers I want to know what's in this export: how many lines, how many actual orders, how many different products, how many lines had no coupon, how many were bulk, and how many were just one or two units.”

Your goal

First flag first appearances: in E2:E55 put 1 when the order id in column A appears for the first time (counting from A2 down to this row), else 0; in F2:F55 the same for the product in column B. Then fill I3:I8: I3 the number of lines (COUNTA on B2:B55), I4 the number of orders (sum of E), I5 the number of different products (sum of F), I6 the lines with no coupon code in D2:D55 (COUNTBLANK), I7 the lines whose quantity (C2:C55) is greater than the number in I1 (COUNTIF, criterion built from I1), I8 the lines with a quantity of 1 or 2.

  • Fill E2:E55, F2:F55, I3:I8 with the right valuesnot done yet
  • With formulas, not typed numbersnot done yet
  • Use COUNTA COUNTBLANK COUNTIFnot done yet

Your sheet

Excel
The sheet for this level
ABCDEFGHIJ
1order_idproductquantitycoupon_codenew_ordernew_productBulk means more than5
212295Birch Brass Floor Mirror3
312295Oakley Brass Framed Print1Order lines
412300Cove Wool Lumbar Pillow2Orders
512300Cove Cast Iron Dutch Oven4Different products
612301Monroe Stoneware Utensil Set3Lines without a coupon
712301Hazel Linen Throw Pillow3Bulk lines
812301Oakley Jute Knit Throw6Lines of 1 or 2 units
912301Avery Leather Bed Frame2
1012303Vale Enamel Bowl Set2
1112305Delancey Brass Vase2
1212305Birch Brass Floor Mirror2
1312313Delancey Black Metal Wall Sconce4
1412313Hazel Porcelain Serving Platter1
1512313Thatcher Marble Candle Holder Set9
1612314Birch Brass Floor Mirror3
1712314Fairhaven Stoneware Cutting Board1
1812314Cove Cast Iron Dutch Oven2
1912314Delancey Brass Vase1
2012316Nantucket Brass Wall Mirror3
2112316Hazel Linen Throw Pillow1
2212317Glen Stoneware Carafe2HOLIDAY20
2312317Delancey Brass Vase2HOLIDAY20
2412322Delancey Black Metal Wall Sconce1
2512322Monroe Glass Desk Lamp12
2612322Vale Enamel Bowl Set4
2712325Ridge Brass Bud Vase3
2812325Oakley Jute Knit Throw8
2912325Vale Enamel Bowl Set3
3012328Maple Wool Throw Pillow4
3112328Thatcher Oak Candle3
3212328Hazel Stainless Dutch Oven2
3312328Harbor Wool Knit Throw8
3412332Ridge Ceramic Chandelier2HOLIDAY20
3512332Cove Wool Lumbar Pillow1HOLIDAY20
3612332Hazel Porcelain Serving Platter4HOLIDAY20
3712341Thatcher Oak Candle4
3812341Pacifica Black Metal Table Lamp1
3912346Dune Ceramic Arc Lamp2
4012346Spruce Wool Duvet Cover1
4112346Elm Brass Floor Mirror1
4212347Northfield Brass Bud Vase2
4312347Cove Wool Lumbar Pillow2
4412349Delancey Jute Area Rug2
4512349Thatcher Oak Candle1
4612349Glen Cotton Runner Rug2
4712353Hazel Linen Throw Pillow4
4812353Pacifica Black Metal Arc Lamp2
4912360Fairhaven Brass Wall Sconce4
5012360Alder Oak Coffee Table1
5112360Elm Brass Floor Mirror4
5212360Bramble Percale Quilt1
5312361Vale Enamel Bowl Set4
5412361Fern Oak Candle Holder Set3
5512361Harbor Oak Floor Mirror6
56
57

Loading the editor…

    ResultPress Check to compare E2:E55, F2:F55, I3:I8

    Mote checks your result

    Press Check. Each right cell becomes a firefly in your lantern.

    The lesson

    Before you add anything up, find out what the data holds. How many rows are there? How many of them are real transactions? What’s missing? Excel has a small family of counting functions for this, and each one counts something different.

    COUNT, COUNTA and COUNTBLANK

    Function Counts On this sheet
    =COUNT(C2:C55) cells holding a number 54 quantities
    =COUNT(B2:B55) (the product names are text) 0
    =COUNTA(B2:B55) cells that are not empty: text, numbers, anything 54
    =COUNTBLANK(D2:D55) empty cells 49

    COUNT only counts numbers, so on a text column it returns 0. That is the classic slip when someone asks “how many rows?” and gets nothing back. For “how many rows”, use COUNTA on a column that is always filled, like the product name or an id.

    COUNTA and COUNTBLANK are two halves of the same range. The coupon column has 5 filled cells (HOLIDAY20 on two orders) and 49 empty ones, and 5 + 49 = 54. When the two halves don’t add up to the row count, the range is the wrong size.

    Lines are not orders

    The sheet has one row per order line: order 12301 bought four products, so it takes four rows. George asked for both numbers, and they are different.

    To count distinct orders in a sheet like this, flag the first time each order id appears. In E2:

    E2   =IF(COUNTIF($A$2:A2,A2)=1,1,0)

    The range $A$2:A2 has a locked start and a moving end. Filled down, row 5 reads $A$2:A5, row 9 reads $A$2:A9: it always runs from the top to the current row. So the COUNTIF asks “how many times has this order id appeared so far?” It is 1 only on the order’s first line, so the flag is 1 there and 0 on every repeat. Add up the column, =SUM(E2:E55), and you get the number of orders: 20. The same flag on column B counts 36 different products.

    At your desk you may see two other ways. Microsoft 365 has =COUNTA(UNIQUE(A2:A55)), and older workbooks use =SUMPRODUCT(1/COUNTIF(A2:A55,A2:A55)). Both make Excel work on a whole list of values inside one cell. This practice sheet keeps one value per cell, so they don’t run here; the flag column gives the same answer and works in every version. Its bonus: you can see which rows counted, and filter on them.

    Counting with a condition

    COUNTIF counts the cells that pass one test. To count bulk lines, more than the 5 units in I1:

    I7   =COUNTIF(C2:C55,">"&I1)

    The operator is in quotes and the cell is joined with &, so the criterion becomes >5. Six lines pass: quantities of 6, 6, 8, 8, 9 and 12. The next levels take COUNTIF much further.

    For “1 or 2 units”, one COUNTIF can’t test two values, but two of them added together can:

    I8   =COUNTIF(C2:C55,1)+COUNTIF(C2:C55,2)

    That’s 13 single-unit lines and 16 lines of two units: 29. A number criterion needs no quotes. Because quantities are whole numbers, =COUNTIFS(C2:C55,">=1",C2:C55,"<=2") gives the same 29.

    Sanity-check the counts

    • The quantity bands should add up: 29 small lines, 19 lines of 3 to 5 units and 6 bulk lines make 54.
    • COUNT(C2:C55) should equal COUNTA(C2:C55). If it’s lower, some quantities were imported as text, and every sum of that column is too low.
    • A distinct count can never be higher than the line count.

    Common mistakes

    • COUNT on text. =COUNT(B2:B55) returns 0. Use COUNTA.
    • Reporting lines as orders. Count distinct order ids, not rows.
    • An unlocked start in the flag. COUNTIF(A2:A2,A2) slides down as one cell, so every row counts 1 and the “orders” come out as 54.
    • ">I1" in quotes. The criterion becomes the text “I1”; join the cell with &.
    • “1 or 2” in one criterion. "1,2" matches nothing. Add two COUNTIFs.

    Pick what may run on this site. You can change this any time from "Cookie settings" at the bottom of every page.