Power Query Group By Another Table 条件
Power Query Group By Another Table Criteria
我在 excel 中有一份报告。我想根据 Main Table 和 Criteria Table.
中的信息获得最终输出
Main Table - 这个 table 有很多列并且已经在 Power Query 中称为 Proposal
| Vendor | Business Area | Amount |
|----------|-----------------|--------|
| 11000010 | MY01 | 100 |
| 11000017 | MY51 | 200 |
| 11000018 | MY53 | 150 |
| 11000019 | MY55 | 220 |
| 11000030 | MY57 | 214 |
| 11000045 | MY59 | 500 |
| 11000056 | MY5HA | 586 |
| 31000125 | MYHW | 564 |
| 31000133 | MY71 | 546 |
| 31000139 | MY73 | 4896 |
| 31000144 | MY38 | 486 |
| 31013425 | MYSA | 4869 |
| 81000225 | MYSA | 489 |
Criteria Table- 此 table 已在 Power Query 中,称为 VendorCriteria。
如果 Business Area 为空白,但供应商在范围内,仍将属于各自的分组。
| Vendor Low Range | Vendor Upper Range | Business Area | Group |
|------------------|--------------------|-------------------------------------------------------------------|--------|
| 10000000 | 29999999 | MY51,MY53,MY55,MY57,MY59,MY5HA,MYHW,MY71,MY73,MY75,MYSA,MYSW,MYS1 | Group1 |
| 10000000 | 29999999 | MY01,MY38 | Group2 |
| 60000000 | 69999999 | | Group3 |
| 30000000 | 39999999 | | Group4 |
| 80000000 | 89999999 | | Group5 |
结果- 这只是为了根据标准 table.
显示相应的行结果
| Vendor | Business Area | Amount | Base on Criterial Table |
|----------|-----------------|--------|-------------------------|
| 11000010 | MY01 | 100 | Group2 |
| 11000010 | MY51 | 200 | Group1 |
| 11000018 | MY53 | 150 | Group1 |
| 11000019 | MY55 | 220 | Group1 |
| 11000030 | MY38 | 214 | Group2 |
| 11000045 | MY59 | 500 | Group1 |
| 11000056 | MY5HA | 586 | Group1 |
| 31000125 | MYHW | 564 | Group4 |
| 31000133 | MY71 | 546 | Group4 |
| 31000139 | MY73 | 4896 | Group4 |
| 31000144 | MY38 | 486 | Group4 |
| 31013425 | MYSA | 4869 | Group4 |
| 81000225 | MY38 | 489 | Group5 |
最终输出-这就是我想要的最终结果。
| Group |
|--------|
| Group1 |
| Group2 |
| Group4 |
| Group5 |
我确实尝试使用以下代码按供应商分组并在我的 主 Table 调用它,但后来我被困在业务领域。
我真正希望的是所有代码都可以是可变的(基于标准 Table),因为在代码下方我需要对范围进行硬编码。
let ValBucket =
{
{(Vendor)=>Vendor<29999999, "Group1"},
{(Vendor)=>Vendor<69999999, "Group2"},
{(Vendor)=>Vendor<89999999, "Group3"}
},
Result = List.First(List.Select(ValBucket, each _{0}(vals))){1}
in
Result
如有任何建议,我们将不胜感激!
这是一种方法。
Main 和 Vendor Criteria 是查询的名称,其中包含您所说的 table:
主要
供应商标准
请阅读代码中的注释以更好地理解算法
M 代码:编辑: 以说明样本数据与真实数据之间的差异
- 删除条件中的额外列 table
- 样本中包含 null space (
""
) 的细胞是 null
真实数据
- 将 null 替换为
""
- 真实数据有重复的供应商,其中一个或多个
Business Areas
未出现在标准 table 的业务领域列表中。
- 将算法更改为 select 组
- 您需要检查这是否始终会产生正确的结果。
let
critTbl = VendorCriteria,
//Remove unneeded column
#"Removed Columns1" = Table.RemoveColumns(critTbl,{"Column1"}),
//replace *null* with a null string, else Text.Split in next step will fail
#"Replaced Value" = Table.ReplaceValue(#"Removed Columns1",null,"",Replacer.ReplaceValue,{"Business Area"}),
//change Business Area into a List
criteria = Table.Buffer(Table.TransformColumns(#"Replaced Value", {"Business Area", each Text.Split(_,",")})),
//Get the Main table
main = Proposal2,
//add column of filtered criteria tables where the filter depends on the values in
// "Vendor Low Range" and "Vendor Upper Range"
#"Added Custom" = Table.AddColumn(main, "Custom",
each Table.SelectRows(criteria, (t)=>
[Vendor]>= t[#"Vendor Low Range"] and [Vendor] <= t[#"Vendor Upper Range"])),
//Determine which Group
//if there are multiple matches, pick the first Group
#"Added Custom1" = Table.AddColumn(#"Added Custom", "Group", each
let
x=Table.SelectRows([Custom], (t)=>
List.Contains(t[Business Area],[Business Area])),
y=if Table.RowCount(x) = 1 then x[Group]{0} else [Custom][Group]{0}
in
y,
type text),
//Remove just the Custom (table) column to display "Result"
Result= Table.RemoveColumns(#"Added Custom1","Custom"),
//Remove the other columns except "Group" to be able to produce the Sorted Distinct table of Groups
#"Removed Columns" = Table.RemoveColumns(Result,{"Vendor", "Business Area"}),
#"Removed Duplicates" = Table.Distinct(#"Removed Columns"),
#"Final Output" = Table.Sort(#"Removed Duplicates",{{"Group", Order.Ascending}})
in
#"Final Output"
结果步骤
最终输出
我在 excel 中有一份报告。我想根据 Main Table 和 Criteria Table.
中的信息获得最终输出Main Table - 这个 table 有很多列并且已经在 Power Query 中称为 Proposal
| Vendor | Business Area | Amount |
|----------|-----------------|--------|
| 11000010 | MY01 | 100 |
| 11000017 | MY51 | 200 |
| 11000018 | MY53 | 150 |
| 11000019 | MY55 | 220 |
| 11000030 | MY57 | 214 |
| 11000045 | MY59 | 500 |
| 11000056 | MY5HA | 586 |
| 31000125 | MYHW | 564 |
| 31000133 | MY71 | 546 |
| 31000139 | MY73 | 4896 |
| 31000144 | MY38 | 486 |
| 31013425 | MYSA | 4869 |
| 81000225 | MYSA | 489 |
Criteria Table- 此 table 已在 Power Query 中,称为 VendorCriteria。
如果 Business Area 为空白,但供应商在范围内,仍将属于各自的分组。
| Vendor Low Range | Vendor Upper Range | Business Area | Group |
|------------------|--------------------|-------------------------------------------------------------------|--------|
| 10000000 | 29999999 | MY51,MY53,MY55,MY57,MY59,MY5HA,MYHW,MY71,MY73,MY75,MYSA,MYSW,MYS1 | Group1 |
| 10000000 | 29999999 | MY01,MY38 | Group2 |
| 60000000 | 69999999 | | Group3 |
| 30000000 | 39999999 | | Group4 |
| 80000000 | 89999999 | | Group5 |
结果- 这只是为了根据标准 table.
显示相应的行结果| Vendor | Business Area | Amount | Base on Criterial Table |
|----------|-----------------|--------|-------------------------|
| 11000010 | MY01 | 100 | Group2 |
| 11000010 | MY51 | 200 | Group1 |
| 11000018 | MY53 | 150 | Group1 |
| 11000019 | MY55 | 220 | Group1 |
| 11000030 | MY38 | 214 | Group2 |
| 11000045 | MY59 | 500 | Group1 |
| 11000056 | MY5HA | 586 | Group1 |
| 31000125 | MYHW | 564 | Group4 |
| 31000133 | MY71 | 546 | Group4 |
| 31000139 | MY73 | 4896 | Group4 |
| 31000144 | MY38 | 486 | Group4 |
| 31013425 | MYSA | 4869 | Group4 |
| 81000225 | MY38 | 489 | Group5 |
最终输出-这就是我想要的最终结果。
| Group |
|--------|
| Group1 |
| Group2 |
| Group4 |
| Group5 |
我确实尝试使用以下代码按供应商分组并在我的 主 Table 调用它,但后来我被困在业务领域。
我真正希望的是所有代码都可以是可变的(基于标准 Table),因为在代码下方我需要对范围进行硬编码。
let ValBucket =
{
{(Vendor)=>Vendor<29999999, "Group1"},
{(Vendor)=>Vendor<69999999, "Group2"},
{(Vendor)=>Vendor<89999999, "Group3"}
},
Result = List.First(List.Select(ValBucket, each _{0}(vals))){1}
in
Result
如有任何建议,我们将不胜感激!
这是一种方法。
Main 和 Vendor Criteria 是查询的名称,其中包含您所说的 table:
主要
供应商标准
请阅读代码中的注释以更好地理解算法
M 代码:编辑: 以说明样本数据与真实数据之间的差异
- 删除条件中的额外列 table
- 样本中包含 null space (
""
) 的细胞是null
真实数据- 将 null 替换为
""
- 将 null 替换为
- 真实数据有重复的供应商,其中一个或多个
Business Areas
未出现在标准 table 的业务领域列表中。- 将算法更改为 select 组
- 您需要检查这是否始终会产生正确的结果。
let
critTbl = VendorCriteria,
//Remove unneeded column
#"Removed Columns1" = Table.RemoveColumns(critTbl,{"Column1"}),
//replace *null* with a null string, else Text.Split in next step will fail
#"Replaced Value" = Table.ReplaceValue(#"Removed Columns1",null,"",Replacer.ReplaceValue,{"Business Area"}),
//change Business Area into a List
criteria = Table.Buffer(Table.TransformColumns(#"Replaced Value", {"Business Area", each Text.Split(_,",")})),
//Get the Main table
main = Proposal2,
//add column of filtered criteria tables where the filter depends on the values in
// "Vendor Low Range" and "Vendor Upper Range"
#"Added Custom" = Table.AddColumn(main, "Custom",
each Table.SelectRows(criteria, (t)=>
[Vendor]>= t[#"Vendor Low Range"] and [Vendor] <= t[#"Vendor Upper Range"])),
//Determine which Group
//if there are multiple matches, pick the first Group
#"Added Custom1" = Table.AddColumn(#"Added Custom", "Group", each
let
x=Table.SelectRows([Custom], (t)=>
List.Contains(t[Business Area],[Business Area])),
y=if Table.RowCount(x) = 1 then x[Group]{0} else [Custom][Group]{0}
in
y,
type text),
//Remove just the Custom (table) column to display "Result"
Result= Table.RemoveColumns(#"Added Custom1","Custom"),
//Remove the other columns except "Group" to be able to produce the Sorted Distinct table of Groups
#"Removed Columns" = Table.RemoveColumns(Result,{"Vendor", "Business Area"}),
#"Removed Duplicates" = Table.Distinct(#"Removed Columns"),
#"Final Output" = Table.Sort(#"Removed Duplicates",{{"Group", Order.Ascending}})
in
#"Final Output"
结果步骤
最终输出