I’m working with a set of data that, amongst other things, has each employees start/end date (if they’ve left).
I’ve been tasked with adding a table to a report of employees who are reaching a service milestone in the next 30 days. At the moment, I’m looking for 1, 2, 3, 4, 5, 10 and 25 year milestones using this snippet in M Query:
#"Added Custom3" = Table.AddColumn(#"Added Custom2", "Upcoming Milestone", each if [Leave Date] <> null then null
else if Date.IsInNextNDays( Date.AddYears( [Start Date], 1 ), 30 ) then "1 year"
else if Date.IsInNextNDays( Date.AddYears( [Start Date], 2 ), 30 ) then "2 years"
else if Date.IsInNextNDays( Date.AddYears( [Start Date], 3 ), 30 ) then "3 years"
else if Date.IsInNextNDays( Date.AddYears( [Start Date], 4 ), 30 ) then "4 years"
else if Date.IsInNextNDays( Date.AddYears( [Start Date], 5 ), 30 ) then "5 years"
else if Date.IsInNextNDays( Date.AddYears( [Start Date], 10 ), 30 ) then "10 years"
else if Date.IsInNextNDays( Date.AddYears( [Start Date], 25 ), 30 ) then "25 years" else null),
#"Added Custom4" = Table.AddColumn(#"Added Custom3", "Days Until Milestone", each if [Leave Date] <> null then null
else if Date.IsInNextNDays( Date.AddYears( [Start Date], 1 ), 30 ) then Duration.TotalDays( Date.AddYears( [Start Date], 1 ) - Date.From(DateTime.LocalNow() ) )
else if Date.IsInNextNDays( Date.AddYears( [Start Date], 2 ), 30 ) then Duration.TotalDays( Date.AddYears( [Start Date], 2 ) - Date.From(DateTime.LocalNow() ) )
else if Date.IsInNextNDays( Date.AddYears( [Start Date], 3 ), 30 ) then Duration.TotalDays( Date.AddYears( [Start Date], 3 ) - Date.From(DateTime.LocalNow() ) )
else if Date.IsInNextNDays( Date.AddYears( [Start Date], 4 ), 30 ) then Duration.TotalDays( Date.AddYears( [Start Date], 4 ) - Date.From(DateTime.LocalNow() ) )
else if Date.IsInNextNDays( Date.AddYears( [Start Date], 5 ), 30 ) then Duration.TotalDays( Date.AddYears( [Start Date], 5 ) - Date.From(DateTime.LocalNow() ) )
else if Date.IsInNextNDays( Date.AddYears( [Start Date], 10 ), 30 ) then Duration.TotalDays( Date.AddYears( [Start Date], 10 ) - Date.From(DateTime.LocalNow() ) )
else if Date.IsInNextNDays( Date.AddYears( [Start Date], 25 ), 30 ) then Duration.TotalDays( Date.AddYears( [Start Date], 25 ) - Date.From(DateTime.LocalNow() ) )
else null)
I’ve got 2 columns, 1 for which milestone is coming up and 1 for the days until that milestone. I’ve got 2 questions:
- Is there a better way I can be doing this rather than a big if statement?
- Ideally I’d like it to look for any milestones between 1 - 25, so would like a way of looping it if possible rather than 25 if statements.
Attached is the data cut down to play around with.
Employees.xlsx (17.6 KB)
Thanks!