Forum Discussion

mafaber's avatar
mafaber
Helper II
4 years ago
Solved

Summing values in a column between two dates in Power Query

Hi,

 

I have a table with a demand plan per product per week and with a certain range in days per product telling how long into the future I need to consider the demand. What I would like to do is to calculate in a column a kind of rolling demand per product per week:

Table:

ProductRange of Coverage in daysWeekWeek start dateDemand end date (start date+RoC)Weekly DemandRolling Demand
aaa14012021.10.04.2021.10.18.10(w01+w02) = 25
aaa14022021.10.11.2021.10.25.1540
aaa14032021.10.18.2021.11.01.2530
aaa14042021.10.25.2021.11.08.55 + w05
bbb21012021.10.04.2021.10.18.100(w01+w02+w03) = 550
bbb21022021.10.11.2021.10.25.200600
bbb21032021.10.18.2021.11.01.250400 + w05
bbb21042021.10.25.2021.11.08.150150 + w05 + w06

 

Demand end date is calculated based on range of coverage of the specific product and the start date of the week, the calculation what I am looking for is the Rolling Demand. (the numbers only of course, text is just explanation)

 

So basically I need the sum of a value column where the week start date is between week start date and demand end date, and I have to calculate it in Power Query (Power BI Dataflow), not in DAX.

 

Thank you for any help!

  • I solved it myself using a custom function.

     

      #"Grouped rows 1" = Table.Group(#"Replaced value", {"material_no"}, {{"data"each _, type nullable table}}),
      // Function to calculate running totals
      RunFunction = (RunTable as tableas table =>
        let
        #"Added index" = Table.AddIndexColumn(RunTable, "index"01type number),
        #"Added custom" = Table.AddColumn(#"Added index", "demand_running_sum"each List.Sum(List.Range(#"Added index"[value_of_week], [index], [range_of_coverage_weeks])))
    in
        #"Added custom",
      // Call the function
      RunTotals = Table.TransformColumns(#"Grouped rows 1", {"data"each RunFunction(_)})
     
    where the custom column calculates the demand starting from that row (index starting from 0) and as long as needed per material (range of coverage in weeks)
     

1 Reply

  • I solved it myself using a custom function.

     

      #"Grouped rows 1" = Table.Group(#"Replaced value", {"material_no"}, {{"data"each _, type nullable table}}),
      // Function to calculate running totals
      RunFunction = (RunTable as tableas table =>
        let
        #"Added index" = Table.AddIndexColumn(RunTable, "index"01type number),
        #"Added custom" = Table.AddColumn(#"Added index", "demand_running_sum"each List.Sum(List.Range(#"Added index"[value_of_week], [index], [range_of_coverage_weeks])))
    in
        #"Added custom",
      // Call the function
      RunTotals = Table.TransformColumns(#"Grouped rows 1", {"data"each RunFunction(_)})
     
    where the custom column calculates the demand starting from that row (index starting from 0) and as long as needed per material (range of coverage in weeks)