A subscriptions dataset has a [Signup Date] and a [Cancel Date] field, both stored as dates. An analyst needs a calculated field that returns the whole number of complete months each customer stayed subscribed, so that a customer who signed up on 15 January and cancelled on 14 March counts as one month rather than two. Which calculation returns the correct result?
// Sample row:
// [Signup Date] = 2024-01-15
// [Cancel Date] = 2024-03-14
// Expected result = 1- ADATEDIFF('month', [Cancel Date], [Signup Date]) - IIF(DAY([Cancel Date]) < DAY([Signup Date]), 1, 0)
- BDATEPART('month', [Cancel Date]) - DATEPART('month', [Signup Date]) - IIF(DAY([Cancel Date]) < DAY([Signup Date]), 1, 0)
- CDATEDIFF('month', [Signup Date], [Cancel Date]) + IIF(DAY([Cancel Date]) < DAY([Signup Date]), 1, 0)
- DDATEDIFF('month', [Signup Date], [Cancel Date]) - IIF(DAY([Cancel Date]) < DAY([Signup Date]), 1, 0) Correct
Why A is wrong: The day-adjustment is correct but the two date arguments are reversed, so DATEDIFF returns a negative value for any customer who cancels after signing up. The dates must run start then end.
Why B is wrong: Subtracting month numbers ignores the year, so it miscounts across year boundaries and returns a negative figure when the cancel month is numerically lower than the signup month.
Why C is wrong: Adding rather than subtracting one inflates the count further; DATEDIFF already over-counts when the end day is earlier, so the correction must reduce the total, not increase it.
Why D is correct: DATEDIFF with 'month' counts calendar-month boundaries crossed, so it over-counts by one when the end day has not yet reached the start day; subtracting one in that case yields complete elapsed months.