dbt_simple_metrics
A dbt package that generates aggregated SQL from metric definitions in your model YAML files. Define your metrics once in models.yml, then reuse them across models with a single macro call.
Metric types are compatible with Cube measure types.
Installation
Add to your project's packages.yml:
packages:
- git: "https://github.com/ponderedw/dbt_simple_metrics.git"
revision: v1.0.9
Then run:
dbt deps # or: dbtf deps
dbt version compatibility
| dbt version | Format 1 config.meta.metrics | Format 3 inline MetricFlow |
|---|
| dbt 1.x | Yes | Yes (1.9+) |
| dbt-fusion 2.x | Yes | Yes (v1.0.9+) |
dbt-fusion note: both macros work with inline Format 3 (v1.0.9+). They will not
work if any YAML in the project retains a top-level semantic_models: block — migrate
those to inline Format 3.
Quick Start
1. Define columns and metrics in your model's YAML
Metrics live under config.meta.metrics in your model's schema YAML file (e.g., models/models.yml). Columns defined under columns serve as the available dimensions.
version: 2
models:
- name: course_performance_summary
columns:
- name: course_code
description: "Course code (e.g., CS101, MATH201)"
- name: course_name
description: "Full name of the course"
- name: academic_year
description: "Academic year of the course offering"
- name: avg_grade_points
description: "Average grade points achieved by students"
- name: total_enrollments
description: "Total number of students enrolled"
config:
meta:
metrics:
average_course_gpa:
type: average
label: "Average Course GPA"
sql: ${avg_grade_points}
total_course_enrollments:
type: sum
label: "Total Course Enrollments"
sql: ${total_enrollments}
2. Call the macro from a model
-- models/marts/academic/course_metrics_example.sql
{{ config(materialized='table') }}
{{ dbt_simple_metrics.metrics(
'course_performance_summary',
['course_code', 'course_name', 'academic_year'],
['average_course_gpa', 'total_course_enrollments']
) }}
3. Run dbt
dbt run --select course_metrics_example
Generated SQL:
select
course_code,
course_name,
academic_year,
avg(avg_grade_points) as average_course_gpa,
sum(total_enrollments) as total_course_enrollments
from "education_dw"."public"."course_performance_summary"
group by 1, 2, 3
Macro Reference
dbt_simple_metrics.join_metrics(models, filters=[])
Cross-model metric query with automatic join inference. Select metrics and dimensions from multiple models — if the semantic models share an entity name, the join is generated automatically. If not, a compile-time error tells you exactly which entities are available on each side.
| Parameter | Type | Description |
|---|
models | list[dict] | Each dict: name (model), metrics (list), dimensions (list) |
filters | list[string] | Optional WHERE clauses on the outer query |
Example:
-- models/marts/academic/joined_metrics_example.sql
{{ config(materialized='table') }}
{{ dbt_simple_metrics.join_metrics([
{
'name': 'course_performance_summary',
'metrics': ['total_course_enrollments', 'average_course_gpa'],
'dimensions': ['department_name']
},
{
'name': 'semester_enrollment_trends',
'dimensions': ['semester_name', 'semester_type']
}
]) }}
Generated SQL:
with
a as (
select
semester_id,
department_name,
sum(total_enrollments) as total_course_enrollments,
avg(avg_grade_points) as average_course_gpa
from "education_dw"."public"."course_performance_summary"
group by 1, 2
),
b as (
select
semester_id,
semester_name,
semester_type
from "education_dw"."public"."semester_enrollment_trends"
group by 1, 2, 3
)
select
a.department_name,
a.total_course_enrollments,
a.average_course_gpa,
b.semester_name,
b.semester_type
from a
join b on a.semester_id = b.semester_id
The macro uses one CTE per model that pre-aggregates within that model's scope, then joins the CTEs. This eliminates column ambiguity and ensures metrics are computed on the correct grain before joining.
How joins are inferred:
A join is created between two consecutive models when their semantic model definitions share an entity name. The relationship type is determined by the entity type on each side: