02. Metrics and Semantic Layer
Metrics and Semantic Layer in dbt
The semantic layer defines consistent business metrics across your organization, ensuring that all teams use the same definitions.
dbt enables you to centralize metrics and connect them to models and dashboards for trusted analytics.
What are Metrics?
- Predefined calculations used in dashboards and reports
- Examples:
total_revenue→ sum oforder_amountactive_customers→ count of uniquecustomer_idin the last 30 days
- Stored in dbt YAML files for consistency
YAML Example:
version: 2
metrics:
- name: total_revenue
model: ref('orders')
label: "Total Revenue"
description: "Sum of all order amounts"
type: sum
sql: order_amount
dimensions:
- order_date
- region
model→ dbt model containing the source datatype→ aggregation type (sum,count,average)dimensions→ attributes to slice metrics
Semantic Layer Benefits
- Provides a single source of truth for business metrics
- Reduces metric discrepancies across dashboards
- Simplifies analyst workflows by predefining calculations
- Enables metric-driven testing in dbt
Best Practices
Define metrics in YAML centrally
Link metrics to specific dbt models for traceability
Use dimensions to slice metrics consistently
Validate metrics using dbt tests
Integrate metrics into BI tools via semantic layer connectors