Creating metrics
After building semantic models, it's time to start adding metrics. This page explains the different supported metric types you can add to your dbt project.
Parameters
The keys for metrics parameters are:
Type-specific parameters
Each metric type has additional specific parameters:
- Simple metrics:
agg(required),expr,percentile,percentile_type,non_additive_dimension,agg_time_dimension,join_to_timespine,fill_nulls_with - Cumulative metrics:
input_metric(required),window,grain_to_date,period_agg - Derived metrics:
expr(required),input_metrics(required) - Ratio metrics:
numerator(required),denominator(required) - Conversion metrics:
entity(required),calculation(required),base_metric(required),conversion_metric(required),window,constant_properties
Refer to the following sections about each metric type for detailed information on type-specific parameters.
Example
Here's a complete example of the metrics spec configuration:
📹 Learn about the dbt Semantic Layer with on-demand video courses!
Explore our dbt Semantic Layer on-demand course to learn how to define and query metrics in your dbt project.
Additionally, dive into mini-courses for querying the dbt Semantic Layer in your favorite tools: Tableau, Excel, Hex, and Mode.
Default granularity for metrics
Conversion metrics
Conversion metrics help you track when a base event and a subsequent conversion event occur for an entity within a set time period.
Cumulative metrics
Derived metrics
Derived metrics allow you to perform calculations using other metrics. For example, you can calculate gross_profit by subtracting a cost metric from a revenue metric, or calculate growth by comparing a metric to its value from a previous time period.
Ratio metrics
Ratio metrics involve a numerator metric and a denominator metric. A filter string can be applied to both the numerator and denominator or separately to the numerator or denominator.
Simple metrics
Filters
Configure a filter using Jinja templating and the following syntax to reference entities, dimensions, time dimensions, or metrics in filters.
Refer to Metrics as dimensions for details on how to use metrics as dimensions with metric filters:
filter: |
{{ Entity('entity_name') }}
filter: |
{{ Dimension('primary_entity__dimension_name') }}
filter: |
{{ TimeDimension('time_dimension', 'granularity') }}
filter: |
{{ Metric('metric_name', group_by=['entity_name']) }}
For example, if you want to filter for the order date dimension grouped by month, use the following syntax:
filter: |
{{ TimeDimension('order_date', 'month') }}
Related docs
Was this page helpful?
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.