Guides

Daana next to an existing stack

A team with an existing dbt or Dataform project and a semantic layer can introduce Daana for one metric and keep everything else. This guide describes what the team keeps, what Daana generates and what the semantic layer still owns. The example is the bookstore's daily revenue from chapter 7, with chapter 8's correction, which dates each amount by its order's date.

What has been verified. The Daana steps on this page ran on the bookstore's local DuckDB file. On BigQuery, daana dar generate writes SQLX files for Dataform; for this guide, those files were generated locally and were not run in Dataform. No integration with dbt or with a semantic layer product has been verified. This guide names none and describes only the boundary between them and Daana.

Who owns what

Part of the stackOwnerNotes
Ingestion of source tablesThe team's existing toolingDaana reads the tables it finds; in the bookstore they are stage.*
Existing dbt or Dataform modelsThe teamThey keep running. Daana does not read, change or replace them
Business model: entities, attributes, relationships, historyDaana (DAB)Declared in dab/model.yaml and the mappings; deployed as tables and view_* views
Reporting outputs: dimensions and factsDaana (DAR)Declared in dar/; created by daana dar apply, or generated as SQLX for Dataform on BigQuery
Scheduling and orchestrationThe team's schedulerIt runs daana execute, and Dataform for the generated SQLX
Metric exposure, BI access, naming for business usersThe semantic layerIt reads the DAR fact instead of deriving the metric from source tables

Daana's outputs are ordinary tables and views in the target database. Any tool that reads the target database can read them.

The metric both sides must agree on

A daily revenue metric depends on four decisions. The bookstore's DAR declaration makes each of them in one place:

DecisionIn the bookstoreWhere it is declared
AmountThe amount charged per order line, tax included, discounts subtractedorder_line_amount_amount in the order line mapping
Event timeThe day the order was placed, not the day the row was loadedThe fact's axis in dar/order_line.yaml: order_line_purchase_ts
GrainOne row per day and currencyThe daily revenue output's grain
CurrencyPart of the grain, so amounts in different currencies are summed separatelyorder_line_amount_currency in the grain

Find the expression for the order line amount in the mapping:

          - id: order_line_amount_amount            # child attribute of the order_line_amount group
            # Business rule: amount charged = quantity * unit_price - discount + tax,
            # taken from the source line total (tax included)
            transformation_expression: line_total

Compare the fact's axis in the ch9 checkpoint with the event-time decision above:

    axis:
      expr: order_line_purchase_ts    # the order's order_date; eff_tmstp is when the line landed
      description: When the order the line is on was placed (event time, not load time)

Use this axis for an order-day metric. The declaration in the ch8 checkpoint's dar/order_line.yaml takes its axis from eff_tmstp, which the order line mapping sets to each line's load time. The load date matches the order date only when lines arrive on the day of their order.

With chapter 8's late order lines, the daily revenue from that declaration has 18 days, although orders were placed on 16. Chapter 8's comparison per day finds six days that differ, and both totals are still 762.88. With order_line_purchase_ts, the daily revenue has 16 days again, and that comparison finds no difference. Fix it in chapter 8 makes this change.

The grain and currency are the same in both declarations. The website's semantic layer page explains why amount, grain, event time and currency belong together in one definition.

Start with one metric

  1. Deploy the DAB beside the existing models. Use the same source tables, and deploy into separate schemas (daana_dw, daana_stage, daana_metadata by default). See Going to production for connection profiles and project declarations per target.
  2. Create the DAR outputs. Run daana dar apply on the deployed DAB (Apply DAR on a deployed DAB, verified on DuckDB). For Dataform on BigQuery, run daana dar generate instead (Generate DAR for Dataform). Use dar/order_line.yaml from the ch9 checkpoint for Dataform. This is the declaration that Fix it in chapter 8 produces, and it has no output check. daana dar generate refuses a document with a checks entry and reports checks for sqlx backend are not implemented. The line-level check of the finished chapter 7 file therefore runs only with daana dar apply. Chapter 7's file uses the load time on its axis because its daily output counts the lines received each day. On BigQuery, place the generated SQLX in its own definitions directory in the existing Dataform repository, apart from hand-written SQLX.
  3. Reconcile the existing metric with the DAR fact, day by day, as in the next section.
  4. Point the semantic layer's metric at the fact. For the bookstore, that is daana_dar.fct__order_line_daily_revenue, column daily_revenue_amount. The existing model that computed the metric can be retired once the two agree, or kept for comparison.

Other metrics and models stay as they are. A second metric follows the same four steps.

Reconcile the metric

Compare the existing calculation with the DAR fact before switching, at the grain of the metric: per order date and currency. Aggregate both sides by order date and currency, then join them on both fields with a FULL JOIN. Treat a pair as failing if one side lacks it or its amounts differ.

An INNER JOIN would drop the pairs that only one side has, so the query uses a FULL JOIN and counts those pairs separately. A comparison of the amounts does not find such a pair when its amount is NULL, because the missing side is NULL as well.

The queries of this section and their outputs were recorded read-only on a bookstore DuckDB file with the full model of chapter 6, after daana dar apply dar/ --connection duckdb with the ch8 checkpoint's dar/order_line.yaml, which created 4 views and ran no checks. That file's daily output, like the ch9 checkpoint's, names its day order_date, while chapter 7's names it received_date.

