@天天吃鸡
用一个逻辑来做吧,虽然长一点,效率高,亲测1万条,妙出。
let
源 = Excel.CurrentWorkbook(){[Name="demo"]}[Content],
类型 = Table.Buffer(Table.TransformColumnTypes(源,{{"月份", type date}, {"条件1", type text}, {"条件2", type text}, {"累计值", Int64.Type}})),
排序 = Table.Sort(类型,{{"条件1", Order.Ascending}, {"条件2", Order.Ascending}, {"累计值", Order.Ascending}}),
索引0 = Table.AddIndexColumn(排序, "索引_0", 0, 1),
索引1 = Table.AddIndexColumn(排序, "索引_1", 1, 1),
分组初始值 = Table.Group(排序, {"条件1", "条件2"}, {{"初始值", each List.Min([累计值]), Int64.Type}}),
合并初始值 = Table.NestedJoin(索引0, {"条件1", "条件2"}, 分组初始值, {"条件1", "条件2"}, "分组初始值", JoinKind.LeftOuter),
展开初始值 = Table.ExpandTableColumn(合并初始值,"分组初始值",{"初始值"}),
合并索引1 = Table.NestedJoin(展开初始值, {"索引_0"}, 索引1, {"索引_1"}, "索引_1", JoinKind.LeftOuter),
展开索引1 = Table.ExpandTableColumn(合并索引1, "索引_1", {"累计值"}, {"累计值1"}),
当前值 = Table.AddColumn(展开索引1, "计算当前值", each if [累计值]=[初始值] then [累计值] else [累计值]-[累计值1],Int64.Type),
结果 = Table.SelectColumns(当前值,{"月份", "条件1", "条件2", "累计值", "计算当前值"})
in
结果