Chapter 10 · Transforming with dbt

The Sales Data Mart

Lab:WWI_ANALYTICS.MART_SALES+dbt run --select mart_salesDiagram 5 in Chapter 0 is the full design of this mart.

Gold is the complete, conformed model of the business. A data mart is a small, focused slice of it for one audience: here, everyone who reports on sales. It has one fact table at a single, obvious grain and six dimensions with friendly column names, so a Power BI developer can build reports without knowing anything about the source system.

FACT_SALES_LINEone row per invoice line DIM_DATE DIM_SALESPERSON DIM_DELIVERY_METHOD DIM_GEOGRAPHY DIM_CUSTOMER DIM_STOCK_ITEM INVOICE · DELIVERY SOLD TO · BILLED TO
Figure 10.1 — The mart's star. Two dimensions play two roles each, so the fact has 8 keys and 6 dimensions.

The grain, and why it matters

The grain is one row per invoice line: one product on one invoice. Everything else follows from that sentence. Quantity and money can be added up across any dimension; an invoice's header facts, such as its delivery method, repeat on each of its lines; and invoice and order numbers stay on the fact as degenerate dimensions, so you can always drill back to a source document.

{{ config(alias='fact_sales_line') }}
select
    l.invoice_line_id                      as sales_line_key,
    l.invoice_date                         as invoice_date_key,      -- DIM_DATE, role 1
    l.delivery_date                        as delivery_date_key,     -- DIM_DATE, role 2
    coalesce(cu.customer_key, -1)          as customer_key,          -- DIM_CUSTOMER, sold to
    coalesce(bt.customer_key, -1)          as bill_to_customer_key,  -- DIM_CUSTOMER, billed to
    coalesce(si.stock_item_key, -1)        as stock_item_key,
    coalesce(sp.salesperson_key, -1)       as salesperson_key,
    coalesce(ge.geography_key, -1)         as delivery_geography_key,
    coalesce(dm.delivery_method_key, -1)   as delivery_method_key,
    l.invoice_id, l.invoice_line_id, l.order_id, ...                 -- degenerate dimensions
    l.quantity, l.extended_price, l.tax_amount, l.total_excluding_tax, l.line_profit
from {{ ref('int_invoice_lines_enriched') }} as l
...

One date dimension, two roles

The invoice date and the delivery date both point at the same DIM_DATE. In Power BI, make one relationship active and the other inactive (use USERELATIONSHIP in a measure), or load the date table twice under different names. Don't copy the data into two date tables.

The mart's SCD2 dimensions use the same key formula as Gold, so a customer key means the same version of the same customer in both. The mart and Gold are conformed.

Answering the questions it was built for

These are real results from the lab. Revenue is excluding tax.

Revenue and profit by fiscal year

select d.fiscal_year, count(*) as lines, sum(f.total_excluding_tax) as revenue, sum(f.line_profit) as profit
from WWI_ANALYTICS.MART_SALES.FACT_SALES_LINE f
join WWI_ANALYTICS.MART_SALES.DIM_DATE d on d.date_key = f.invoice_date_key   -- mart's calendar via GOLD.DIM_DATE
group by 1 order by 1;
Fiscal yearLinesRevenueProfitMargin
FY201351,16038,373,718.7019,143,873.9049.9 %
FY201464,75548,879,106.2524,283,242.4549.7 %
FY201571,28653,827,320.9526,815,310.8549.8 %
FY2016 (to 31 May)41,06431,181,195.3015,486,753.7049.7 %

The fiscal year starts in November, and the data stops on 31 May 2016, so FY2016 covers only seven months.

By sales territory and customer category

TerritoryRevenue
Southeast38,265,165.75
Mideast25,758,309.30
Southwest23,924,720.30
Plains23,307,175.85
Great Lakes20,153,233.85
Far West19,879,592.80
Rocky Mountain11,076,949.95
New England7,696,054.80
External2,200,138.60
Customer categoryCustomersRevenue
Novelty Shop459123,220,010.30
Supermarket5814,360,495.80
Gift Store4811,683,614.10
Computer Store5111,560,346.80
Corporate4711,436,874.20

Products, people and delivery

  • Chiller vs dry stock: chilled goods earned 1,142,515.20 at a 43.9 % margin; dry goods 171,118,826.00 at 49.8 %.
  • Top salesperson: Archer Lamble with 17,944,098.45, ahead of Kayla Woodcock (17,566,878.40) and Hudson Onslow (17,315,102.70).
  • Delivery: every invoice line in this sample was delivered by Delivery Van, on average 1.00 day after the invoice.
  • Credit notes: none in this sample (0 of 228,265 lines); the flag is there for real data.
 Snowsight · Worksheet → Chart
revenue and profit by month from MART_SALES.FACT_SALES_LINE, as a chart
The mart in Snowsight's charting.

Key takeaways

  • A mart serves one audience: one fact at one clear grain, a handful of friendly dimensions.
  • Role-playing dimensions (two dates, two customers) use one table, not copies.
  • Keys are conformed with Gold, so mart and Gold agree on every customer version.
  • Degenerate invoice and order numbers let every report drill back to a document.