Hi @Frede,
I’ve added comments to the M code below
I hope this provides enough detail to help you better understand what is going on in List.Generate
( myStartMonth as number, myStartYear as number, myPeriodID as text, endDate as any ) as list =>
let
// load the Interval table into memory
BufferedInterval = Table.Buffer(Interval),
// keep the min date from the last date in the date table and the subscription enddate
maxDate = if endDate <> Date.Type or endDate = null then Date.From(CalenderMax) else List.Min( { Date.From(CalenderMax), Date.From(endDate) }),
Source = List.Generate(
// Create an initial Record
() => [x = 1, y = maxDate, z = #date(myStartYear, myStartMonth, 1)],
// Define the Condition (Do-While-Loop)
each [z] < [y],
// Function that defines a new value for each Record element in that iteration.
each [x = [x] + 1, y = [y], z = Date.AddMonths( [z],
Table.SelectRows( BufferedInterval,
(BT) => BT[Periodicity] = myPeriodID ){0}[Interval in Months]
)
]
),
// Create a table from the generated Record values in List.Generate
CreateTable = Table.FromRecords(Source)
in
// from that table extract the values from column "z", a single column returns a list
// if this evaluation returns an error an empty list is returned
try CreateTable[z] otherwise {}