谢谢大海老师引导,已解决
this is code
let
源 = Excel.CurrentWorkbook(){[Name="表1"]}[Content],
更改的类型 = Table.TransformColumnTypes(源,{{"Id", type text}, {"明细", Int64.Type}}),
合并的查询1 = Table.NestedJoin(更改的类型,{"Id"},表2,{"Id"},"表2",JoinKind.LeftOuter),
a = Table.ExpandTableColumn(合并的查询1, "表2", {"合计"}, {"合计"}),
排序的行 = Table.Sort(a,{{"Id", Order.Ascending}, {"明细", Order.Ascending}}),
已添加自定义1 = Table.Group(排序的行, "Id", {{"计数",each Table.AddIndexColumn(_,"Index",1),type table}}),
"展开的“计数”" = Table.ExpandTableColumn(已添加自定义1, "计数", List.RemoveItems(Table.ColumnNames(已添加自定义1{0}[计数]),{"Id"})),
已添加自定义 = Table.AddColumn(#"展开的“计数”", "累计明细", each List.Sum(Table.SelectRows(#"展开的“计数”",(x)=> x[Index]<=[Index] and x[Id]=[Id])[明细])),
已添加条件列 = Table.AddColumn(已添加自定义, "结果", each if [累计明细] <= [合计] then [明细] else if [明细]-([累计明细]-[合计]) < 0 then 0 else [明细]-([累计明细]-[合计]))
in
已添加条件列