1. Lighthouse

Level 7 · Days between dates with DATEDIF

The request

From Jason King, Logistics Coordinator

“Returns are accepted within 30 days of the order. I want to see how many days each of July's returns took to come back, and which ones came in after the window. The window length is in J1 in case we change it.”

Your goal

In F2:F35, return the number of whole days from the order date (column C) to the return date (column D) with DATEDIF. In G2:G35, return "outside" when that number is greater than the window in J1, otherwise "within".

  • Fill F2:G35 with the right valuesnot done yet
  • With formulas, not typed numbersnot done yet
  • Use DATEDIFnot done yet

Your sheet

Excel
The sheet for this level
ABCDEFGHIJK
1order_idcategoryorder_datereturned_onreasondays_to_returnwindow_checkReturn window (days)30
25371Outdoor2024-05-24 07:192024-07-01changed_mind
35480Outdoor2024-06-04 19:022024-07-01changed_mind
45544Outdoor2024-06-12 13:102024-07-01changed_mind
55560Kitchen2024-06-14 15:542024-07-02changed_mind
65657Furniture2024-06-26 09:102024-07-03damaged
75540Decor2024-06-11 21:572024-07-04changed_mind
85521Kitchen2024-06-09 13:382024-07-05changed_mind
95659Furniture2024-06-26 12:082024-07-05damaged
105487Kitchen2024-06-05 13:432024-07-06late
115682Textiles2024-06-27 22:032024-07-09wrong_item
125505Decor2024-06-07 12:542024-07-10late
135521Kitchen2024-06-09 13:382024-07-11late
145720Decor2024-07-02 09:402024-07-11changed_mind
155631Outdoor2024-06-22 18:312024-07-13late
165720Textiles2024-07-02 09:402024-07-13changed_mind
175768Textiles2024-07-09 01:462024-07-15changed_mind
185773Lighting2024-07-09 20:082024-07-15damaged
195505Textiles2024-06-07 12:542024-07-17changed_mind
205584Kitchen2024-06-17 10:422024-07-17late
215646Decor2024-06-24 20:422024-07-17changed_mind
225798Lighting2024-07-12 20:192024-07-17damaged
235777Decor2024-07-10 17:172024-07-18wrong_item
245756Lighting2024-07-07 19:442024-07-19damaged
255777Decor2024-07-10 17:172024-07-19wrong_item
265798Outdoor2024-07-12 20:192024-07-19wrong_item
275811Kitchen2024-07-14 18:392024-07-22wrong_item
285867Kitchen2024-07-21 10:462024-07-26wrong_item
295810Decor2024-07-14 17:412024-07-27changed_mind
305798Outdoor2024-07-12 20:192024-07-28changed_mind
315880Outdoor2024-07-22 10:492024-07-28wrong_item
325901Kitchen2024-07-24 14:432024-07-29wrong_item
335798Textiles2024-07-12 20:192024-07-30changed_mind
345851Outdoor2024-07-18 17:472024-07-30damaged
355905Textiles2024-07-24 20:482024-07-30changed_mind
36
37

Loading the editor…

    ResultPress Check to compare F2:G35

    Mote checks your result

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

    The lesson

    Jason’s question is a classic operations metric: how long does a return take to come back, and which ones broke the policy? Because dates are numbers, the days between two dates is a subtraction. The catch is in the data: the order date has a time on it.

    Subtracting dates

    The order in row 2 was placed on 2024-05-24 at 07:19 (C2) and came back on 2024-07-01 (D2). Try:

    =D2-C2

    The result is 37.70, not 38. A date without a time sits at midnight. The order came in at 7:19 in the morning, so between the two lie 37 full days plus the 0.70 of a day that was left of May 24. Format the cell with no decimals and it shows 38, which looks right, while every formula built on it still works on 37.70. Ordered on the 24th, back on the 1st: your colleagues count that as 38 days, so drop the time first.

    One more surprise: Excel sometimes formats the result of a date subtraction as a date, and the cell shows something like 1900-02-06. The number underneath is fine; set the cell’s format to Number or General.

    DATEDIF counts whole days

    F2   =DATEDIF(C2,D2,"d")

    DATEDIF(start, end, unit) takes the earlier date first and ignores the time of day. The unit says what to count:

    Unit Counts C2 to D2
    "d" whole days 38
    "m" whole months 1
    "y" whole years 0

    "m" and "y" are what you would use for tenure or a customer’s age in months. Two other ways give the same 38 days: =D2-INT(C2) (INT cuts the time off the order date) and =DAYS(D2,C2) (the end date comes first there). Excel doesn’t list DATEDIF in its formula autocomplete, an old quirk, but it works in every version and in Google Sheets.

    If the start is later than the end, DATEDIF returns #NUM!. On real data that is a useful alarm: a return logged before its order is a data-entry error.

    Outside the window or within it

    The policy is “within 30 days”, and the window is in J1. A return on day 30 is within it; day 31 is not. So the test is >:

    G2   =IF(F2>$J$1,"outside","within")

    Fill both columns down to row 35. July has 34 returns in this sheet and 5 of them came back after the window. One sits exactly on the edge: row 20, a Kitchen return 30 days after its order, which is within. If you count 6 outside, check the operator.

    Row 20 is also a reminder to read the columns: its reason is late, which means the delivery arrived late, not that the return did. Name your own columns so nobody confuses the two.

    Other date parts you’ll meet

    Timestamps hold more than a date. =HOUR(C2) returns 7, the hour the order was placed, handy for “when do people shop” questions. =WEEKDAY(D2,2) returns 1 for Monday through 7 for Sunday (July 1, 2024 was a Monday), and =NETWORKDAYS(C2,D2) counts only Monday to Friday, 27 here, for policies stated in business days.

    Common mistakes

    • Plain subtraction on a timestamp. It gives fractional days. Use DATEDIF, or INT on the timestamp.
    • The dates in the wrong order. DATEDIF(D2,C2,"d") returns #NUM!; =C2-D2 returns a negative number.
    • >= instead of >. Day 30 is within a 30-day window; >= flags row 20 by mistake.
    • Typing 30 into the formula. Point at $J$1, so a policy change is one cell.

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