# sales-forecast

Guess the next 8 weeks of one product's sales from your own history, and see how good each guess would have been before you trust it. From lesson 05-08 of Distru's No Bullshit AI Course (learnai.distru.com).

Two ways in. They give the same numbers.

- **No tech person:** the sheet, [`weekly-sales-forecast.csv`](../weekly-sales-forecast.csv). Import it into Google Sheets (File > Import) or open it in Excel, then paste your weekly totals over columns A and B.
- **Tech person:** [`forecast.py`](forecast.py). Point it at Distru's Sales Order Item History report and it does the weekly totals, the test and the forecast.

## What it does

It totals sales into weeks that start on a Sunday, hides your last 8 finished weeks, and asks three simple methods to forecast them:

| Method | Next week is… |
|---|---|
| Same as last week | last week's number |
| Recent average | the average of the last 4 weeks (you can change the 4) |
| Last year's pattern | the same week last year, scaled by how you are doing against last year (your last 8 weeks divided by the same 8 weeks a year earlier) |

Each one is scored on the hidden weeks: how many units it was off on an average week, and what percent that is. Then all three forecast your next 8 weeks, and it tells you which did best on the test.

Sunday weeks matter. Week 1 and week 53 are then exactly 364 days apart, the same calendar dates, so last year's 4/20 week lines up with this year's.

## The sheet

1. Get weekly totals for one product, oldest first. The script's `--weekly-out` gives you exactly that. Or ask your chat AI to write the formula that adds your export up by week, and check its total against the export's total before you paste.
2. Paste them over columns A (the Sunday each week starts) and B (units). Up to 156 weeks.
3. Read row 19: which method did best on your last 8 weeks. Use that column in the "Next 8 weeks" block.
4. Your last 8 weeks were quiet? Test it around last 4/20 instead: find 4/20's week in column A, take its row number, subtract 6, and type that in cell E6.

Last year's pattern needs 68 weeks of data. With less, its column stays blank.

Column I uses Excel's `FORECAST.ETS` (exponential smoothing with a 52-week season). Google Sheets and Numbers do not have it, so it shows an error there; ignore it. We checked the other formulas with a spreadsheet engine against the script; they agree to the decimal.

## The script

```bash
# 1. See what it does, on made-up data. No file, no key.
python3 forecast.py --demo
python3 forecast.py --demo --hide-from 2026-03-22     # score the three around 4/20 instead

# 2. From a CSV: Distru's Sales Order Item History report, or anything with a date and a quantity column.
python3 forecast.py --csv item-history.csv --product "Blue Dream 3.5g"
python3 forecast.py --csv item-history.csv --product "Blue Dream 3.5g" --weekly-out weeks.csv   # for the sheet

# 3. Straight from Distru. See the requests first; nothing is sent.
python3 forecast.py --distru --product-id <product id> --since 2024-09-01 --dry-run
export DISTRU_API_TOKEN=...     # Distru -> Settings -> Integrations -> Distru API. Never paste it into a chat.
python3 forecast.py --distru --product-id <product id> --since 2024-09-01
```

Python 3.9 or newer, standard library only. Nothing to install. Writes `forecast.csv` (change it with `--out`).

What it reads:

- **CSV:** headers are matched loosely, so `Order Date`, `order_date` and `order date` are the same column. Use `--date-col` and `--qty-col` if yours are called something else. `--product` keeps rows whose product name, SKU or id matches exactly (case does not matter). Canceled orders are skipped, and returned quantity is subtracted when the column is there. The week still in progress is dropped.
- **Distru:** `GET /public/v1/reports/sales-order-item-history`, one request per 90 days, with `product_ids[]`, `sample=EXCLUDE` and every order status except `CANCELED`. The report returns everything in one response (no `next_page`) and falls back to the last 30 days if `order_datetime` is left out, so the script always sends a date range. Order dates come back in your company's timezone. GET only; the key is read from the environment and never printed.

## Before you trust it

- **Weeks you were out of stock look like weeks nobody wanted it.** Every method learns that as a quiet week and orders less, and you run out again. The script warns you when many weeks are zero. Fix those weeks by hand, or leave them out.
- **New products have no history.** Use a similar product's numbers, and say so.
- **It only knows your past.** A new store opening, a price change, a competitor closing, a new rule from your state: add what you know on top.
- **Pick the method on the test, not on a hunch.** In quiet weeks the three are close. Around 4/20 and the holidays, last year's pattern usually wins by a lot.

## For the tech person

This is the floor, not the ceiling. The seasonal naive forecast is the standard baseline in forecasting; a fancier model (ETS, ARIMA, gradient boosting over many SKUs with promotions and price as features) has to beat it on a walk-forward test before it earns its complexity. Never score on a random split of weeks: the model then sees weeks after the ones it is guessing. Good reading: Hyndman and Athanasopoulos, *Forecasting: Principles and Practice* (otexts.com/fpp3), and the statsmodels `ExponentialSmoothing` and `SARIMAX` docs.

Adapted in part from Microsoft's *ML for Beginners*, time series lessons (MIT License, Copyright (c) Microsoft Corporation); not endorsed by Microsoft. CC0 1.0: copy it, change it, no attribution needed. Version 1.0.0 (2026-09-29).
