Forum Discussion
Combining two tables into one with time complexity
- 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 remOthColsThis gives a monthly target table:
Pete
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
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_Pete4 years agoSuper 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
- maati19804 years agoFrequent Visitor
Real data consist of huge number of columns (ca.100), customer is just a simplification of the problem.
- v-jingzhang4 years agoCommunity Support
Hi maati1980
Have you solved this problem? If so, can you share your solution here to help other users that may have similar requirement? If not, can you add some dummy customer data into the sample data and expected result to show what you want to achieve?
Best Regards,
Community Support Team _ Jing