一种方法可能是使用Table.Sort 并为comparisonCriteria 参数提供一个自定义函数,用于对行进行排序:
- 主要由 SKU(在比较过程中动态确定)
- 其次是
parentsku 列是否为null
- 第三个是
sku 列
下面的代码尝试做上面描述的事情:
let
initialTable = Table.FromRows({
{"200", null},
{"200-1", "200"},
{"200-2", "200"},
{"103", null},
{"xxx", "103"},
{"yyy1", "103"},
{"yyy3", "103"},
{"400", null},
{"600", null},
{"601", "600"},
{"602", "600"},
{"405", "400"}
}, type table [sku = text, parentsku = text]),
// Custom sorting behaviour to sort by SKU. If SKUs are equal (for two rows), preference should be given to rows containing a null parent SKU.
sorted = Table.Sort(initialTable, (x as record, y as record) =>
let
xSku = if null <> x[parentsku] then x[parentsku] else x[sku],
ySku = if null <> y[parentsku] then y[parentsku] else y[sku],
skusComparison = Value.Compare(xSku, ySku),
skusAreNotEqual = 0 <> skusComparison,
parentComparison = if null = x[parentsku] then -1 else if null = y[parentsku] then 1 else Value.Compare(x[sku], y[sku]),
sorted = if skusAreNotEqual then skusComparison else parentComparison
in sorted
)
in
sorted
并生成如下表格:
我认为这符合您的预期输出。