Forum Discussion

maati1980's avatar
maati1980
Frequent Visitor
4 years ago
Solved

Combining two tables into one with time complexity

Hello, I'm looking for a solution for combining two tables. One [Sales_data]: Date Sales 01.01.2022 1 01.02.2022 2 01.03.2022 3 01.04.2022 4 01.05.2022 5 01.06.2022 6 ...
  • BA_Pete's avatar
    4 years ago

    Hi maati1980 ,

     

    Here's a way of increasing the granularity of your target table to match your monthly sales table. I've assumed that the targets set quarterly are the monthly targets for the next 3 months:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjDUMzDWMzIwMlLSUTJSitWBCJnBhEzhQpYwIUMDmJihEVzMWCk2FgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, Sales = _t]),
        chgTypes = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Sales", Int64.Type}}),
        sortASC = Table.Sort(chgTypes,{{"Date", Order.Ascending}}),
        addIndex1 = Table.AddIndexColumn(sortASC, "Index1", 1, 1, Int64.Type),
        addIndex0 = Table.AddIndexColumn(addIndex1, "Index0", 0, 1, Int64.Type),
        mergeOnSelf = Table.NestedJoin(addIndex0, {"Index1"}, addIndex0, {"Index0"}, "addIndex0", JoinKind.LeftOuter),
        expandDate = Table.ExpandTableColumn(mergeOnSelf, "addIndex0", {"Date"}, {"dateTo"}),
        replaceNullDateTo = Table.ReplaceValue(expandDate,null, each Date.AddMonths([Date], 3) ,Replacer.ReplaceValue,{"dateTo"}),
        #"replaceDateTo-1Day" = Table.ReplaceValue(replaceNullDateTo,each [dateTo], each Date.AddDays([dateTo], -1),Replacer.ReplaceValue,{"dateTo"}),
        addMonthList = Table.AddColumn(#"replaceDateTo-1Day", "monthList", each List.Transform(
        {Number.From([Date])..Number.From([dateTo])},
        each Date.StartOfMonth(Date.From(_))
    )),
        expandMonthList = Table.ExpandListColumn(addMonthList, "monthList"),
        tableDistinct = Table.Distinct(expandMonthList),
        remOthCols = Table.SelectColumns(tableDistinct,{"monthList", "Sales"})
    in
        remOthCols

     

    This gives a monthly target table:

     

    Pete