Turn changing source columns into a stable sales table
A branch sends one row per product, with months as columns.
Turn changing source columns into a stable sales table
A branch sends one row per product, with months as columns. Next month it will add another column. Reshape the table into one row per product and period so the same model can handle the next delivery.
The instructor keeps the product key fixed and unpivots the other fields, including new month columns on subsequent refreshes.
Work through the decision
Two products each have January and February amounts. After unpivoting, how would you detect both missing rows and duplicated money?
Check your reasoning
Expect four product-month records if all four amounts are present. Reconcile their sum to the four original cells and assert one row per product-month. Define how genuinely missing months should be represented.
Decisions and tradeoffs
- Unpivot other columns
- Preserve stable identifiers and turn the remaining period columns into attribute-value rows. Isolate any unrelated metadata first.
- Append queries
- Append stacks compatible row sets. Align column names and data types to avoid sparse or misleading fields.
- Merge queries
- A merge uses matching keys to add information across tables. Check uniqueness on the lookup side.
- Reference the staging query
- A reference starts from an existing query’s result and follows its upstream steps. It does not guarantee a single source read.
- Disable load on staging
- Keep an intermediate query available to dependent queries without exposing an intermediate table in the semantic model.
- Group by the reporting grain
- Aggregate only after defining the output key and how each numeric field combines.
Practice each guide skill
Work through these exercises in the order of the published guide. The checklist numbers are local references.
- Convert an amount using the supplier’s numeric locale and verify precision on a known invoice.
- Split a compound identifier or derive line amount from quantity and price, then check against a known record.
- Group by branch and day and reconcile the daily totals back to order lines.
- Unpivot monthly headers into rows, pivot a category into fields and identify when transposing would exchange the wrong axes.
- Turn a JSON list into records and expand nested fields while preserving parent identifiers.
- Separate order-line facts from product attributes. Verify one dimension row per business key.
- Compare a reference with a duplicate after editing the staging query. Observe which derived definition follows the edit.
- Append two months with the same schema; merge customer attributes by key and count unmatched facts.
- Choose stable dimension and fact keys; detect blank or duplicated dimension keys before defining the relationship.
- Keep intermediate query dependencies available during refresh while disabling their model load.
Official objectives for this lesson
Reconcile a changing set of month columns
Predict how many product-month rows survive when April is added and one cell is missing. Zero represents a measured sale of zero. This Python exercise mirrors the shape of an unpivot. Run with Python 3 and its standard library.
wide = [
{"sku": "A", "Mar": 8, "Apr": 0},
{"sku": "B", "Mar": 4, "Apr": None},
]
facts = []
missing = []
for product in wide:
for month, value in product.items():
if month == "sku":
continue
key = (product["sku"], month)
if value is None:
missing.append(key)
else:
facts.append((*key, value))
assert len({(sku, month) for sku, month, _ in facts}) == len(facts)
print("rows:", len(facts), "sum:", sum(value for _, _, value in facts))
print("missing:", missing)
print("observed zeros:", sum(value == 0 for _, _, value in facts))
Check the result
rows: 3 sum: 12
missing: [('B', 'Apr')]
observed zeros: 1One of four source cells has no observation. The zero remains an explicit fact. Add May to both products and verify that the transformation accepts the new month without changing the loop. In Power Query, keep any extra non-month metadata out of the unpivot set.
Related lesson · Build a sales fact table at a stable grain
Turn changing source columns into a stable sales table quiz
Answer 3 questions. Check each answer for an explanation.
Worked example · Turn changing source columns into a stable sales table