Power Query表中的网络流数据库

时间:2017-12-15 01:36:42

标签: database excel powerquery

我有以下Excel表格:

Sample Table in Excel

给出A,B和D列(数据)。计算其他列。每列中的公式如下所示。

Formula in each column of Excel Table

给定列A,B和D,可以使用Power Query创建此表吗?

1 个答案:

答案 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