BS Consulting
Blog

Historical modelling: Which team owned station S17?

S17 moves from North team to South team on 1 June. A production record from May arrives on 3 June. Looking up today’s station record would rewrite the past. A type 2 slowly changing dimension can preserve the correct attribution.

One station, several valid descriptions

The business key StationCode remains S17. The surrogate key StationSK identifies one version of the station’s description. Version 81 belongs to North and version 94 to South. Each version covers a validity interval. The upper bound already belongs to the next version. An open end denotes the current assignment.

S17 ownership in the agreed plant time zone
  1. Until 1 June, 00:00Version 81 · North teamEnd excluded
  2. From 1 June, 00:00Version 94 · South teamStart included

Correcting a typo in a team name need not create a new version. If the organisation wants the corrected spelling in all historical reports, a type 1 overwrite can serve it well. A real change of responsibility is different: previously completed shifts may still belong to the previous owner. Decide the policy per attribute.

Use event time to assign the key

Assigning a key to each run
RunEvent at S17ArrivalStationSK
R1031 May, 23:503 June81
R111 June, 00:001 June94
R122 June, 08:102 June94

Run the following lookup during the fact load with consistent time zones. A left join keeps unmatched runs visible. Store the resulting key on the fact row. Power BI can then use an ordinary relationship from DimStation[StationSK] to that key without resolving time intervals when readers open the report.

SQL
SELECT r.RunID, s.StationSK
FROM ProductionRun AS r
LEFT JOIN DimStation AS s
  ON s.StationCode = r.StationCode
 AND r.EventAt >= s.ValidFrom
 AND (r.EventAt < s.ValidTo OR s.ValidTo IS NULL);

Detect gaps, overlaps and retroactive corrections

Group the lookup result by RunID and count non-null StationSK values. Zero matches suggest a gap or missing reference data. Two matches suggest overlapping intervals. Arbitrarily choosing one hides a data problem. Quarantine the row or use an explicitly labelled unknown member with a defined correction process.

Suppose that on 5 June you learn the handover actually happened on 30 May. Adjusting the dimension boundary also changes which stored fact keys are correct. Identify affected S17 events and restate their keys. Recording load time helps explain when the information arrived. Answering the additional question “What did we believe on 2 June?” would require a separate history of what the system knew at each point in time.

Run the interval check as a load assertion

Expose the time-join result as a temporary view named RunStationMatch with RunID and StationSK. The following test should return zero rows for the three example runs. COUNT(StationSK) counts only matched non-null keys. COUNT(*) would count the unmatched row returned by a left join and could hide the gap you are looking for.

SQL
SELECT RunID, COUNT(StationSK) AS VersionMatches
FROM RunStationMatch
GROUP BY RunID
HAVING COUNT(StationSK) <> 1;

Test the assertion itself. In a test copy introduce a gap between versions 81 and 94 and place a run inside it. Next create an overlap and insert a run during the overlap. Only after the control query detects both conditions does a successful load establish more than the fact that SQL completed without an exception.

Try it yourself

A run occurs exactly at midnight on 1 June. A developer uses BETWEEN with inclusive bounds on both versions. What will the validation show?

Show the solution

The run can match both versions. Its correct new key is 94. An assertion of exactly one match per RunID catches the overlap. The half-open interval in the example excludes the old version at the handover instant.

Sources and further reading