如果规则2只处理仅有一个空行的话,还是可以用List.Accumulate,否则的话可能需要借助索引等辅助,走分组标记子查询的方法。
let
源 = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Zc3BDcMwCAXQXThH6IMN2At0hR4s779GsFslUSv5APjr/TFIRToUqEYHvV/SHRBFLhbsKDlorzkWmsegXD9PQo0r9vEy/GtEGtCVMta25E3ED1HyN26hAlZvAa2tkHPrvgvDGI9CAUr8xdl3H815Ag==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [销售合同号 = _t, 外销发票号 = _t, 重量 = _t, #"分摊金额(含税)" = _t]),
tlist = List.RemoveLastN(Table.ToRows(源),0),
转换 = List.Accumulate(tlist,{},(s,c)=>if Text.Length(c{2})<=1 then List.RemoveLastN(s)&{List.RemoveLastN(List.Last(s))&{List.Sum(List.Transform({List.Last(List.Last(s)),c{3}},Number.From))} } else s&{c}),
转换2 = List.Accumulate(转换,{},(s,c)=>if Text.Length(Text.From(c{3}))<=1 then
List.RemoveLastN(s) & { List.RemoveLastN(List.Last(s)) & { Number.From( List.Last(s){2} ) / List.Sum( List.Transform({ List.Last(s){2}, c{2} }, Number.From) ) * Number.From(List.Last(s){3}) } } & {List.RemoveLastN(c)&{ Number.From(c{2}) / List.Sum(List.Transform({List.Last(s){2}, c{2} },Number.From) ) * Number.From(List.Last(s){3})} } else s&{c}),
result = Table.FromRows(转换2,Table.ColumnNames(源))
in
result