सामग्री पर जाएँ

लेसन 10 · Facts and Dimensions

Measures and Additivity

Learn which measures you can safely sum: additive, semi-additive and non-additive facts, how to compute ratios and averages, and how to treat NULLs.

A number in a fact table looks like something you can add up. Often it is, sometimes only in some directions, and sometimes never. Getting this wrong produces reports that look reasonable and are false. This property is called additivity.

The three kinds

Kind Can you sum it across... Examples
Additive All dimensions Sales amount, quantity, discount amount
Semi-additive Some dimensions, usually not time Account balance, stock on hand, headcount
Non-additive None Unit price, percentage margin, ratio, temperature

Counts of distinct things need care too. Monthly unique customers cannot be added to get yearly unique customers, because the same person may appear in several months.

Additive facts

Sales amount can be summed by product, store, customer and time in any combination. Prefer to store additive facts. Instead of storing only a unit price, also store the extended amount (quantity times price) so that totals are simple and consistent.

Semi-additive facts

Stock on hand can be added across stores: 5 tents in Lisbon plus 7 in Porto makes 12 tents. It cannot be added across days: 12 tents on Monday and 12 on Tuesday is not 24 tents.

For time, pick a rule that fits the question: the last value of the period, the average over the period, or the minimum or maximum.

SQL Handle a semi-additive balance

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

Notice the two steps for the average. A plain AVG(on_hand) over all rows would give the average per store per day, which answers a different question.

Non-additive facts and ratios

For a ratio, store the parts and calculate the ratio at query time, after aggregating. The rule: ratio of sums, not sum or average of ratios.

SQL Ratio of sums vs average of ratios

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

The first query says 45%, the second about 10.7%. The business earned 108 on 1,010 of sales, so the second is correct. NULLIF(x, 0) turns a zero divisor into NULL, so the query does not fail or invent a number on platforms that raise division errors.

Distinct counts

To count unique customers for a quarter, count over the quarter's detail rows. Adding three monthly distinct counts double counts repeat buyers. Some platforms offer approximate distinct-count structures that can be merged, trading a small error for speed.

NULLs, zeros and units

  • NULL means unknown or not applicable. Zero is a real measurement. AVG and SUM skip NULL values, so this choice changes results.
  • Do not leave a dimension key empty in a fact table. Point it at an "Unknown" dimension row, so inner joins do not drop facts.
  • Keep one unit per column. Mixed currencies cannot be summed. Common designs store the original amount, the currency code and a converted amount, and document which exchange rate and date were used.
  • Use exact decimal types or integer minor units for money. Binary floating point can produce small rounding differences. The examples here use REAL only for brevity.

Document every measure

Measure Definition Additivity Notes
net_amount gross_amount minus discount_amount Additive Excludes tax
on_hand Units counted at end of day Semi-additive Not summable over time
margin_pct (net - cost) / net Non-additive Compute after summing

A table like this belongs in your documentation and in the semantic layer.

Non-additive values worth keeping

Some numbers belong in the fact table even though they cannot be summed. A unit price at the time of sale helps to study pricing: you might look at its minimum, maximum or typical value. For an average selling price, though, divide summed revenue by summed quantity, which weights each price by the units sold at that price.

Measurements such as temperature or a satisfaction score can be averaged, but think about what is being weighted. An average over rows gives busy sensors or large stores more influence. Sometimes that is what you want, and sometimes each location should count once. Write the choice into the definition.

Currencies, taxes and signs

Money measures hide several decisions:

  • Tax: is net_amount before or after tax?
  • Discounts: are they stored as positive numbers to subtract, or as negative numbers to add?
  • Returns: are they negative rows in the sales table, or positive rows in a returns table?
  • Currency conversion: which rate source and which date apply?

Use one sign convention and describe it. If one source stores discounts as negative numbers and another as positive, convert them while loading, before anyone sums them together.

Test additivity before you publish

For each measure, ask three questions: Does the sum across products mean something? Across places? Across time? Then try each in SQL and show the result to someone who knows the business. If a sum has no meaning, hide it or provide a correct calculation under a clear name.

Counting events

Fact tables often need counts as well as sums. COUNT(*) counts rows, which at a receipt-line grain means lines, not receipts. To count receipts use COUNT(DISTINCT receipt_no), and make sure the identifier is unique across stores, or count the combination of store and receipt number. Many surprising "average basket" figures come from dividing revenue by the number of lines instead of the number of baskets.

Common mistakes

BI tools often apply SUM to every numeric column by default. Check the default aggregation for balances, prices and percentages, and hide raw columns that should not be summed.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. Which measure is semi-additive?

    Balances can be added across accounts but not across days.
  2. 2. How should you compute an overall margin percentage?

    The ratio of sums weights each row by its size.
  3. 3. Why can monthly unique customer counts not be added into a yearly count?

    Distinct counts overlap between periods.
  4. 4. What is a good way to handle a fact row whose customer is unknown?

    The fact stays visible in joins and is clearly labeled.

Summary

  • Additive facts sum in all directions; semi-additive ones do not sum over time; non-additive ones never sum.
  • For balances choose last, average, minimum or maximum over time.
  • Calculate ratios from summed parts, and recount distinct values from detail.
  • Treat NULL, zero, currencies and money types deliberately, and document each measure.

Next: Dimension Tables and Surrogate Keys

आपकी प्रगति इसी ब्राउज़र में सेव होती है। खाते की ज़रूरत नहीं है।