let
源 = Excel.CurrentWorkbook(){[Name="表1"]}[Content],
分组的行 = Table.Group(源, {"订单日期", "订单号", "产品编码"}, {{"A", each _, type table [订单号=number, 订单日期=datetime, 产品编码=number, 订单数量=number, 废品率=number, 实发数量=number, 库存数量=number, 实发与库存差=number]}}),
data = Table.TransformColumns(分组的行,{"A",each [a=_,b=Table.SelectColumns(a,{"实发数量","库存数量"}),c=Table.ToColumns(b),d=List.Zip({List.Repeat(List.Distinct(c{1}),List.Count(c{0})-1),List.FirstN(List.Transform({1..List.Count(c{0})},each List.Sum(List.FirstN(c{0},_))),List.Count(c{0})-1)}),e=Table.AddColumn(Table.FromColumns({c{0},List.Distinct(c{1})&List.Transform(d,each _{0}-_{1})},{"实发数量","库存数量"}),"实发与库存差异",each [实发数量]-[库存数量])][e]}),
#"展开的“A”" = Table.ExpandTableColumn(data, "A", {"实发数量", "库存数量", "实发与库存差异"}, {"实发数量", "库存数量", "实发与库存差异"})
in
#"展开的“A”" |