题目:
将表1、表2导入到PQ中:
结果:
完整代码:
let
源 = Table.AddColumn(表1, "Mail Type", each List.Last(表2[Mail Type])),
fn = (t) => [
s1 = List.Sum(t[订舱重量]), s2 = 源[账单重量]{0},
w = if s2 <> s1 then [
r = List.Last(List.PositionOf(t[订舱重量],List.Min(t[订舱重量]),#infinity)),
n = s2 - (s1 - t[订舱重量]{r}),
t1 = Table.RemoveColumns(Table.AddColumn(Table.AddIndexColumn(t, "索引", 0, 1, Int64.Type), "匹配后重量", each if [索引]=r then n else [订舱重量]), {"索引"})
][t1] else t][w],
匹配后重量 = fn(表2),
合并的查询 = Table.NestedJoin(匹配后重量, {"年", "月", "AB", "Carrier", "Export", "Dest", "Mail Type"}, 源, {"年", "月", "账期", "Carrier", "启运站", "目的港", "Mail Type"}, "源.1", JoinKind.LeftOuter),
展开的查询 = Table.ExpandTableColumn(合并的查询, "源.1", {"AWB费用", "杂费", "其他费用"}, {"AWB费用", "杂费", "其他费用"})
in
展开的查询
- 自定义函数fn,其参数t是表格,上面直接传入表2,到函数中处理,函数由中括号包裹,最终取[W]的键,这个非常像字典对象的结构,在PQ中叫记录集,表的每行实际也是记录集。
- 自定义函数中使用这个结构能够暂时存储一些反复用的变量,如s1、s2,方便在后面的逻辑调用,