Lompat ke konten

Pelajaran 6 ยท Dimensional Modeling

The Dimensional Design Process

Design a dimensional model in four steps: choose the business process, declare the grain, identify dimensions and identify facts, with runnable SQL checks.

Dimensional modeling arranges data around two ideas. Facts are measurements of something that happened, such as the quantity and value of a sale. Dimensions describe the context: when, who, what, where. The result is easy to read, and most questions have the same shape: sum some facts, grouped and filtered by dimension attributes.

Ralph Kimball's well-known method designs each model in four steps. Doing them in order prevents most modeling mistakes.

Four steps, in order

Business process What activity is measured?
Grain What is one row?
1 / 2

Step 1: choose the business process

A business process is an activity the organization performs and records: selling, shipping, receiving stock, handling support tickets. Model the process, not a department or a report. A table built for "the marketing dashboard" tends to break when someone asks a different question.

For Harbor Outfitters, start with retail sales.

Talk to people who do the work and who use the numbers. Collect real questions, such as:

  • Which products sell best in each store by month?
  • How much discount did we give during each promotion?
  • Do loyalty members buy more items per visit?

Step 2: declare the grain

The grain states exactly what one row of the fact table represents. Write it as a full sentence:

One row for each product line on a sales receipt.

This is the most important decision. Everything else must be true at that grain. Prefer the most detailed (atomic) grain the source offers: you can always sum detail into totals, but you cannot split a daily total back into receipts.

Different grains need different fact tables. Do not put daily store targets into the receipt-line table.

Step 3: identify the dimensions

Ask what describes each row: date, store, product, customer, promotion. Each dimension should have one value per fact row. If a sale line can have several promotions at once, you need a different design, such as a bridge table.

Fill dimensions with readable attributes. A product dimension with name, brand, category and size gives users many ways to group and filter.

Step 4: identify the facts

Facts are the measurements that make sense at the declared grain: quantity, gross amount, discount amount, net amount, cost. A value that belongs to a coarser grain does not fit. An order-level shipping fee copied onto every line would be counted several times; either allocate it across the lines with an agreed rule or keep it in a separate fact table.

Write the design down

A one-page design sheet is a good artifact to review with users:

Item Retail sales design
Process Retail sales
Grain One row per product line on a receipt
Dimensions Date, store, product, customer, promotion
Facts quantity, gross_amount, discount_amount, net_amount, cost_amount
Source Point-of-sale system
Freshness Loaded daily
Open questions How are returns recorded? Which date counts for online pickups?

The open questions row is valuable. It shows what you still do not know.

Build and test the grain

A grain statement can be tested. If the grain is one row per receipt line, the combination of receipt number and line number must be unique.

SQL Check that the data matches the declared grain

Press Run (or Ctrl + Enter) to see the result here.

The test finds receipt R101, line 1, twice. DISTINCT is used only to show the effect; in a real pipeline you would find out why the row arrived twice and stop it at the source of the problem. In production, enforce the grain with a unique constraint if your platform enforces them. Several analytical platforms accept key declarations without enforcing them, so check yours and run tests like this one.

Interview questions that reveal the grain

The grain often hides in the details of how work is done. Ask the people who record the data:

  • What exactly creates a record, and when?
  • Can a record be changed or canceled later? How would you know?
  • Can one sale be paid in two ways or shipped in two parcels?
  • How are returns and exchanges recorded: as new rows, as negative rows or by editing the original?
  • Is there anything you track on paper or in a spreadsheet because the system cannot?

Then look at the data. Count rows per receipt, search for repeated line numbers and check for negative quantities. What people describe and what the system stores sometimes differ, and both deserve a place in your notes.

Worked decision: what is one row?

For retail sales you could choose among these grains:

Candidate grain What you gain What you lose
One row per receipt line Product-level and basket analysis Largest table
One row per receipt Basket totals Which products were bought
One row per store per day Very small and fast Products, baskets and customers

The first is atomic, so it can produce the other two with GROUP BY. If performance later requires a daily summary, build it as an additional aggregate table, derived from the detail. Do not replace the detail with the summary.

Adding things later

A good grain makes change manageable. You can add a new fact to the table if it is true at the existing grain. You can add a new dimension if it has one value for each existing row; old rows point to a member such as "Not recorded". You can add attributes to a dimension, although older versions may lack values for them.

What you cannot safely do is quietly change the grain, for example by starting to load one row per unit sold instead of one per line. Every count built on the table would change meaning. A new grain calls for a new table and a planned migration.

Common mistakes

The classic error is mixing grains in one table, such as line rows plus a "total" row per receipt. Every sum then counts money twice. If you cannot say what one row means in a single sentence, stop and fix the grain.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the correct order of the four steps?

    You choose what to measure, define one row, then add its context and measurements.
  2. 2. Which is a clear grain statement?

    It says exactly what a single row represents.
  3. 3. Why prefer an atomic grain?

    Detailed data supports questions you have not thought of yet.
  4. 4. An order-level shipping fee is copied onto each of three order lines. What happens when you sum it?

    A coarser-grain value repeated on finer rows is overcounted.

Summary

  • Model business processes, not individual reports.
  • Follow the order: process, grain, dimensions, facts.
  • State the grain in one sentence and test it in the data.
  • Every dimension and fact must be true at that grain.

Next: Star and Snowflake Schemas

Progres Anda disimpan di browser ini. Tidak perlu akun.