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
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.
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:
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:
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
| Column | Type |
|---|---|
| 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
| fecha | sucursal | ventas |
|---|---|---|
| 2025-01-02 | Roma | 12120 |
| 2025-01-02 | Coyoacán | 10650 |
| 2025-01-02 | Del Valle | 8880 |
Every run starts from fresh copies of the course's tables, so try anything. Tab indents; to leave the editor, press Escape, then Tab.
Ready to run your query.
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. Ctrl+Enter runs it. Tab indents; to leave the editor, press Escape, then Tab.
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.
| Row | Remove | |||
|---|---|---|---|---|
| 1 | ||||
| 2 | ||||
| 3 | ||||
| 4 | ||||
| 5 | ||||
| 6 | ||||
| 7 | ||||
| 8 | ||||
| 9 | ||||
| 10 | ||||
| 11 | ||||
| 12 | ||||
| 13 | ||||
| 14 | ||||
| 15 | ||||
| 16 | ||||
| 17 | ||||
| 18 | ||||
| 19 | ||||
| 20 |