ExcelQuestion
New member
- Joined
- May 27, 2018
- Messages
- 26
- Reaction score
- 0
- Points
- 0
- Excel Version(s)
- Office 365
Hello Experts,
The User would select from the dropdown list within column [Select Frequency]. Then, helper columns [DateAdd Name] and [DateAdd Value] would be populated from the dimension table. The goal is to generate a list of dates based on the selected frequency (ie. monthly, bi-weekly, semi-monthly, quarterly, yearly), shown below.

1) This Function does work except that it's filled with many nested IF...THEN's. Is there an efficient way to rewrite this? Perhaps a SWITCH equivalent in Power Query?
2) All frequencies work...except for the "Semi-monthly 1st, 15th" and the "Semi-monthly 15th, last" options as I am unsure how to reference the Period (the Counter equivalent) in this case.
Is there a way to reference the previous row's date?
The M code for both semi-monthly options would have worked only if the previous date could be referenced. A sample of the desired semi-monthly output is included in the screenshot above.
The sample file is attached.
Thanks in advance,
Ricky
The User would select from the dropdown list within column [Select Frequency]. Then, helper columns [DateAdd Name] and [DateAdd Value] would be populated from the dimension table. The goal is to generate a list of dates based on the selected frequency (ie. monthly, bi-weekly, semi-monthly, quarterly, yearly), shown below.

1) This Function does work except that it's filled with many nested IF...THEN's. Is there an efficient way to rewrite this? Perhaps a SWITCH equivalent in Power Query?
2) All frequencies work...except for the "Semi-monthly 1st, 15th" and the "Semi-monthly 15th, last" options as I am unsure how to reference the Period (the Counter equivalent) in this case.
Is there a way to reference the previous row's date?
The M code for both semi-monthly options would have worked only if the previous date could be referenced. A sample of the desired semi-monthly output is included in the screenshot above.
(Input_Start_Date as date, Periods as number, Select_Frequency as text, DateAdd_Name as text, DateAdd_Value as number) =>
let
Date = Input_Start_Date,
GenerateDates = List.Generate(
()=> [Date = Input_Start_Date, Period = 0],
each [Period] <= Periods,
each [Period = [Period] + 1,
Date =
if DateAdd_Name = "dateaddweeks" then
Date.AddWeeks(Input_Start_Date, Period * DateAdd_Value)
else
if DateAdd_Name = "dateaddmonths" then
Date.AddMonths(Input_Start_Date, Period * DateAdd_Value)
else
if DateAdd_Name = "dateaddyears" then
Date.AddYears(Input_Start_Date, Period * DateAdd_Value)
else
if DateAdd_Name = "dateaddquarters" then
Date.AddQuarters(Input_Start_Date, Period * DateAdd_Value)
else
// Both semi-monthly options (below) doesn't work. Is there a way to reference the previous row's date?
if DateAdd_Name = "date_function" and DateAdd_Value = 1 then
if Date.Day(Input_Start_Date) < 15 then
#date(Date.Year(Input_Start_Date),Date.Month(Input_Start_Date),15)
else Date.StartOfMonth(Date.AddMonths(Input_Start_Date,1))
else
if DateAdd_Name = "date_function" and DateAdd_Value <> 1 then
if Date.Day(Input_Start_Date) < 15 then
#date(Date.Year(Input_Start_Date),Date.Month(Input_Start_Date),15)
else if Input_Start_Date = Date.EndOfMonth(Input_Start_Date) then
#date(Date.Year(Input_Start_Date),Date.Month(Date.AddMonths(Input_Start_Date,1)),15)
else Date.EndOfMonth(Input_Start_Date)
else null
]),
ConvertToTable = Table.FromRecords(GenerateDates)
in
ConvertToTable
The sample file is attached.
Thanks in advance,
Ricky