Forum Discussion

RolandsP's avatar
RolandsP
Resolver IV
8 years ago
Solved

Generate a list in Power Query for each index value

Hi, I have a simple table in Power Query. It contains following columns: - index (unique key for each table entry) - revenue - forecastIntervalMonths - forecastStartDate - forecastEndDate   ...
  • MarcelBeug's avatar
    MarcelBeug
    8 years ago

    That would be something like this:

     

    let
        Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"index", Int64.Type}, {"revenue", Int64.Type}, {"forecastIntervalMonths", Int64.Type}, {"forecastStartDate", type date}, {"forecastEndDate", type date}}),
        AddedMonths = Table.AddColumn(#"Changed Type", "Months", each 12 * (Date.Year([forecastEndDate])-Date.Year([forecastStartDate])) + Date.Month([forecastEndDate]) - Date.Month([forecastStartDate]) - (if Date.Day([forecastStartDate]) > Date.Day([forecastEndDate]) then 1 else 0), Int64.Type),
        AddedDateList = Table.AddColumn(AddedMonths, "Dates", (ThisRow) => List.Transform({0..Number.IntegerDivide(ThisRow[Months],ThisRow[forecastIntervalMonths])}, each Date.AddMonths(ThisRow[forecastStartDate],_ * ThisRow[forecastIntervalMonths])), type {date}),
        RemovedMonths = Table.RemoveColumns(AddedDateList,{"Months"})
    in
        RemovedMonths