dim is a type-2 slowly-changing dimension of customer plans: a list of versions, each a dict
{"key", "plan", "valid_from", "valid_to", "current"}
with dates as "YYYY-MM-DD" strings. A key's current version has valid_to = None and current = True.
changes is today's batch, in no particular order: dicts {"key", "plan", "changed_on"}. Apply them, key by key, in changed_on order:
- •A change to the plan the key already has at that point (its current version — which may have been created earlier in this batch) does nothing.
- •A change dated on or before the current version's
valid_from is stale: ignore it. - •Otherwise the current version is closed (
valid_to = changed_on, current = False) and a new current version starts (valid_from = changed_on, valid_to = None, current = True). - •A key with no versions gets its first one, starting on
changed_on.
Write apply_scd2(dim, changes) returning the whole dimension afterwards — every version, sorted by key, then valid_from. Don't modify dim or the dicts in it.
Python 3.13 in your browser — the standard library plus pandas and numpy; no pip installs.