The reconciliation below was run on three projects: the unchanged one, a copy that labels every amount EUR, and a copy with a missing discount on a line labeled EUR. Each run was repeated on an executed chapter 5 project with the ch9 checkpoint's dar/order_line.yaml, in a UTC session and a Europe/Stockholm session. Every run gave the results shown below.

In the bookstore, the existing subquery stands for an existing model that sums line_total by order date and currency. Reconcile it with the DAR fact:

WITH existing AS (
  SELECT CAST(timezone('UTC', o.order_date) AS DATE) AS order_date, o.currency, sum(l.line_total) AS revenue
  FROM stage.sales_order_lines l
  JOIN stage.sales_orders o ON o.order_id = l.order_id
  GROUP BY 1, 2
),
daana AS (
  SELECT CAST(order_date AS DATE) AS order_date, order_line_amount_currency AS currency,
         sum(daily_revenue_amount) AS revenue
  FROM daana_dar.fct__order_line_daily_revenue
  GROUP BY 1, 2
)
SELECT count(DISTINCT coalesce(e.order_date, d.order_date)) AS days,
       count(*) AS date_currency_pairs,
       count(*) FILTER (WHERE e.order_date IS NULL OR d.order_date IS NULL) AS unmatched_pairs,
       count(*) FILTER (WHERE e.order_date IS NULL OR d.order_date IS NULL
                           OR e.revenue IS DISTINCT FROM d.revenue) AS failing_pairs,
       CAST(sum(e.revenue) AS DECIMAL(10,2)) AS existing_total,
       CAST(sum(d.revenue) AS DECIMAL(10,2)) AS daana_total,
       CASE WHEN count(*) FILTER (WHERE e.order_date IS NULL OR d.order_date IS NULL
                                     OR e.revenue IS DISTINCT FROM d.revenue) = 0
            THEN 'pass' ELSE 'fail' END AS result
FROM existing e
FULL JOIN daana d ON d.order_date = e.order_date AND d.currency = e.currency;
daysdate_currency_pairsunmatched_pairsfailing_pairsexisting_totaldaana_totalresult
161600762.88762.88pass

The existing model and the DAR fact give the same result on all 16 days.

Matching totals do not prove that the currency is right. In a copy of the project, the order line mapping labels every amount 'EUR' instead of 'USD'. On that copy, built the same way, a comparison by date alone finds no difference, and the totals match:

WITH existing AS (
  SELECT CAST(timezone('UTC', o.order_date) AS DATE) AS order_date, sum(l.line_total) AS revenue
  FROM stage.sales_order_lines l
  JOIN stage.sales_orders o ON o.order_id = l.order_id
  GROUP BY 1
),
daana AS (
  SELECT CAST(order_date AS DATE) AS order_date, sum(daily_revenue_amount) AS revenue
  FROM daana_dar.fct__order_line_daily_revenue
  GROUP BY 1
)
SELECT count(*) AS days,
       count(*) FILTER (WHERE e.revenue IS DISTINCT FROM d.revenue) AS days_that_differ,
       CAST(sum(e.revenue) AS DECIMAL(10,2)) AS existing_total,
       CAST(sum(d.revenue) AS DECIMAL(10,2)) AS daana_total
FROM existing e
FULL JOIN daana d ON d.order_date = e.order_date;
daysdays_that_differexisting_totaldaana_total
160762.88762.88

The reconciliation by date and currency fails on the same copy. Each USD pair in the existing model has no matching pair in the DAR fact, and each EUR pair in the fact has no match in the model:

daysdate_currency_pairsunmatched_pairsfailing_pairsexisting_totaldaana_totalresult
16323232762.88762.88fail

A missing pair can have a NULL amount. A second copy loads chapter 7's missing discount with daana init . --example bookstore --load-data ch7-missing-discount. Its order line mapping labels only the line with the missing discount, order 6 line 2, 'EUR'. The DAR fact has one extra pair, 2024-11-06, EUR, with NULL revenue because the line's amount is NULL.

The existing model has no EUR pair. Both totals are 741.14, and a comparison of the amounts alone finds no difference on the extra pair. On this copy, the reconciliation by date and currency reports the extra pair as a failure:

daysdate_currency_pairsunmatched_pairsfailing_pairsexisting_totaldaana_totalresult
161711741.14741.14fail

When the existing metric uses a different definition, the totals differ. The size of the difference shows which decision differs. For example, an existing metric defined as quantity times unit price gives 735.66. Sum the parts of the amount charged to see what that definition leaves out:

SELECT round(sum(quantity * unit_price), 2) AS gross,
       round(sum(discount_amount), 2) AS discount,
       round(sum(tax_amount), 2) AS tax,
       round(sum(line_total), 2) AS line_total
FROM stage.sales_order_lines;
grossdiscounttaxline_total
735.6634.8062.02762.88

Gross 735.66 minus 34.80 discount plus 62.02 tax is 762.88. Decide which amount the business means by revenue. If it is not the amount charged, change the mapping or the declaration and reconcile again. Revenue before tax is 700.86 for the same lines, with order_line_tax_amount left out.