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🚀 Application Q15 of 15
dbt - Advanced Patterns
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?
ACreate a metric with type 'sum' on a boolean field active
BDefine a single metric with type 'percentage' and sql 'active = true / total users'
CDefine a metric with type 'count' and sql 'active / total' directly in one metric
DDefine 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
Step-by-Step Solution
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]
Quick Trick:Build percentages by combining filtered count metrics [OK]
Common Mistakes:
MISTAKES
Trying to write raw SQL for percentage in one metric
Using invalid metric types like 'percentage'
Summing boolean fields without filtering
Master "Advanced Patterns" in dbt
9 interactive learning modes - each teaches the same concept differently