Challenge - 5 Problems
dbt-utils Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of surrogate_key with multiple columns
Given the following dbt SQL snippet using
dbt_utils.surrogate_key, what is the output of the surrogate key for the row where id = 2 and date = '2024-01-01'? Assume the hash function produces lowercase hex strings.select {{ dbt_utils.surrogate_key(['id', 'date']) }} as sk from (select 2 as id, '2024-01-01' as date) as tdbt
select {{ dbt_utils.surrogate_key(['id', 'date']) }} as sk from (select 2 as id, '2024-01-01' as date) as tAttempts:
2 left
💡 Hint
The surrogate_key macro concatenates columns with a pipe '|' and hashes the string.
✗ Incorrect
The dbt_utils.surrogate_key macro concatenates the values with '|' and then applies a hash function. For id=2 and date='2024-01-01', the string is '2|2024-01-01'. The hash of this string is 'a7f5f35426b927411fc9231b56382173'.
❓ data_output
intermediate2:00remaining
Result of pivot macro on sales data
Consider a table
Data:
sales with columns region, product, and sales_amount. Using dbt_utils.pivot to pivot product values into columns with sum of sales_amount, what is the resulting table output for this data?Data:
region | product | sales_amount
North | A | 100
North | B | 150
South | A | 200
South | B | 50dbt
select * from {{ dbt_utils.pivot('sales', 'product', 'sales_amount', 'region', 'sum') }}
Attempts:
2 left
💡 Hint
Pivot creates columns for each product with aggregated sales_amount per region.
✗ Incorrect
The pivot macro sums sales_amount grouped by region and product. For North, A=100 and B=150; for South, A=200 and B=50.
🔧 Debug
advanced2:00remaining
Error raised by unpivot macro misuse
What error will this dbt SQL code raise?
Assume
select * from {{ dbt_utils.unpivot('sales', ['A', 'B'], 'product', 'sales_amount') }}Assume
sales table has columns region, A, B.dbt
select * from {{ dbt_utils.unpivot('sales', ['A', 'B'], 'product', 'sales_amount') }}
Attempts:
2 left
💡 Hint
Unpivot requires specifying columns to keep besides the unpivoted columns.
✗ Incorrect
The unpivot macro does not automatically include columns like 'region' unless specified. This causes a runtime error when referencing 'region' after unpivoting.
🚀 Application
advanced2:00remaining
Using surrogate_key in incremental model
You want to create an incremental dbt model that uses
dbt_utils.surrogate_key on columns user_id and event_date to uniquely identify rows. Which of the following SQL snippets correctly creates the surrogate key and uses it as the primary key for incremental updates?Attempts:
2 left
💡 Hint
The surrogate key should be added as a new column, not replace existing columns.
✗ Incorrect
Option A adds the surrogate key as a new column named 'id' while keeping all original columns, which is correct for incremental models.
🧠 Conceptual
expert2:00remaining
Understanding pivot and unpivot transformations
Which statement correctly describes the relationship between
dbt_utils.pivot and dbt_utils.unpivot macros?Attempts:
2 left
💡 Hint
Think about how data shape changes with pivot and unpivot.
✗ Incorrect
Pivot reshapes data by turning unique values from one column into multiple columns. Unpivot reverses this by turning columns back into rows.