Build with Daana

Chapter 7: Reporting with DAR

In chapter 6, you read the bookstore data warehouse at one instant. Reporting means choosing what each row represents, which timestamp dates it, and how to test its amount. Operations needs the amounts on order lines received each day, grouped by currency. The day comes from ingested_at.

You make those choices in the reporting declarations. Daana adds views in daana_dar and leaves the model, mappings, and loaded warehouse from chapters 1 to 6 unchanged. You will read the example's daily output, apply it, and extend it. Afterward, you will be able to read and apply a reporting output, verify it against the source, trace a row to its load, and add a measure.

Start state: The main project bookstore has completed daana execute. The terminal is in bookstore, and DuckDB is closed. To start at this chapter, follow Start at any chapter.

Reporting outputs

DAR (Data according to Requirements) declares reporting outputs on the DAB (Data according to Business), the business model with history in daana_dw. Its YAML documents describe dimensions that hold entity versions and facts that hold amounts to report. Each DAR document is a YAML file with one declaration. The grain defines what each row represents.

The outputs are views in daana_dar. Each output is declared on the entities of the DAB, with its own grain and its own time basis. The next section opens dar/order_line.yaml, where these keys define its rows:

  • kind: fact.state makes line_fact the line fact, with one row per order line. Its axis gives the timestamp that the column fact_ts holds.
  • kind: fact.consolidated gives one row per combination of the columns in grain.
  • legs names the input facts; lines reads order_line.line_fact.
  • date gives the date expression for the first column of grain.

The chapter explains the other keys where it uses them. It leaves out spine, additivity, and ai_context, because its steps do not depend on them. For another example, read the declaration in the guide.

Get the documents

The example's reporting documents are in checkpoint ch7. Create the side project reporting-files, then copy only its three DAR documents into the main project:

Run in terminal

