Forum Discussion

rcarmichael1990's avatar
rcarmichael1990
New Member
1 year ago
Solved

Removing Promotional Data From Sales

Good afternoon, I have a data set in PQ which comprises of a list of dates, product codes, the sales quantity, and a flag (where an item on promotion is denoted by a number greater than 0) which sho...
  • AlienSx's avatar
    1 year ago
    let
        // duration step
        one_day = #duration(1, 0, 0, 0),
        // function to generate list of dates and sales
        calc = (x, y, n) => 
            [daily_sales = (y{1} - x{1}) / n, 
            lst = List.Zip({List.Dates(x{0} + one_day, n - 1, one_day), List.Numbers(x{1} + daily_sales, n - 1, daily_sales)})][lst],
        // Source data - replac with yours
        Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
        tp = Table.TransformColumnTypes(Source,{{"Date", type date}}),
        // filter out promo sales and get list from table
        nopromo_table = Table.SelectRows(tp, (x) => x[Promo Flag] = 0),
        nopromo = List.Buffer(Table.ToList(nopromo_table, (x) => {x{0}, x{2}})),
        // generate list of missing dates and sales 
        generate = List.Generate(
            () => [i = 0, s = nopromo{0}, n = 0, res = {}],
            (x) => x[i] < List.Count(nopromo),
            (x) => 
                [
                    i = x[i] + 1, 
                    s = nopromo{i}, 
                    n = Duration.Days(s{0} - x[s]{0}), 
                    res = if n <= 1 then {} else calc(x[s], s, n)
                ],
            (x) => x[res]
        ), 
        // generated list >> to table
        baseline_sales = Table.FromList(List.Combine(generate), (x) => x, {"Date", "Sale Qty"}),
        // combine and sort new sales table, fill down product column
        all = Table.Sort(nopromo_table & baseline_sales, "Date"), 
        fd = Table.FillDown(all, {"Product"})
    in
        fd