-
Notifications
You must be signed in to change notification settings - Fork 0
Create data pipeline: gold_user_engagement #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| -- Model: gold_user_engagement | ||
| -- User engagement metrics by channel for Marketing dashboard | ||
| SELECT | ||
| channel, | ||
| COUNT(DISTINCT user_id) AS active_users, | ||
| SUM(sessions) AS total_sessions, | ||
| AVG(session_duration_minutes) AS avg_session_duration | ||
| FROM {{ ref('stg_user_activity') }} | ||
| WHERE activity_date >= DATEADD(day, -7, CURRENT_DATE) | ||
| GROUP BY 1 | ||
|
Comment on lines
+1
to
+10
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing The PR metadata specifies 🔧 Proposed fix+{{ config(materialized='table') }}
+
-- Model: gold_user_engagement
-- User engagement metrics by channel for Marketing dashboard🤖 Prompt for AI Agents |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Off-by-one:
>= DATEADD(day, -7, CURRENT_DATE)spans 8 days, not 7.With today as the anchor,
DATEADD(day, -7, CURRENT_DATE)resolves to 7 days ago. Using>=therefore includes that anchor day plus 7 subsequent days (including today) — a total of 8 days. Additionally, aggregating today's still-accumulating data skews the daily metrics for the Marketing dashboard.For 7 complete days (exclusive of today):
Or, for a rolling 7-day window ending today inclusive but exactly 7 days:
Choose whichever matches the business requirement.
🤖 Prompt for AI Agents
🧩 Analysis chain
🌐 Web query:
dbt dateadd macro documentation💡 Result:
dateaddis now documented as a dbt Core cross-database macro (namespace:dbt), not indbt_utils(those cross-db macros were removed fromdbt_utilsstarting in v1.0). [1] (docs.getdbt.com)dbt
dateaddmacro (Core)Signature
{{ dbt.dateadd(datepart="day", interval=1, from_date_or_timestamp="..." ) }}Args
datepart: a date/time part (database-specific)interval: integer (positive or negative)from_date_or_timestamp: a date/time expression [1] (docs.getdbt.com)Examples
{{ dbt.dateadd(datepart="day", interval=1, from_date_or_timestamp="'2016-03-09'") }} {{ dbt.dateadd(datepart="month", interval=-2, from_date_or_timestamp="'2016-03-09'") }} ``` [1] ([docs.getdbt.com](https://docs.getdbt.com/reference/dbt-jinja-functions/cross-database-macros)) ### Sources - [1] dbt Developer Hub — “Cross-database macros” (includes `dateadd`) ([docs.getdbt.com](https://docs.getdbt.com/reference/dbt-jinja-functions/cross-database-macros)) - [2] dbt-utils README note about cross-database macros moving to dbt Core ([github.com](https://github.com/dbt-labs/dbt-utils?utm_source=openai)) Citations: - 1: https://docs.getdbt.com/reference/dbt-jinja-functions/cross-database-macros - 2: https://docs.getdbt.com/reference/dbt-jinja-functions/cross-database-macros - 3: https://docs.getdbt.com/reference/dbt-jinja-functions/cross-database-macros - 4: https://docs.getdbt.com/reference/dbt-jinja-functions/cross-database-macros - 5: https://github.com/dbt-labs/dbt-utils?utm_source=openai --- 🌐 Web query: `DATEADD function Snowflake Redshift PostgreSQL BigQuery compatibility` 💡 Result: ### `DATEADD` compatibility overview (Snowflake vs Redshift vs PostgreSQL vs BigQuery) | Engine | Has `DATEADD`? | Add *N* units to a date/time | |---|---:|---| | **Snowflake** | Yes | `DATEADD(part, value, expr)` (aliases: `TIMEADD`, `TIMESTAMPADD`) [1] | | **Amazon Redshift** | Yes | `DATEADD(datepart, interval, expr)` [2] | | **PostgreSQL** | **No** (built-in) | Use arithmetic: `timestamp + interval '1 day'`, `date + integer` (days), etc. [5] | | **BigQuery (GoogleSQL)** | **No** (`DATEADD` name) | Use typed functions: `DATE_ADD(date, INTERVAL n part)`, `DATETIME_ADD(...)`, `TIMESTAMP_ADD(...)`, `TIME_ADD(...)` [3][4] | --- ## Syntax mappings ### Snowflake → Redshift (nearly 1:1) Both accept the same 3-argument pattern: `(part, number, expression)` [1][2]. ```sql -- Snowflake / Redshift DATEADD(day, 7, some_ts)Snowflake / Redshift → BigQuery
BigQuery uses
*_ADD(expr, INTERVAL n PART)(note theINTERVALclause and PART position) [3][4]. Google’s Snowflake migration guide summarizes this mapping as:DATEADD(part, value, dateOrTime)→DATE_ADD(date, INTERVAL value part)[6]Snowflake / Redshift → PostgreSQL
PostgreSQL typically uses
+ interval(and fordate,+ integeradds days) [5].Key behavioral gotchas (portability)
Return type differences when input is
DATEand you add sub-day unitshour/minute/secondto aDATEreturns a timestamp (TIMESTAMP_NTZ) starting at midnight [1].DATEADDreturnsTIMESTAMPfor date inputs (date becomes timestamp at00:00:00) [2].Month/quarter/year end-of-month behavior
MONTH/QUARTER/YEAR, if the target month is shorter, the result becomes the last day of that month [3].year/quarter/monthcan shift the day-of-month when the result month has fewer days [1].BigQuery
TIMESTAMP_ADDsupports only up toDAYTIMESTAMP_ADD(..., INTERVAL n DAY/HOUR/MINUTE/...)is supported, but notMONTH/QUARTER/YEARforTIMESTAMP_ADD[4]. (UseDATETIME_ADD, or convert toDATETIME/DATEwhere appropriate.)Sources: Snowflake
DATEADD[1], RedshiftDATEADD[2], BigQueryDATE_ADD[3], BigQueryTIMESTAMP_ADD[4], PostgreSQL date/time operators & interval rules [5], BigQuery Snowflake translation guide [6].Use the
{{ dbt.dateadd() }}macro instead ofDATEADDfor cross-warehouse compatibility.DATEADD(datepart, interval, date)is native to Snowflake and Redshift but PostgreSQL does not have a built-inDATEADDfunction, and BigQuery usesDATE_ADD/DATETIME_ADD/TIMESTAMP_ADD. dbt provides a cross-warehouse{{ dbt.dateadd() }}macro that compiles to the correct dialect for each adapter.🔧 Proposed fix using dbt's built-in macro
📝 Committable suggestion
🤖 Prompt for AI Agents