Skip to content

47. Month Start, Month End and Due Dates

Difficulty: Medium · Topics: Dates, Filtering & Selection

For each invoice in invoices (issue_date as YYYY-MM-DD), compute:

Return invoice_id, issue_date, month_start, month_end, due_date.

Row order does not matter; column names must match. Your code is graded on 3 test cases, including hidden edge cases.

Sample data

invoices

invoice_idissue_date
12024-01-15
22024-01-31
32023-02-28

Expected output

invoice_idissue_datemonth_startmonth_enddue_date
12024-01-152024-01-012024-01-312024-02-15
22024-01-312024-01-012024-01-312024-02-29
32023-02-282023-02-012023-02-282023-03-28

Hints

Hint 1F.trunc("issue_date", "month") gives the first day of the month.
Hint 2F.last_day gives the last day, including leap years.
Hint 3F.add_months(col, 1) clamps to the month end (Jan 31 + 1 month = Feb 29 in 2024).

PySpark functions you'll practise

Related problems

Browse

Topics: Window Functions · Joins · Aggregations · Pivot, Unpivot & Rollup · Arrays · Null Handling · Conditional Logic · Dates · Filtering & Selection · Strings

Difficulty: Easy · Medium · Hard · PySpark interview roadmap · All problems