BS Consulting
Blog

Star schema: How 100 parts turn into 300

A run produces 100 parts and receives three defect reports. Joining the two tables makes the report show 300 produced parts. The error is already present before the first visual is built.

State what one row represents

ProductionRun has one row per completed run, including its output quantity. DefectEvent has one row per reported defect. Both contain RunID, but only the first has at most one row per key. Joining the two repeats the output quantity for each matching defect. All three resulting combinations are valid while the sum of their quantities is misleading.

Grain is the meaning of one row. Write it as a sentence for every fact table. Group by the expected key and test uniqueness. Include runs with zero, one and several defects. Testing only runs with exactly one defect hides the error.

Effect of a left join to individual defect reports
RunActual unitsDefect reportsUnits after naive join
R11003300
R260060
Total1603360

Let dimensions filter the facts

Dimensions describe entities such as day, station and product. The station key is unique in the station dimension and can occur many times in the facts. In this example an active one-to-many relationship filtering from the dimension to the fact is sufficient. Put the dimension’s station field in the slicer so the same selection can restrict both production and defect reports.

Two shared dimensions filter two separate fact tables
Calendar dayStation dimension
Production runs · unitsDefect reports · events

Each dimension filters both facts through one-to-many relationships.

The diagram assumes the meaning of day and station is appropriate in both facts. Production day and the later date a defect was reported answer different operational questions. Label the date role explicitly. A defect-type filter also needs a definition: should the denominator continue to cover all output or only runs in which this defect appeared?

Aggregate before joining when you need one row per run

For a run-level export, group the defect messages by RunID first. The left join keeps runs with zero reports. COALESCE converts their missing match to a zero count. The SQL assumes every DefectEvent row represents a distinct report.

SQL
WITH defects_by_run AS (
    SELECT RunID, COUNT(*) AS DefectCount
    FROM DefectEvent
    GROUP BY RunID
)
SELECT p.RunID, p.Units, COALESCE(d.DefectCount, 0) AS DefectCount
FROM ProductionRun AS p
LEFT JOIN defects_by_run AS d ON d.RunID = p.RunID;

The resulting run-level quantities can now be summed. Individual defect details have been collapsed, so retain the event fact for drillthrough. Applying DISTINCT to output quantities afterwards would introduce another error: two separate runs of 100 parts would be treated as a single quantity.

Distinguish two metrics with different units

For the first two runs, defect reports per 100 produced units can be calculated: 3 divided by 160 times 100 is 1.875 reports per 100 units. The percentage of defective parts cannot yet be calculated from those records. All three reports could concern one part, or three different parts. The second question requires a stable part identifier and a rule for repeated reports.

Reflect that difference in your measure names and visual titles. Pass RunID to the drillthrough page so a reviewer can trace the three events without accidentally summing a repeated run quantity in the detail view.

Try it yourself

Add R3 with 100 units and two defect reports. What should the control totals be across all three runs?

Show the solution

Expect 260 produced units and five reports. The naive left join now yields 560 units. Test both correct totals across the full data and for one station to check the grain and the model relationships.

Sources and further reading