Skip to main content

Extending dbt Models

Step-by-step guide to adding a new Gold model to Konsolidat.

Adding a Gold Model​

1. Create the SQL File​

Create dbt_project/models/gold/gold_your_model.sql:

{{
config(
engine='MergeTree()',
order_by='tuple()'
)
}}

select
tb.data_area_id,
tb.fiscal_year,
tb.fiscal_period,
tb.main_account,
{{ dim_select('tb.', trailing=true) }}
{{ measure_passthrough('tb.') }}
from {{ ref('gold_trial_balance') }} as tb
where tb.fiscal_period between 1 and 12

Key points:

  • Materialization, schema and the gold tag come from dbt_project.yml (models.open_epm.gold); set engine and order_by in the model, or use **epm_config(order_by=...).
  • Use dim_select() / dim_group_by() / dim_partition_by() with trailing=true or leading=true instead of a literal comma next to them: a site may declare no dimensions (konsol#230), and a dangling comma then breaks the SQL (konsolidat#220).
  • Use measure_passthrough() / measure_select() for the standard measures.
  • Reference upstream models with {{ ref('model_name') }} and konsol-owned tables with {{ source('epm_staging', '...') }} or {{ source('epm_gold', '...') }}.
  • A ClickHouse LEFT JOIN fills a miss with the column default ('', 0, 1970-01-01), not NULL. Test for the sentinel, never is null.
  • Amounts: cast_to_decimal128(expr, 2) rounds; tolerances are {{ materiality_floor() }}.
Dimensions and measures are registry-driven

dim_select(), measure_select() and the other helpers read the dimensions and base_measures vars in dbt_project.yml. That block is generated from the Dimension and Measure doctypes in konsol (konsol.dbt_config.regenerate_vars()), between the konsol-managed vars markers. To add or change a dimension or measure, edit the doctype and publish; do not hand-edit the block.

2. Add YAML Documentation​

Add an entry to dbt_project/models/gold/_gold__models.yml:

- name: gold_your_model
description: "What this model computes, and from what"
columns:
- name: data_area_id
description: "Legal entity"
data_tests:
- not_null

3. Add a Test, Red First​

Create dbt_project/tests/assert_your_condition.sql and run it before changing anything else: it must fail on the current state, then pass after your model lands. See Testing Guide.

{# konsolidat#NNN: the rule this holds #}
select *
from {{ ref('gold_your_model') }}
where some_condition_that_should_never_be_true

4. Build and Test​

dbt run --select gold_your_model
dbt test --select gold_your_model
dbt build --select gold_your_model+

A full no-var build must stay byte-for-byte unchanged by anything opt-in you add (run-scope filters, cluster macros).

5. Register for Governed Builds​

Every gold model carries a domain:<scope> tag in the konsol-managed model domains block of dbt_project.yml. The block is generated from konsol's Build Model doctype, which maps the model to a Build Scope (actuals, consolidation, reporting, scenarios, staging). Create the Build Model doc for your model; scripts/check_gold_domains.py in the product repo fails CI for a gold model without a scope. A scoped governed build (dbt build --select tag:domain:<scope>) only builds tagged models.

6. Run-scope filters (optional)​

If the model is part of a period close, add {{ period_filter() }} and {{ scope_filter() }} inside a where 1 = 1 block, and make it incremental with a pre-hook DELETE on the same predicates (see gold_cash_flow_indirect). Both macros render nothing when no var is set.

Reading Governed Reference Data​

There are no CSV seeds (deleted in konsolidat 85dff3b). Reference data is declared in konsol and written through to ClickHouse; see Reference Data for the doctype-to-table map.

To add a new reference table:

  1. In konsol: the doctype (or field), its _REFERENCE_TABLE_DDL entry in konsol/clickhouse.py, and the sync call in the doctype's hooks (sync_doctype_after_commit). Keep the DDL identical in konsolidat's clickhouse/init-db.sql.
  2. In dbt: declare the table under the epm_staging (or epm_gold) source in models/staging/_staging__sources.yml, with a description that says which doctype writes it.
  3. Read it with {{ source('epm_staging', 'your_table') }}. Filter on status = 'Published' where the table carries one; konsol syncs Published or submitted rows only, but the consumer refuses anything else regardless.
  4. If the two repos may deploy in either order, read a new column only when present (adapter.get_columns_in_relation(...)), as bronze_trial_balance_submissions and silver_main_accounts do.

The sync watermark then covers the table, and assert_staging_not_stale fails the build if it is emptied or missed by a reconcile.

Patterns to Follow​

Aggregation Model​

select
tb.data_area_id,
tb.fiscal_year,
ph.fiscal_quarter,
tb.main_account,
{{ dim_select(prefix='tb.', trailing=true) }}
sum(tb.period_net_amount) as quarter_net_amount
from {{ ref('gold_trial_balance') }} as tb
left join {{ ref('gold_period_hierarchy') }} as ph
on tb.fiscal_period = ph.fiscal_period
group by
tb.data_area_id,
tb.fiscal_year,
ph.fiscal_quarter,
tb.main_account{{ dim_group_by(prefix='tb.', leading=true) }}

Pairing Two Scenarios (e.g. Variance)​

Prefer one stream plus conditional aggregation over a FULL OUTER JOIN: under join_use_nulls = 0 the unmatched side of a join loses its keys.

select
data_area_id, fiscal_year, fiscal_period, main_account,
{{ dim_select(dims=get_budget_dimensions(), trailing=true) }}
sumIf(amount, scenario_type = 'actual') as actual_amount,
if(countIf(scenario_type = 'budget') > 0, sumIf(amount, scenario_type = 'budget'), null) as budget_amount
from typed
group by data_area_id, fiscal_year, fiscal_period, main_account
{{- dim_group_by(dims=get_budget_dimensions(), leading=true) }}

Window Function Model (e.g. YTD)​

select
*,
sum(period_net_amount) over (
partition by data_area_id, fiscal_year, main_account{{ dim_partition_by(leading=true) }}
order by fiscal_period
rows between unbounded preceding and current row
) as ytd_net_amount
from {{ ref('gold_trial_balance') }}

Consolidation Layer (Empty Dims)​

For a non-entity layer of gold_fully_consolidated_tb:

select
consolidation_group,
'' as data_area_id,
fiscal_year,
fiscal_period,
debit_account as main_account,
'IC Elimination' as account_name,
{{ dim_empty_strings(trailing=true) }}
'' as reporting_currency,
debit_elimination as amount,
'ic_elimination' as adjustment_type,
rule_id as journal_id
from {{ ref('gold_ic_eliminations') }}
where elimination_view = 'group'

Sum each layer to the grain assert_journal_grain_unique checks (group, entity, year, period, account, adjustment_type, dimensions), or gold_consolidated_ytd runs a separate running total per row.

Making a Model Queryable from Excel​

The add-in's functions (=K.EPM(), =K.CF(), ...) read a ClickHouse table through the Dataset registry (konsol doctype Dataset, with Dataset Measure and Dataset Dimension child rows). No change to api.py:

  1. Create a Dataset whose clickhouse_table is your model's table.
  2. Add the measures and dimensions it may be queried on.
  3. Publish it. The API resolves the fact argument through the registry and validates measures and the table name against it.

The query builder filters on data_area_id, fiscal_year, fiscal_period, main_account and the declared dimensions. A model keyed differently needs a thin view that renames its keys into those slots: gold_consolidated_fact (group as data_area_id) and gold_cashflow_fact (cf_line_item as main_account) are the two examples.

See Extending the API.

Next Steps​