Loading...
Loading...
Use when creating or modifying dbt Semantic Layer components including semantic models, metrics, and dimensions leveraging MetricFlow.
npx skill4agent add dbt-labs/dbt-agent-skills building-dbt-semantic-layer[!NOTE] This skill contains guidance for the new dbt semantic layer YAML spec, valid for dbt 1.12.0 and above. If the user is using a different version of dbt, you can use the migration guide to help them migrate to the new spec and add new components to their semantic layer. Ask the user if they want to migrate to the new spec before proceeding.
customerssemantic_model:enabled: trueagg_time_dimensionmodels:
- name: orders
semantic_model:
enabled: true # enable the semantic model
agg_time_dimension: ordered_at # set the primary time column (this is a column in the dbt model)_identity: \n\t type: primaryentity: type: foreignmodels:
- name: orders
semantic_model:
enabled: true # enable the semantic model
agg_time_dimension: ordered_at # set the primary time column (this is a column in the dbt model)
columns:
- name: order_id # this is the primary key column of the model
entity:
type: primary
name: order
- name: customer_id # this is a foreign key column of the model
entity:
type: foreign
name: customerdimension: type: timegranularitydimension: type: categoricalmodels:
- name: orders
semantic_model:
enabled: true # enable the semantic model
agg_time_dimension: ordered_at # set the primary time column (this is a column in the dbt model)
columns:
- name: order_id
entity:
type: primary
name: order
- name: customer_id
entity:
type: foreign
name: customer
- name: ordered_at
granularity: day # set the granularity of the time column
dimension:
type: time
- name: order_status
dimension:
type: categoricalsimplederivedcumulativeconversionratiomodels:
- name: orders
semantic_model:
enabled: true # enable the semantic model
agg_time_dimension: ordered_at # set the primary time column (this is a column in the dbt model)
columns:
- name: order_id
entity:
type: primary
name: order
- name: customer_id
entity:
type: foreign
name: customer
- name: ordered_at
granularity: day # set the granularity of the time column
dimension:
type: time
- name: order_status
dimension:
type: categorical
metrics:
- name: order_count
type: simple
agg: count
expr: 1
- name: total_revenue
type: simple
agg: sum
expr: amount
- name: average_order_value
type: simple
agg: average
expr: amountdbt parsedbt sl validatemf validate-configssemantic_model:derived_semantics derived_semantics:
dimensions:
- name: order_size_bucket
type: categorical
expr: case when amount > 100 then 'large' else 'small' end
label: "Order Size"
entities:
- name: order_customer_key
type: foreign
expr: "order_id || '-' || customer_id"metricsmodels.metricsmetrics - name: revenue_per_order
type: derived
description: Average revenue per order
label: Revenue per Order
expr: total_revenue / total_orders
input_metrics:
- name: total_revenue
- name: total_orders
# With offset window
- name: revenue_growth
type: derived
expr: total_revenue - revenue_last_week
input_metrics:
- name: total_revenue
- name: total_revenue
alias: revenue_last_week
offset_window: 1 week
filter: "{{ Dimension('order__status') }} = 'completed'" - name: cumulative_revenue
type: cumulative
description: Running total of revenue
label: Cumulative Revenue
input_metric: total_revenue
grain_to_date: week
period_agg: first
# With window
- name: trailing_7d_revenue
type: cumulative
input_metric: total_revenue
window: 7 days - name: conversion_rate
type: ratio
description: Orders divided by visits
label: Conversion Rate
numerator: total_orders
denominator: total_visits
# With filters
- name: premium_conversion_rate
type: ratio
numerator:
name: total_orders
filter: "{{ Dimension('order__customer_segment') }} = 'premium'"
alias: premium_orders
denominator: total_visits - name: signup_to_purchase
type: conversion
description: Rate of signups converting to purchase
label: Signup to Purchase
entity: customer
calculation: conversion_rate
base_metric: signups
conversion_metric: purchases
window: 7 days
constant_properties:
- base_property: signup_channel
conversion_property: purchase_channel# For metrics depending on multiple semantic models
metrics:
- name: cross_model_ratio
type: ratio
numerator:
name: metric_from_model_a
filter: "{{ Dimension('entity__dim') }} > 10"
denominator:
name: metric_from_model_b
config:
group: example_group
tags:
- example_tag
meta:
owner: "@someone"semantic_model:enabled: trueagg_time_dimension:semantic_modelentity:dimension:granularity:metrics:metrics:entity: primarylabelday| Pitfall | Fix |
|---|---|
Missing | Every semantic model needs a default time dimension |
| Must be at column level |
| Defining a column as both an entity and a dimension | A column can only be one or the other |
Simple metrics in top-level | Top-level is only for cross-model metrics |
Using | Cumulative metrics can only have one |
Missing | Must list metrics used in |