Forum Discussion

danielpaduck's avatar
danielpaduck
Helper III
2 years ago
Solved

Budgeting / Forecasting Help

Hi,   I have an interesting issue.  I have a client that essentially wants me to spread the period cost value over from the start month to the end of the current year.  Thus, we have the following:...
  • OwenAuger's avatar
    OwenAuger
    2 years ago

    No worries.

    Yes, the Json.Document step was generated by using the Enter Data function in the Power Query user interface in order to input your sample row, however I realise that this obscures the intent.

     

    Here is a version using the #table constructor instead (PBIX attached too).

    This code can be pasted into a blank query in the Power Query advanced editor to see the steps.

    let
      Source = 
        #table(
          type table[PlanID = text, StartDate = date, EndDate = date, PeriodCost = Currency.Type],
          {
            { "E28B8051205F462282A572F6DA375D5D", #date(2024,4,1), #date(2025,10,31), 4551.00 }
          }
        ),
      #"Added NumPeriods" = Table.AddColumn(
        Source,
        "NumPeriods",
        each (Date.Year([EndDate]) - Date.Year([StartDate]))
          * 12 + Date.Month([EndDate]) - Date.Month([StartDate]) + 1,
        Int64.Type
      ),
      #"Added Date List" = Table.AddColumn(
        #"Added NumPeriods",
        "Date",
        each
          let
            StartDate = [StartDate]
          in
            List.Transform(
              {0 .. [NumPeriods] - 1},
              each Date.AddMonths(Date.StartOfMonth(StartDate), _)
            ),
        type {date}
      ),
      #"Expanded Date List" = Table.ExpandListColumn(#"Added Date List", "Date"),
      #"Removed Columns" = Table.RemoveColumns(
        #"Expanded Date List",
        {"NumPeriods", "StartDate", "EndDate"}
      ),
      #"Renamed Columns" = Table.RenameColumns(#"Removed Columns", {{"Date", "StartDate"}}),
      #"Added EndDate" = Table.AddColumn(
        #"Renamed Columns",
        "EndDate",
        each Date.EndOfMonth([StartDate]),
        type date
      )
    in
      #"Added EndDate"