Which statement best describes the purpose of metric definitions in a dbt semantic layer?
Think about how metrics help maintain consistency in analytics.
Metric definitions in dbt semantic layer specify calculations like sums, averages, or counts that can be reused. This ensures consistent results across different reports.
Given the following dbt metric definition for total_sales:
metrics:
- name: total_sales
label: Total Sales
model: sales
expression: sum(amount)
timestamp: order_date
dimensions:
- product_category
- regionWhat would be the output of a query that calculates total_sales grouped by region for orders in 2023?
SELECT region, SUM(amount) AS total_sales FROM sales WHERE order_date >= '2023-01-01' AND order_date < '2024-01-01' GROUP BY region ORDER BY region;
Consider what the SQL query is doing with the GROUP BY clause.
The query sums the amount for each region in 2023, matching the metric definition's aggregation and grouping.
Review this metric definition snippet in a dbt semantic model:
metrics:
- name: average_order_value
label: Average Order Value
model: orders
expression: avg(total)
timestamp: order_date
dimensions:
- customer_id
- order_statusWhat is the likely cause of an error when running this metric?
Check if the column used in the expression exists in the source model.
If the orders model does not have a total column, the aggregation avg(total) will fail.
You want to define a metric in dbt semantic layer to count unique active users per month from a user_activity model with columns user_id and activity_date. Which metric definition is correct?
Think about counting unique users and grouping by month.
Counting distinct user_id grouped by month using activity_date as timestamp is correct. Using sum(user_id) or wrong timestamp is incorrect.
Consider two metrics defined in dbt semantic layer:
metrics:
- name: daily_revenue
model: sales
expression: sum(amount)
timestamp: sale_date
dimensions:
- product_id
- name: monthly_revenue
model: sales
expression: sum(amount)
timestamp: sale_date
dimensions:
- product_id
- monthIf you query both metrics together grouped by product_id and month, what will be the expected output?
Think about how time dimensions affect aggregation levels.
When grouping by month, daily_revenue sums daily amounts into monthly totals, aligning with monthly_revenue. This allows comparison at the month level.