Forum Discussion
Fill missing dates with previous value
- 4 years ago
Hi ddijstelbloem ,
Try this:
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bc5BDsAgCATAv3CWBLZq7VuI//9GpXiqcNpksoAZAaxgKBXqAvFZsXaaxUhvVoYAP62hT6rYysgQH7YD/QEd0ZRc90/tuOrxisU6cl3d+QI=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [MutationDate = _t, Partcode = _t, CumInvQty = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"MutationDate", type text}, {"Partcode", Int64.Type}, {"CumInvQty", Int64.Type}}), #"Changed Type with Locale" = Table.TransformColumnTypes(#"Changed Type", {{"MutationDate", type date}}, "en-GB"), #"Sorted Rows" = Table.Sort(#"Changed Type with Locale",{{"Partcode", Order.Ascending}, {"MutationDate", Order.Ascending}}), #"Added Index" = Table.AddIndexColumn(#"Sorted Rows", "Index", 0, 1, Int64.Type), #"Added Custom" = Table.AddColumn(#"Added Index", "FilledMutationDate", each let EndDate_ = let NextPartcode_ = try #"Added Index"[Partcode]{ [Index] + 1 } otherwise null, NextDate_= try #"Added Index" [MutationDate] { [Index] + 1 } otherwise null in if [Partcode]=NextPartcode_ then Number.From(NextDate_)-1 else Number.From([MutationDate]) in {Number.From([MutationDate])..EndDate_}), #"Expanded FilledMutationDate" = Table.ExpandListColumn(#"Added Custom", "FilledMutationDate"), #"Changed Type1" = Table.TransformColumnTypes(#"Expanded FilledMutationDate",{{"FilledMutationDate", type date}}), #"Removed Columns" = Table.RemoveColumns(#"Changed Type1",{"MutationDate", "Index"}), #"Reordered Columns" = Table.ReorderColumns(#"Removed Columns",{"FilledMutationDate", "Partcode", "CumInvQty"}) in #"Reordered Columns"Source Table:
Result Table:
...
Best Regards,
Icey
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
The given solution is not working for me unfortunately. Maybe not completely clear but the Column Partcode consists of multiple parts, each with several mutationdates
If we take the first Partcode 42007505, these are the mutations in a certain period and I need the gab between 4-1-2021 and 20-1-2021 to be filled with 5-1-2021, 6-1-2021, 7-1-2021 and the CumInvQuantity should be 572.,7 for all these dates until 20-1-2021 where it changes. 22-1-2021 until 4-2-2021 should display 268,7 etc.
Hi ddijstelbloem ,
Try this:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bc5BDsAgCATAv3CWBLZq7VuI//9GpXiqcNpksoAZAaxgKBXqAvFZsXaaxUhvVoYAP62hT6rYysgQH7YD/QEd0ZRc90/tuOrxisU6cl3d+QI=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [MutationDate = _t, Partcode = _t, CumInvQty = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"MutationDate", type text}, {"Partcode", Int64.Type}, {"CumInvQty", Int64.Type}}),
#"Changed Type with Locale" = Table.TransformColumnTypes(#"Changed Type", {{"MutationDate", type date}}, "en-GB"),
#"Sorted Rows" = Table.Sort(#"Changed Type with Locale",{{"Partcode", Order.Ascending}, {"MutationDate", Order.Ascending}}),
#"Added Index" = Table.AddIndexColumn(#"Sorted Rows", "Index", 0, 1, Int64.Type),
#"Added Custom" = Table.AddColumn(#"Added Index", "FilledMutationDate", each let EndDate_ =
let NextPartcode_ = try #"Added Index"[Partcode]{ [Index] + 1 } otherwise null, NextDate_= try #"Added Index" [MutationDate] { [Index] + 1 } otherwise null
in if [Partcode]=NextPartcode_ then Number.From(NextDate_)-1 else Number.From([MutationDate])
in {Number.From([MutationDate])..EndDate_}),
#"Expanded FilledMutationDate" = Table.ExpandListColumn(#"Added Custom", "FilledMutationDate"),
#"Changed Type1" = Table.TransformColumnTypes(#"Expanded FilledMutationDate",{{"FilledMutationDate", type date}}),
#"Removed Columns" = Table.RemoveColumns(#"Changed Type1",{"MutationDate", "Index"}),
#"Reordered Columns" = Table.ReorderColumns(#"Removed Columns",{"FilledMutationDate", "Partcode", "CumInvQty"})
in
#"Reordered Columns"
Source Table:
Result Table:
...
Best Regards,
Icey
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
- ddijstelbloem4 years agoFrequent Visitor
I have replaced the source but now I keep getting an error. Can you see what is wrong?
- Icey4 years agoCommunity Support