📖 Scenario: You work as a data analyst at a retail company. Your team wants to create clear, reusable metrics for sales and customer data. These metrics will help everyone understand the business performance easily.
🎯 Goal: Build metric definitions in dbt and create a semantic layer to calculate total sales and average order value.
📋 What You'll Learn
Create a metric definition for total sales
Create a metric definition for average order value
Use a semantic layer to combine these metrics
Output the final metrics values
💡 Why This Matters
🌍 Real World
Companies use metric definitions and semantic layers to standardize business metrics so everyone reports consistent numbers.
💼 Career
Data analysts and engineers build these layers to make data trustworthy and easy to use for decision makers.
Progress0 / 4 steps
1
Create initial sales data model
Create a dbt model file called sales.sql with a table named sales that has columns order_id, customer_id, and amount. Insert these exact rows: (1, 101, 100), (2, 102, 150), (3, 101, 200).
dbt
Hint
Use a CTE with union all to create the rows inside the model.
2
Define total sales metric
Create a metric definition in metrics.yml called total_sales that sums the amount column from the sales model.
dbt
Hint
Define the metric with name, model, type, and sql fields.
3
Define average order value metric
Add a metric definition in metrics.yml called average_order_value that calculates the average amount per order_id from the sales model.
dbt
Hint
Use type: average and add group_by: order_id to calculate average per order.
4
Query metrics from semantic layer
Write a dbt model called metrics_summary.sql that selects the total_sales and average_order_value metrics from the semantic layer and prints their values.
dbt
Hint
Use sum(amount) and avg(amount) in your select statement from the sales model.
Practice
(1/5)
1. What is the main purpose of defining metrics in dbt's semantic layer?
easy
A. To standardize how key numbers are calculated and used across reports
B. To create visual dashboards automatically
C. To store raw data from different sources
D. To write SQL queries faster
Solution
Step 1: Understand metric definitions
Metric definitions specify how to calculate important numbers like sales or counts.
Step 2: Connect metric definitions to semantic layer purpose
The semantic layer uses these definitions to ensure everyone uses the same calculations, keeping reports consistent.
Final Answer:
To standardize how key numbers are calculated and used across reports -> Option A
But when you run your report, the average is incorrect. What is the likely error?
medium
A. The metric type 'average' is invalid; it should be 'avg'
B. The metric type should be 'average' instead of 'avg'
C. The model reference should not use ref()
D. The 'sql' field should be 'amount' instead of 'total_price'
Solution
Step 1: Check valid metric types in dbt
dbt metric types include 'average' for average, not 'avg'.
Step 2: Identify the impact of wrong type
Using 'avg' is invalid and causes incorrect calculation or errors.
Final Answer:
The metric type should be 'average' instead of 'avg' -> Option B
Quick Check:
Use 'average' for average metric type [OK]
Hint: Use 'average' not 'avg' for average metrics [OK]
Common Mistakes:
Using 'avg' instead of 'average' as metric type
Changing model reference incorrectly
Assuming 'sql' field name is wrong
5. You want to create a metric that calculates the percentage of active users out of total users using dbt's semantic layer. Which approach correctly defines this metric?
hard
A. Create a metric with type 'sum' on a boolean field active
B. Define a single metric with type 'percentage' and sql 'active = true / total users'
C. Define a metric with type 'count' and sql 'active / total' directly in one metric
D. Define two separate metrics: one for active_user_count (type: count with filter active=true) and one for total_user_count (type: count), then create a derived metric dividing active_user_count by total_user_count
Solution
Step 1: Understand metric composition in dbt semantic layer
Complex metrics like percentages are built by combining simpler metrics, not by writing raw SQL in one metric.
Step 2: Define filtered and total counts separately
Define active_user_count with a filter for active=true, and total_user_count without filter.
Step 3: Create derived metric for percentage
Use a derived metric that divides active_user_count by total_user_count to get the percentage.
Final Answer:
Define two separate metrics: one for active_user_count (type: count with filter active=true) and one for total_user_count (type: count), then create a derived metric dividing active_user_count by total_user_count -> Option D
Quick Check:
Build complex metrics by combining simple filtered metrics [OK]
Hint: Build percentages by combining filtered count metrics [OK]
Common Mistakes:
Trying to write raw SQL for percentage in one metric
Using invalid metric types like 'percentage'
Summing boolean fields without filtering
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.