Skip to content
Start free

How do I compare sales to the same period last year?

Compare against the same stretch of last year with half-open intervals, so a half-finished year doesn't look like a collapse.

Free course. Your Maestro, an AI, .

Are we doing better than last year?

It's June 30, 2026, and Café Altura's management wants to know if we're doing better than last year. The quick answer adds up what was sold in 2026 and compares it with what was sold in 2025:

  • 2026 so far: 6,980,100 pesos.
  • 2025, full year: 12,984,830 pesos.
  • Change: -46.2%.

The arithmetic is right and the conclusion is wrong. 2026 has six months in it and 2025 has twelve. The fair comparison is the same stretch of the calendar: January to June 2025, which sold 6,468,030 pesos. Against that, 2026 is up 7.9%.

Shifting a date back a year or a month looks trivial, but it has edge cases. Make a prediction.

Two dates that have no exact match one period earlier. Write what you think each one gives and why.

Two dates that have no exact match one period earlier. Write what you think each one gives and why.

One month before March 31 and one year before February 29

intervalos.py
import duckdb

print(duckdb.sql("""
    SELECT
      (DATE '2026-03-31' - INTERVAL 1 MONTH)::DATE::VARCHAR AS un_mes_antes,
      (DATE '2024-02-29' - INTERVAL 1 YEAR)::DATE::VARCHAR AS un_anio_antes
""").fetchall())

A guess is fine: it's kept as you wrote it, so you can compare. Only you and your Maestro see your predictions.

Make your prediction

How to compare periods

Three rules cover almost every case:

  • Compare matching stretches of the calendar. Write both periods as half-open intervals: >= '2026-01-01' AND < '2026-07-01' against >= '2025-01-01' AND < '2025-07-01'. For a month, use the full month in each year: March 2026 sold 1,229,670 pesos and March 2025 sold 1,148,240, so March is up 7.1%.
  • Compare complete periods. If your data stops partway through a month, the current month is incomplete and will look low. Here the data runs through June 30, so June is complete.
  • By day, watch the day of the week. March 2, 2026 was a Monday and March 2, 2025 was a Sunday. A coffee shop doesn't sell the same on a Monday as on a Sunday. Going back 364 days (52 weeks) lands on the same weekday: Monday, March 3, 2025.

If your business compares weeks or days against last year, work out with your Maestro which date to use.

One filter per period and one sum per year:

Python
f = data["fecha"]
v26 = data.loc[(f >= "2026-01-01") & (f < "2026-07-01"), "ventas"].sum()
v25 = data.loc[(f >= "2025-01-01") & (f < "2025-07-01"), "ventas"].sum()
print(round(100 * (v26 - v25) / v25, 1))

FILTER adds up only the rows that meet its condition, so both periods fit in one query:

SQL
SELECT
  sum(ventas) FILTER (WHERE fecha >= '2026-01-01' AND fecha < '2026-07-01') AS ventas_2026,
  sum(ventas) FILTER (WHERE fecha >= '2025-01-01' AND fecha < '2025-07-01') AS ventas_2025
FROM ventas_diarias;

Try it

The query compares each shop's January to June 2026 against all of 2025, and every shop seems to drop by half. Fix the 2025 period so it's the same stretch. A good answer has three rows with positive changes, between 7 and 9%. If you've never seen FILTER before, ask your Maestro to walk you through it with this same query.

For each shop, compare January to June 2026 sales with the same period in 2025 and calculate the percentage change to one decimal place.

Your turn

For each shop, compare January to June 2026 sales with the same period in 2025 and calculate the percentage change to one decimal place.

Tables: ventas_diarias (fecha, sucursal, ventas)

ventas_diarias · 1,598 rows

ColumnType
fecha El día de la venta.DATE
sucursal Roma, Coyoacán o Del Valle.VARCHAR
ventas Ventas del día en pesos.BIGINT

First rows

fechasucursalventas
2025-01-02Roma12120
2025-01-02Coyoacán10650
2025-01-02Del Valle8880

Every run starts from fresh copies of the course's tables, so try anything.

Ready to run your query.

Run it yourself

Try it on your own

Now in pandas: compare the whole chain's sales in March 2026 against March 2025, and print both totals and the percentage change to one decimal place. A good answer gives 7.1.

Compare the chain's sales in March 2026 against March 2025: print both totals and the percentage change to one decimal place.

Compare the chain's sales in March 2026 against March 2025: print both totals and the percentage change to one decimal place.

Run your code to see what it prints and the value of its last expression. Every run starts fresh.

Ready to run your code.

Run your code to see its output here.

Data: ventas-diarias, 1,598 rows

Enter saves a cell and Escape leaves it as it was. Leave a cell empty for a missing value.

The exercise's data, "ventas-diarias". Your edits run with your code.
RowRemove
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Page 1 of 80

Run it yourself

Check yourself: Your data runs through June 30, 2026. What should you compare 2026 sales against to know whether you're doing better than last year?