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]:

DateSales
01.01.20221
01.02.20222
01.03.20223
01.04.20224
01.05.20225
01.06.20226
01.07.20227
01.08.20228
01.09.20229
01.10.202210
01.11.202211
01.12.202212

 

Second [Target_sales]:

DateSales
01.03.20222
01.06.20225
01.09.202210
01.12.202213

 

When I merge first table with second I can't achieve targets for every month from [Sales_data] table because the targets are provided quarterly. My goal is to see following graph:

 

I hope that with this kid simplified data it's clear.

Thanks in advance for your help.

maati

  • 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

7 Replies

  • 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

    • maati1980's avatar
      maati1980
      Frequent Visitor

      Thanks for your direction. It's clear. But what if I would add a column to both tables with customer. Then the index would not work as the dates would be multiplied by different customers.

      • BA_Pete's avatar
        BA_Pete
        Super User

        Hi maati1980 ,

         

        In that case, you would need to Group By customer first, keeping an All Rows aggregator, then perform the indexing (and probably the rest of the steps, for simplicity) on the nested tables.

        Is this a hypothetical, or does it actually reflect your real data?

         

        Pete