0
0
dbtdata~20 mins

Metric definitions and semantic layer in dbt - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Metric Mastery in dbt Semantic Layer
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding Metric Definitions in dbt Semantic Layer

Which statement best describes the purpose of metric definitions in a dbt semantic layer?

AThey define reusable calculations that can be applied consistently across reports and dashboards.
BThey store raw data tables before transformation.
CThey are used to schedule dbt runs automatically.
DThey replace the need for SQL in data transformations.
Attempts:
2 left
💡 Hint

Think about how metrics help maintain consistency in analytics.

Predict Output
intermediate
2:00remaining
Output of a Metric Definition Query

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
      - region

What would be the output of a query that calculates total_sales grouped by region for orders in 2023?

dbt
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;
AA list of all sales transactions without aggregation.
BAn error because the metric definition cannot be used in SQL queries.
CA table showing total sales by product_category instead of region.
DA table with regions as rows and their corresponding total sales amounts for 2023.
Attempts:
2 left
💡 Hint

Consider what the SQL query is doing with the GROUP BY clause.

🔧 Debug
advanced
2:00remaining
Identifying the Error in a Metric Definition

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_status

What is the likely cause of an error when running this metric?

AThe timestamp field <code>order_date</code> is missing from the metric definition.
BThe expression uses <code>avg(total)</code> but the column <code>total</code> does not exist in the <code>orders</code> model.
CDimensions cannot include <code>customer_id</code> in metric definitions.
DThe metric name <code>average_order_value</code> is too long and invalid.
Attempts:
2 left
💡 Hint

Check if the column used in the expression exists in the source model.

🚀 Application
advanced
2:30remaining
Creating a Metric for Monthly Active Users

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?

A
metrics:
  - name: monthly_active_users
    label: Monthly Active Users
    model: user_activity
    expression: count_distinct(user_id)
    timestamp: activity_date
    dimensions:
      - month
B
metrics:
  - name: monthly_active_users
    label: Monthly Active Users
    model: user_activity
    expression: count(user_id)
    timestamp: activity_date
    dimensions:
      - activity_date
C
metrics:
  - name: monthly_active_users
    label: Monthly Active Users
    model: user_activity
    expression: sum(user_id)
    timestamp: activity_date
    dimensions:
      - month
D
metrics:
  - name: monthly_active_users
    label: Monthly Active Users
    model: user_activity
    expression: count(distinct user_id)
    timestamp: user_id
    dimensions:
      - month
Attempts:
2 left
💡 Hint

Think about counting unique users and grouping by month.

data_output
expert
3:00remaining
Result of Combining Metrics with Different Time Dimensions

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
      - month

If you query both metrics together grouped by product_id and month, what will be the expected output?

AThe output will show daily_revenue broken down by day and monthly_revenue by month in separate rows.
BThe query will fail because daily_revenue cannot be grouped by month dimension.
CThe daily_revenue will be aggregated by month, matching monthly_revenue, showing total revenue per product per month.
DThe monthly_revenue will be duplicated for each day, inflating totals.
Attempts:
2 left
💡 Hint

Think about how time dimensions affect aggregation levels.