Sign in to run and submit your work
Reading is open to everyone. Running code and saving drafts need an account so your work is yours and comes back on your next visit.
or
CODE WORKSPACE
Every order is reported with the customer's tier. The pipeline joins to the customer dimension and takes the current row, which means an order placed in February is reported under the tier the customer reached in May. The revenue by tier report has been restating itself every month and nobody could see why.
Write as_of_join(facts, dimension, key_field, time_field, attributes).
Function to write
as_of_join(facts: list[dict], dimension: list[dict], key_field: str, time_field: str, attributes: list[str]) -> list[dict]Copies of the facts with the attributes in effect at their timestamp, plus the version's valid_from.
How to approach it
Sort each key's versions once, then binary-search the start dates for each fact.
Sample cases
+ 2 held back until you submit
orders across three versions
An order before any version, one exactly on a boundary, one between versions, and one for an unknown key.
Input
Argument 1
| order_id | customer_id | ordered_at | amount |
|---|---|---|---|
| o1 | c1 | 2026-02-14 | 10 |
| o2 | c1 | 2026-03-01 | 20 |
| o3 | c1 | 2026-04-30 | 30 |
| o4 | c2 | 2026-01-01 | 40 |
| o5 | c3 | 2026-03-05 | 50 |
Argument 2
| customer_id | valid_from | tier |
|---|---|---|
| c1 | 2026-01-01 | silver |
| c1 | 2026-03-01 | platinum |
| c1 | 2026-05-01 | gold |
| c2 | 2026-02-01 | free |
Argument 3
'customer_id'Argument 4
'ordered_at'Argument 5
[
'tier'
]Returns
| order_id | customer_id | ordered_at | amount | tier | dimension_valid_from |
|---|---|---|---|---|---|
| o1 | c1 | 2026-02-14 | 10 | silver | 2026-01-01 |
| o2 | c1 | 2026-03-01 | 20 | platinum | 2026-03-01 |
| o3 | c1 | 2026-04-30 | 30 | platinum | 2026-03-01 |
| o4 | c2 | 2026-01-01 | 40 | None | None |
| o5 | c3 | 2026-03-05 | 50 | None | None |
no facts
Nothing to enrich returns an empty list rather than the dimension.
Input
Argument 1
[] (empty list)
Argument 2
| customer_id | valid_from | tier |
|---|---|---|
| c1 | 2026-01-01 | silver |
| c1 | 2026-03-01 | platinum |
| c1 | 2026-05-01 | gold |
| c2 | 2026-02-01 | free |
Argument 3
'customer_id'Argument 4
'ordered_at'Argument 5
[
'tier'
]Returns
[] (empty list)
an empty dimension
With no versions at all every fact still comes back, carrying nulls.
Input
Argument 1
| order_id | customer_id | ordered_at | amount |
|---|---|---|---|
| o1 | c1 | 2026-02-14 | 10 |
| o2 | c1 | 2026-03-01 | 20 |
| o3 | c1 | 2026-04-30 | 30 |
| o4 | c2 | 2026-01-01 | 40 |
| o5 | c3 | 2026-03-05 | 50 |
Argument 2
[] (empty list)
Argument 3
'customer_id'Argument 4
'ordered_at'Argument 5
[
'tier'
]Returns
| order_id | customer_id | ordered_at | amount | tier | dimension_valid_from |
|---|---|---|---|---|---|
| o1 | c1 | 2026-02-14 | 10 | None | None |
| o2 | c1 | 2026-03-01 | 20 | None | None |
| o3 | c1 | 2026-04-30 | 30 | None | None |
| o4 | c2 | 2026-01-01 | 40 | None | None |
| o5 | c3 | 2026-03-05 | 50 | None | None |
Constraints
valid_from is the latest one at or before the fact's timestamp.None for each attribute — the customer had no tier yet, and using the earliest version invents one.dimension_valid_from.Worked example
Order o1 was placed on 2026-02-14, when c1 was silver — the platinum version does not begin until 2026-03-01. Joining to the current row reports it as gold, the tier that began in May, and moves three months of revenue into a tier that did not exist yet.
Order o2 falls exactly on 2026-03-01, the day platinum begins. It is platinum, because the interval is closed at the start and open at the end — the same convention that makes the versions tile the timeline without overlapping.
Order o4 for c2 was placed on 2026-01-01, a month before that customer has any version at all. The tier is None. Reaching for the earliest version instead is the tempting fix and it fabricates history: it asserts the customer was free in January, which the dimension does not say.
What this tests
Point-in-time correctness — the reason a fact table is not simply joined to a dimension — and binary search over a sorted history. The leakage this prevents is the same one that ruins a machine-learning training set built from a warehouse.
as_of_join(facts: list[dict], dimension: list[dict], key_field: str, time_field: str, attributes: list[str]) -> list[dict]Submit for review to find out what your query gets right, what it gets wrong, and how it compares with the best working query for this exercise.
This scenario runs a full workspace — editor, canvas and results side by side. It needs a laptop or desktop to be usable. Open this page on a bigger screen to start building.