Month-end inventory in Power BI: select the last valid observation
Build a SQLite view that preserves missing counts, then report closing inventory and observation coverage separately in Power BI.
In this article
Inventory is a balance at a point in time
A warehouse closes January with 47 units and February with 42. A grand total of 89 looks plausible in a matrix, but it adds two observations of the same stock across time. A closing balance belongs to a point in time. You can add products at a shared cutoff when their units are comparable. To sum across dates, use a flow measure such as units shipped. For this report, a selection of several months should show the balance of the latest visible close. Products that were missed on closing day use their last observation at or before the cutoff. Carrying that value forward is a business assumption. Keep the source date and its age available so the report shows which rows rely on it.
Build a fixture with a real zero and a missing product
Start with an empty SQLite database, two month ends, and products A, B, and C. The snapshot table permits a single observation per product and date. A is counted on both month ends. B is counted two days before January close and fifteen days before February close. The latter count is an explicit zero. C appears in the expected product list and has no observation. B also has a March count of 99. That number makes an accidental future lookup easy to catch. All dates are ISO strings in one business calendar. If the source contains UTC timestamps, assign each observation its business date first. When multiple observations per day are valid, use a timestamp plus a stable tie breaker in the key and ordering.
CREATE TABLE month_end (Cutoff TEXT PRIMARY KEY);
CREATE TABLE product (Product TEXT PRIMARY KEY);
CREATE TABLE snapshots (
Product TEXT NOT NULL REFERENCES product(Product),
SnapshotDate TEXT NOT NULL,
Units INTEGER NOT NULL,
PRIMARY KEY (Product, SnapshotDate)
);
INSERT INTO month_end VALUES ('2026-01-31'), ('2026-02-28');
INSERT INTO product VALUES ('A'), ('B'), ('C');
INSERT INTO snapshots VALUES
('A', '2026-01-31', 40), ('A', '2026-02-28', 42),
('B', '2026-01-29', 7), ('B', '2026-02-13', 0),
('B', '2026-03-01', 99);
Select the last observation for each closing date
The query pairs close dates with expected products and joins observations on or before each cutoff. Keeping the date condition in the LEFT JOIN preserves C. Moving it to WHERE would discard the row because there is no snapshot date to compare. ROW_NUMBER ranks the newest observation first within each cutoff and product. The result retains the closing date, the observation date, the quantity, and the age in days. The February row for B uses February 13 and shows an age of 15 days. The row for C has a missing quantity, observation date, and age. On a large history, measure the intermediate join size. A precomputed close or an indexed latest-row lookup can use the same cutoff rule while reducing the candidate rows.
CREATE VIEW inventory_close AS
WITH candidates AS (
SELECT m.Cutoff, p.Product, s.SnapshotDate, s.Units,
ROW_NUMBER() OVER (
PARTITION BY m.Cutoff, p.Product
ORDER BY s.SnapshotDate DESC
) AS rn
FROM month_end AS m
CROSS JOIN product AS p
LEFT JOIN snapshots AS s
ON s.Product = p.Product AND s.SnapshotDate <= m.Cutoff
)
SELECT Cutoff, Product, SnapshotDate, Units,
CAST(JULIANDAY(Cutoff) - JULIANDAY(SnapshotDate) AS INTEGER) AS AgeDays
FROM candidates
WHERE rn = 1;
SELECT * FROM inventory_close ORDER BY Cutoff, Product;
Connect the closing table to a Power BI model
Export the five view columns with a header row using SQLite CSV mode, then import the file through Text/CSV in Power BI. Name the query InventoryClose. Set Cutoff and SnapshotDate to Date and Units and AgeDays to Whole Number. Leave missing cells as null in Power Query. Create a MonthEnd dimension with one row per closing date and a Product dimension with one row per product. Both dimensions filter InventoryClose through active, single-direction, one-to-many relationships. Use Cutoff as the date relationship. SnapshotDate stays available for diagnostics because it answers a different question. Place MonthEnd dates on the matrix and use the Product dimension for its slicer. Include only published closing dates in the month dimension, since a future closing date would become the latest visible cutoff.
Make totals follow the latest visible close in DAX
MAX reads the latest close visible in the current cell. On the January row it selects January, and on the two-month total it selects February. CALCULATE evaluates the quantity over that cutoff. KEEPFILTERS intersects the date restriction with the current selection, and the Product dimension continues to filter the fact. With both months selected, the main card shows 42. With January selected, it shows 47. With only B selected in February, the balance is zero. With C selected, the balance is blank. The second measure counts nonblank quantities, including zero, and reports zero observations when none exist. Create the measures separately and display a closing date alongside a total so the report makes its time basis visible.
Units at Close =
VAR Cutoff = MAX('MonthEnd'[Cutoff])
RETURN
CALCULATE(
SUM('InventoryClose'[Units]),
KEEPFILTERS('MonthEnd'[Cutoff] = Cutoff)
)
Observed Products at Close =
VAR Cutoff = MAX('MonthEnd'[Cutoff])
RETURN
COALESCE(
CALCULATE(
COUNT('InventoryClose'[Units]),
KEEPFILTERS('MonthEnd'[Cutoff] = Cutoff)
),
0
)
Reconcile quantity, coverage, and observation age
The fixture returns six rows. January shows A 40, B 7, and C blank. February shows A 42, B zero, and C blank. Both months have two observed products out of three expected. Confirm that each product appears once per close and that the March observation never contributes to February. For production, agree on a business calendar and how old an observation can be before reconciliation is required. With a review threshold of fourteen days, B is flagged in February even though its quantity is valid. If late corrections can restate a past close, regenerate the affected month ends and retain the source version. Check totals, coverage, and stale observations before changing report formatting. These are separate questions about quantity, completeness, and how recently it was measured.
Reader discussion
Which DAX or data model metric would you check first when applying this approach?
No comments yet. Be the first to contribute.
Write a comment