答案 0 :(得分:0)
可以使用其中一种循环机制:List.Generate,List.Accumulate或递归函数。
由于迭代次数已知,我更喜欢List.Accumulate。
在下面的查询中,List.Accumulate构建一个记录列表,其中包含必须添加到Source的字段。
结果表是通过列值列表和新表类型构建的。
let
Source = Table1,
IncomingSupplies = List.Buffer(Source[Incoming Supply]),
GenerateFlow =
List.Accumulate(
List.Skip(IncomingSupplies),
{[Beginning On Hand Inventory = Source[Starting Inventory]{0},
Shipments = 0.5 * #"Beginning On Hand Inventory",
Ending On Hand = #"Beginning On Hand Inventory" + IncomingSupplies{0} - Shipments]},
(Result,Supply) =>
Result &
{[Beginning On Hand Inventory = List.Last(Result)[Ending On Hand],
Shipments = 0.5 * #"Beginning On Hand Inventory",
Ending On Hand = #"Beginning On Hand Inventory" + Supply - Shipments]}),
NewTableType = Value.Type(Table.AddColumn(Source,"Records",each [], type[])),
CombineSourceAndFlow = Table.FromColumns(Table.ToColumns(Source)&{GenerateFlow},NewTableType),
ExpandFlow = Table.ExpandRecordColumn(CombineSourceAndFlow, "Records", {"Beginning On Hand Inventory", "Shipments", "Ending On Hand"}),
Typed = Table.TransformColumnTypes(ExpandFlow,{{"Beginning On Hand Inventory", Int64.Type}, {"Shipments", Int64.Type}, {"Ending On Hand", Int64.Type}}),
Reordered = Table.ReorderColumns(Typed,{"Month", "Starting Inventory", "Beginning On Hand Inventory", "Incoming Supply", "Shipments", "Ending On Hand"})
in
Reordered