daana init ../reporting-files --example bookstore --checkpoint ch7
cp ../reporting-files/dar/*.yaml dar/

The directory dar/ now holds customer.yaml, order.yaml, and order_line.yaml beside README.md. Apply them unchanged until the last task. customer.yaml and order.yaml declare the dimensions that the optional tier section reads.

The next steps printed by daana init do not apply. The side project is only created, never deployed, so it needs no license. Open dar/order_line.yaml in your editor.

The lines received each day

Look at the last entry in dar/order_line.yaml, the output daily_revenue:

  daily_revenue:                      # compiles to fct__order_line_daily_revenue
    kind: fact.consolidated
    description: >
      Daily revenue: order lines' amounts summed by the day the line was received,
      its order's day while lines arrive on time. One row per day and currency;
      every line is USD in this data.
    grain: [received_date, order_line_amount_currency]
    spine: {driving: lines}
    legs:
      lines:
        fact: order_line.line_fact
        date: "CAST(fact_ts AS DATE)"   # the day the line was received: its order's day while lines arrive on time
        order_line_amount_currency: order_line_amount_currency
        aggregates:
          daily_revenue_amount:
            expr: order_line_amount_amount
            agg: sum
            description: >
              Daily revenue: the amounts charged on the order lines that
              were received that day, tax included, in the lines' currency.
            ai_context:
              synonyms: [daily revenue, revenue per day, daily sales, sales by day]

The daily output, daily_revenue, sums the line fact's amounts by day and currency. Its column received_date holds the day each line was received, the date of ingested_at. The view is fct__order_line_daily_revenue, and its amount column is daily_revenue_amount.

A line received on a later day than its order counts on the day it was received. Chapter 8 investigates a report in which that happens.

Exercise

Find the declarations that make the daily output's day the day its lines were received. Start at the leg's date and end in dab/mappings/order-line-mapping.yaml. Name the three links across the two files and what each gives the next, ending at a source column. Read only, and try before opening a correct answer.

Hint: The leg's date names a column of the line fact. The list of keys in "Reporting outputs" says which key of the line fact gives that column its value. In the mapping, one entry dates every row of the entity.

Show a correct answer

A correct answer

The leg's date is CAST(fact_ts AS DATE), so the day is the date of fact_ts. Find the line fact's axis in the same file:

    axis:
      expr: eff_tmstp                 # ingested_at: when the line was received in stage
      description: When the line was received in stage, a load time; on the order's day while lines arrive on time

The line fact's timestamp column, fact_ts, holds the order line's effective timestamp, eff_tmstp. Find its source in dab/mappings/order-line-mapping.yaml:

        entity_effective_timestamp_expression: ingested_at

In this project, the mapping sets the effective timestamp to the line's load time, which is the time at which the line was received in the source table. It is not the time at which daana execute ran. The main project's mapping is the checkpoint's file, which daana init wrote in chapter 1. Chapter 5 made the same choice in its side project.

The name received_date is only a label for the day: grain names the column, and date gives its value.

Check, then apply

Check the documents before applying them:

Run in terminal

daana dar check dar/ && daana dar apply dar/

Expected output of daana dar check

Checking dar check: dar/

Spec: dar/v1.0
Anchor: entity:customer
Documents: 3
Outputs: 5

✓ Validation passed

daana dar check reads the documents in dar/ offline and reports their specification, dar/v1.0, three documents, and five outputs. It also compares each document with the local model. It refuses a document that names something the model does not have: an entity that the document reads, a relationship that it declares, or an attribute that it lists in a dimension.

A pass does not show that the amounts in the data are correct. Inspect the result of creating the reporting views:

Expected output of daana dar apply

Applied 11 objects (11 views) after 1 checks to daana_dar

daana dar apply compiles the documents against the deployed DAB and creates the views in daana_dar. Without --connection, it uses the duckdb profile named in daana.yaml. The five outputs produce eleven views. Each of the three dimensions also has a latest view, the line fact has a wide view, and two views hold the relationships that the line fact reads.

Daana ran the line fact's output check after creating the views, and it passed. This apply needed no deploy or execute of the DAB.

Query the daily output

Open DuckDB:

Run in terminal

duckdb -readonly bookstore.duckdb

The new view already groups amounts by the day received and currency, so count(*) counts days for each currency. Count the days, find the first and the last, and add up the amounts:

Run in DuckDB

SELECT order_line_amount_currency AS currency, count(*) AS reporting_days,
       min(received_date) AS first_day, max(received_date) AS last_day,
       round(sum(daily_revenue_amount), 2) AS amount
FROM daana_dar.fct__order_line_daily_revenue
GROUP BY order_line_amount_currency;

Expected output

currencyreporting_daysfirst_daylast_dayamount
USD162024-11-012024-11-25762.88

The 762.88 USD matches the source total of chapter 2 over 31 lines. Each of the 16 rows is a day from 1 to 25 November on which lines were received. Its received_date holds that day, not the day on which an order was placed. Days without lines have no row.

Daana generated the view's SQL without changing the integrated order-line entity. The next checks examine more than the total.

The output check

The document also declares a check on the line fact. Find it at the end of line_fact:

    checks:
      every_line_has_an_amount:
        # A NULL discount makes line_total NULL; SUM would skip the line.
        expr: COALESCE(SUM(IF(order_line_amount_amount IS NULL, 1, 0)), 0) = 0
        over: output
        severity: error
        description: Every order line carries the amount charged.

An output check is a SQL expression over one output that Daana evaluates after creating the outputs with daana dar apply. This check counts lines without an amount and passes when there are none. A missing discount makes line_total NULL, and SUM skips such a line. The check runs on the line fact because a check on the daily output would not find the missing line: the day still has the amounts of its other lines.

With severity error, a failed check makes the command exit with status 1. The ledger, which records the active DAR revision, keeps the previously active revision. The new views are already published. See What a failed check leaves behind and Go back to the last good outputs.

The optional section See the check fail on a missing discount shows this failure. It is not needed to continue. Confirm the check's condition on the supplied data:

Run in DuckDB

SELECT count(*) AS lines_without_amount
FROM daana_dar.fct__order_line
WHERE order_line_amount_amount IS NULL;

Expected output

lines_without_amount
0

Every line has an amount, so the data passes the check, as the apply result reported.

Grain and source

The declaration promises one row per day and currency. Count pairs that occur more than once:

Run in DuckDB

SELECT count(*) AS duplicate_groups
FROM (
  SELECT received_date, order_line_amount_currency
  FROM daana_dar.fct__order_line_daily_revenue
  GROUP BY received_date, order_line_amount_currency
  HAVING count(*) > 1
) duplicates;

Expected output

duplicate_groups
0

No pair occurs twice, so the output has its declared grain. Compare 6 November with the source independently of Daana.

The source's ingested_at gives the load time whose day the report calls received_date. Its line_total gives the amount. The main project's mapping assigns USD to every amount, so use that constant:

Run in DuckDB

SELECT 'source' AS layer, 'USD' AS currency,
       CAST(sum(l.line_total) AS DECIMAL(10,2)) AS amount
FROM stage.sales_order_lines l
WHERE CAST(timezone('UTC', l.ingested_at) AS DATE) = DATE '2024-11-06'
UNION ALL
SELECT 'report', order_line_amount_currency,
       CAST(daily_revenue_amount AS DECIMAL(10,2))
FROM daana_dar.fct__order_line_daily_revenue
WHERE received_date = DATE '2024-11-06'
ORDER BY layer;

Expected output

layercurrencyamount
reportUSD82.59
sourceUSD82.59

Both rows show 82.59 USD. The output agrees with the source at its declared grain for this day. The comparison is a baseline for one day, not a proof for every later load.

After applying the daily output, reading its check, and comparing its grain and one day with the source, trace one order line to its load and extend the output.

Trace one order line to its load

Order 6 was placed on 6 November 2024. Trace its line 2 from the source table to the run that loaded it.

Before you continue

On which day does the daily output count line 2 of order 6, and is that the day its order was placed? Write down the prediction and the reason for it.

Query the line together with its order header:

Run in DuckDB

SELECT l.order_id, l.order_line_id, l.quantity, l.unit_price, l.discount_amount, l.tax_amount, l.line_total,
       timezone('UTC', o.order_date) AS order_date, timezone('UTC', l.ingested_at) AS ingested_at
FROM stage.sales_order_lines l
JOIN stage.sales_orders o USING (order_id)
WHERE l.order_id = 6 AND l.order_line_id = 2;

Expected output

order_idorder_line_idquantityunit_pricediscount_amounttax_amountline_totalorder_dateingested_at
62125.996.001.7521.742024-11-06 13:20:002024-11-06 13:40:00

The line counts on 6 November, its order's day. It was received at 13:40, twenty minutes after the order, so its fact_ts is 13:40. Both timestamps fall on 6 November, so the received day matches the order's day. A later load time does not always mean a later day.

The quantity times unit price, minus discount, plus tax gives 21.74. Check whether every line was received on its order's day:

Run in DuckDB

SELECT count(*) AS lines,
       count(*) FILTER (WHERE CAST(timezone('UTC', l.ingested_at) AS DATE)
                           <> CAST(timezone('UTC', o.order_date) AS DATE)) AS on_another_day,
       min(timezone('UTC', l.ingested_at) - timezone('UTC', o.order_date)) AS shortest_delay,
       max(timezone('UTC', l.ingested_at) - timezone('UTC', o.order_date)) AS longest_delay
FROM stage.sales_order_lines l
JOIN stage.sales_orders o USING (order_id);

Expected output

lineson_another_dayshortest_delaylongest_delay
31000:20:0000:20:00

All 31 lines were received twenty minutes after their orders, on the same day, so the received days also match the order days in this project. Compare line 6-2's source amount with the line fact:

Run in DuckDB

SELECT 'source' AS layer, CAST(line_total AS DECIMAL(10,2)) AS amount
FROM stage.sales_order_lines
WHERE order_id = 6 AND order_line_id = 2
UNION ALL
SELECT 'reporting fact', CAST(order_line_amount_amount AS DECIMAL(10,2))
FROM daana_dar.fct__order_line
WHERE order_line_key = '6-2'
ORDER BY layer;

Expected output

layeramount
reporting fact21.74
source21.74

The amounts agree. The warehouse row carries data_key for the pipeline and inst_key for its run. Start at the stored amount in daana_dw.order_line_desc, then join to daana_metadata.batch_history and daana_metadata.batch_history_sql. Select the amount's rows by their name in daana_metadata.atom_contx_desc, so that the query needs no numeric ID:

Run in DuckDB

SELECT DISTINCT d.order_line_key,
       CAST(d.val_num AS DECIMAL(10,2)) AS amount, d.uom AS currency,
       bh.source_table, bh.row_st,
       bs.executed_sql LIKE '%INSERT%' AS has_insert
FROM daana_dw.order_line_desc d
JOIN daana_metadata.atom_contx_desc ac
  ON CAST(ac.atom_contx_key AS BIGINT) = d.type_key
JOIN daana_metadata.batch_history_sql bs
  ON bs.proc_key = d.data_key AND bs.procinst_key = d.inst_key
JOIN daana_metadata.batch_history bh
  ON bh.proc_key = bs.proc_key AND bh.procinst_key = bs.procinst_key
WHERE d.order_line_key = '6-2'
  AND ac.val_str = 'order_line_order_line_amount';

Expected output

order_line_keyamountcurrencysource_tablerow_sthas_insert
6-221.74USDsales_order_linesCtrue

The run completed with C after reading sales_order_lines. The stored amount and currency form one group: 21.74 USD. Its recorded SQL has an INSERT. To read the statement, select bs.executed_sql in place of the select list, and look for line_total and stage.sales_order_lines in it.

Daana records operational lineage: every row joins to the pipeline run and SQL that produced it. This is evidence for the contributing load, not an automatic lineage graph for every report transformation. See Operational lineage.

Add the line count

Operations also needs the number of lines received per day and currency, beside the amount.

Exercise

Add daily_line_count to daily_revenue in dar/order_line.yaml, keeping its grain and received day. Count every line, whatever its amount, rather than orders or units. Before editing, choose what to count and explain why. Change only this document, and try before opening a correct answer.

  • The output still has one row per day and currency: the repeated-pairs query in "Grain and source" returns 0.
  • For 6 November 2024, USD has 3 lines and the amount 82.59.
  • Over all days, USD has 16 days, 31 lines, and the amount 762.88.
  • The output check stays in place, and daana dar check and daana dar apply both succeed.

Hint: Add an entry under aggregates, after daily_revenue_amount and its ai_context, at the same indentation. The entry's expr names a line fact column, and agg names the function.

Show a correct answer

A correct answer

Under aggregates of daily_revenue, keep daily_revenue_amount and add this entry after its ai_context, at the same indentation as daily_revenue_amount:

          daily_line_count:
            expr: order_line_key
            agg: count
            description: Number of order lines received that day

The key is the line fact's grain: one key per line, never missing. Counting it counts lines whatever their amounts. Counting order_line_amount_amount also meets the displayed criteria while the output check holds, because every line has an amount. In the side project bookstore-broken, counting amounts gives 2 for the day and 30 overall.

An order can have several lines, so a count of orders is not a count of lines. A count of units sums quantities, and the line fact has no quantity column. Placing the entry alongside aggregates instead of inside it makes the check report expected string, got object. The apply does not run, so no view changes.

Verify your change

Quit DuckDB, then check and apply the documents:

Run in terminal

daana dar check dar/ && daana dar apply dar/

The commands again report three documents, five outputs, and eleven views after one check. Reopen DuckDB and check the requested day:

Run in DuckDB

SELECT received_date, order_line_amount_currency AS currency, daily_line_count,
       CAST(daily_revenue_amount AS DECIMAL(10,2)) AS amount
FROM daana_dar.fct__order_line_daily_revenue
WHERE received_date = DATE '2024-11-06';

Expected output

received_datecurrencydaily_line_countamount
2024-11-06USD382.59

Three lines and 82.59 USD meet the day's criterion. Check all days:

Run in DuckDB

SELECT order_line_amount_currency AS currency, count(*) AS reporting_days,
       sum(daily_line_count) AS received_lines,
       CAST(sum(daily_revenue_amount) AS DECIMAL(10,2)) AS amount
FROM daana_dar.fct__order_line_daily_revenue
GROUP BY order_line_amount_currency;

Expected output

currencyreporting_daysreceived_linesamount
USD1631762.88

The 31 lines meet the total criterion, with the original 16 days and 762.88 USD. Count the source lines independently:

Run in DuckDB

SELECT count(*) AS source_lines FROM stage.sales_order_lines;

Expected output

source_lines
31

The source also holds 31 lines, so the counts agree. Run the repeated-pairs query from "Grain and source" again. It still returns 0.

You chose what to count while keeping the output's grain and time basis, and checked the count against the source.

What Daana did

  • Daana created eleven views from five outputs, as the apply results show.
  • Daana generated the SQL that sums the amounts and counts the lines for each received day and currency. Over all 16 days, the output holds 31 lines and 762.88 USD.
  • Daana ran the output check after each apply, reported as after 1 checks.
  • Daana recorded the load and its SQL, as line 6-2's trace shows.

The new entry changed the reporting requirement. The model, mappings, and loaded warehouse stayed unchanged. Quit DuckDB.

Both optional sections below start in bookstore, with DuckDB closed. The first one uses the line count that you added in the last task.

Optional: A missing amount

The source calculation shows what can make an amount missing. Inspect the definition of line_total in data/seed.sql:

    discount_amount NUMERIC(10,2) DEFAULT 0,
    tax_amount NUMERIC(10,2) DEFAULT 0,
    line_total NUMERIC(12,2) GENERATED ALWAYS AS (
        (quantity * unit_price) - discount_amount + COALESCE(tax_amount, 0)
    ) VIRTUAL,

NULL means a missing value. COALESCE replaces a missing tax with zero, so the line still has its amount before tax. A missing discount makes line_total NULL, because the subtraction has a missing operand.

See the check fail on a missing discount

Testing the failure in a side project keeps the main project's data unchanged. Inspect data/ch7-missing-discount.sql, where the order system overwrites the line in place, as chapter 5 described:

UPDATE stage.sales_order_lines SET discount_amount = NULL WHERE order_id = 6 AND order_line_id = 2;

Copy the main project into a side project, load the data file there, execute, and apply the documents:

Run in terminal

cp -R ../bookstore ../bookstore-broken
cd ../bookstore-broken
daana init . --example bookstore --load-data ch7-missing-discount
daana execute && daana dar apply dar/

Expected output of daana dar apply

Error: dar apply: data-quality check "every_line_has_an_amount" failed for entity:order_line output "fct__order_line": the new outputs are already published in daana_dar; the ledger was not updated and keeps the previously active DAR revision, if any — fix the data or the check and apply again

Daana had already replaced the views, so a failed check does not stop the wrong number from being published.

In this side project, the data needs a fix before applying again.

From bookstore-broken, reopen DuckDB. Read order 6's lines in the line fact:

Run in DuckDB

SELECT order_line_key,
       CAST(order_line_amount_amount AS DECIMAL(10,2)) AS order_line_amount_amount
FROM daana_dar.fct__order_line
WHERE order_line_key LIKE '6-%'
ORDER BY order_line_key;

Expected output

order_line_keyorder_line_amount_amount
6-134.76
6-2NULL
6-326.09

Line 6-2 has no amount, and that missing value is what the output check counts. The counts below come from an entry that counts order_line_key. If your entry counts amounts, it gives 2 for the day and 30 overall. Read the side project's daily output:

Run in DuckDB

SELECT count(*) AS days,
       count(*) FILTER (WHERE daily_revenue_amount IS NULL) AS days_without_amount,
       CAST(sum(daily_revenue_amount) AS DECIMAL(10,2)) AS amount,
       sum(daily_line_count) AS received_lines,
       CAST(max(daily_revenue_amount) FILTER (WHERE received_date = DATE '2024-11-06') AS DECIMAL(10,2)) AS amount_2024_11_06,
       max(daily_line_count) FILTER (WHERE received_date = DATE '2024-11-06') AS lines_2024_11_06
FROM daana_dar.fct__order_line_daily_revenue;

Expected output

daysdays_without_amountamountreceived_linesamount_2024_11_06lines_2024_11_06
160741.143160.853

The day shows 60.85, which is 82.59 without the line's 21.74. All 16 days have amounts, totaling 741.14 instead of 762.88. SUM skips the missing amount, so the query succeeds. The line count still gives 3 lines that day and 31 overall.

The output check detects the missing line because it counts missing amounts at the line grain. A check for missing daily amounts would pass.

Daana stored the change as a correction, because the unchanged ingested_at kept the line's effective timestamp. The history view has one row for line 6-2, without an amount. The earlier amount stays in the stored rows.

Quit DuckDB, then return to the main project:

Run in terminal

cd ../bookstore

The main project's data and views are unchanged.

Optional: Revenue by loyalty tier when each line was received

A customer's current tier can differ from the tier they had when they ordered, which changes how revenue is grouped. An order line reaches its customer through its order: the line is_part_of an order, which is_placed_by a customer.

The customer declaration in dar/customer.yaml creates dim__customer, with one row per customer version. Its latest: view adds dim__customer__latest with each customer's current version. Find these lines in dar/order_line.yaml:

relationships:
  order:    {changes: never}          # a line stays on the order it was placed on
  customer: {via: order}              # the customer who placed that order (dar/order.yaml)
  # ...
    keys: [order, customer]           # the line's order and that order's customer
    wide:                             # compiles to fct__order_line__wide
      dims:
        customer: [customer_name, customer_loyalty_tier]   # from dim__customer, as of fact_ts

keys adds order_key and customer_key to fct__order_line. Both relationships are changes: never: a line has one order and an order one customer, so DAR reads the order from the line's latest link and the customer from that order's latest link, whatever fact_ts is. DAR accepts changes: never only because the model declares both relationships MANY_TO_ONE (see Relationship cardinality). wide creates fct__order_line__wide, which joins each line to the dim__customer version valid at fact_ts.

The join uses fact_ts, the time each line was received. In this data, every line was received twenty minutes after its order, so the join reads the tier twenty minutes after the order, on the order's day. Reopen DuckDB.

Customer validity intervals

The wide fact selects a customer version by its validity interval, from valid_from through valid_to, including both endpoints. List the versions of Emily Johnson, customer 2, in dim__customer:

Run in DuckDB

SELECT customer_key, valid_from, valid_to, customer_city, customer_loyalty_tier
FROM daana_dar.dim__customer
WHERE customer_key = '2'
ORDER BY valid_from;

Expected output

customer_keyvalid_fromvalid_tocustomer_citycustomer_loyalty_tier
22023-02-14 11:05:402024-10-31 23:59:59.999999NULLNULL
22024-11-01 00:00:002024-11-04 23:59:59.999999NULLSilver
22024-11-05 00:00:002024-11-28 23:59:59.999999BostonSilver
22024-11-29 00:00:009999-12-31 23:59:59PhiladelphiaSilver

Each interval ends one microsecond before the next begins, so the intervals do not overlap. The current version ends at 9999-12-31 23:59:59, and the NULL values mark attributes not yet available. Emily's tier started with her loyalty membership on 1 November; her later move changed her city while her tier stayed Silver. The source holds only the current tier, dated by the membership's join date.

Compare revenue by tier

Grouping the same lines by historical and current tiers shows whether the choice of customer version changes the amounts assigned to each tier.

Before you continue

Whose orders will move to another tier if the report uses the tier when each line was received? Write down a prediction, considering customers who joined the loyalty program after ordering.

Group revenue by customer values at fact_ts:

Run in DuckDB

SELECT coalesce(customer_loyalty_tier, '(no tier)') AS tier_when_received,
       count(DISTINCT customer_key) AS customers,
       count(*) AS order_lines,
       CAST(sum(order_line_amount_amount) AS DECIMAL(10,2)) AS revenue
FROM daana_dar.fct__order_line__wide
GROUP BY 1
ORDER BY revenue DESC;

Expected output

tier_when_receivedcustomersorder_linesrevenue
Bronze39208.04
Gold27156.53
Silver35151.63
Platinum15139.12
(no tier)25107.56

The groups sum to 762.88, matching the daily output because every line counts once. Group those same lines by current tier:

Run in DuckDB

SELECT coalesce(c.customer_loyalty_tier, '(no tier)') AS current_tier,
       count(DISTINCT w.customer_key) AS customers,
       count(*) AS order_lines,
       CAST(sum(w.order_line_amount_amount) AS DECIMAL(10,2)) AS revenue
FROM daana_dar.fct__order_line__wide w
JOIN daana_dar.dim__customer__latest c USING (customer_key)
GROUP BY 1
ORDER BY revenue DESC;

Expected output

current_tiercustomersorder_linesrevenue
Bronze39208.04
Silver36171.15
Gold27156.53
Platinum15139.12
(no tier)1488.04

Silver increases by 19.52 and (no tier) decreases by the same amount, so lines with 19.52 of revenue were received while their customer had no tier. Query the orders whose historical and current tiers differ:

Run in DuckDB

SELECT w.order_key, w.customer_name, CAST(min(w.fact_ts) AS DATE) AS received_date,
       coalesce(w.customer_loyalty_tier, '(no tier)') AS tier_when_received,
       coalesce(c.customer_loyalty_tier, '(no tier)') AS current_tier,
       CAST(sum(w.order_line_amount_amount) AS DECIMAL(10,2)) AS revenue
FROM daana_dar.fct__order_line__wide w
JOIN daana_dar.dim__customer__latest c USING (customer_key)
WHERE w.customer_loyalty_tier IS DISTINCT FROM c.customer_loyalty_tier
GROUP BY ALL
ORDER BY received_date;

Expected output

order_keycustomer_namereceived_datetier_when_receivedcurrent_tierrevenue
5David Wilson2024-11-05(no tier)Silver19.52

David Wilson's order 5 moves from Silver to (no tier) in the historical result. He ordered on 5 November and joined as Silver on 11 November, so his 19.52 belongs to (no tier) when the lines were received.

If you predicted no change, you applied David's current tier to an order before he joined. Emily's move changed her city; her tier was already Silver before her first order.

Daana resolved each line's customer through the order and joined the customer version valid at fact_ts. Quit DuckDB.

What you can do now

You can read and apply a reporting output, verify it against the source, trace a row to its load, and add a measure. The tutorial is complete. You decided what the data means and checked each result against the source, and Daana generated the warehouse and reporting SQL. To look up a key of this chapter's declarations later, see the DAR document reference.

Check a number at its own grain, because a total can hide a missing value.

Chapters 8, 9, and 10 go further. Chapter 8 compares the amounts on the lines received each day with revenue by order date. Chapter 9 adds reader profiles that share an email address with the CRM. Chapter 10 runs the project against a second target and generates SQLX files for Dataform from the DAR documents.