33. Monthly Retention by Cohort
Difficulty: Hard · Topics: Joins, Aggregations, Dates, Null Handling, Filtering & Selection
An advanced problem: unlock it to open the statement, sample data and editor. Graded on 3 test cases.
Learn the concepts
- trunc / add_months · 4 min read. Truncate dates to the month or year and shift them by whole months, including month-end rules.
- Parsing dates and timestamps · 4 min read. to_date, to_timestamp and date_format patterns, and why yyyy and YYYY are not the same.
- fillna / dropna · 4 min read. Fill or drop missing values per column, and why fillna(0) leaves string columns alone.
PySpark functions you'll practise
- groupBy
- agg
- count
- min
- join
- fillna
- trunc
- add_months
- date_format
- filter
- select
- withColumn
- distinct
Related problems
- Customers Who Bought Every Product · Hard · Joins, Aggregations, Filtering & Selection
- Fill Missing Dates (Calendar Spine) · Hard · Joins, Arrays, Aggregations, Dates, Null Handling, Filtering & Selection
- Reconcile Two Sources (Full Outer Join) · Hard · Joins, Null Handling, Conditional Logic, Filtering & Selection
- Price Valid at Order Time (Range Join) · Hard · Joins, Null Handling, Filtering & Selection
- Year-over-Year Growth · Medium · Joins, Filtering & Selection
Browse
Topics: Window Functions · Joins · Aggregations · Pivot, Unpivot & Rollup · Arrays · Null Handling · Conditional Logic · Dates · Filtering & Selection · Strings · Data Lake · Lakehouse · Streaming · Data Prep for AI · Spark Performance
Difficulty: Easy · Medium · Hard · Learn · All problems · dbt practice