【问题标题】:Powerpivot retrieve cardinal value from data range of table2 to add as column to table1Powerpivot 从 table2 的数据范围中检索基值以将其作为列添加到 table1
【发布时间】:2021-01-27 16:40:10
【问题描述】:

使用 Excel 365 Powerquery。

我有两个数据源 table1table2 具有以下条目:

table1:

ID | salary
===========
1  |    10
2  |  1000

table2:
ID | inclminval | exclmaxval | class | display
20 |         0  |        100 |    P1 | Poor man
30 |       100  |       9999 |    P9 | Wealthy

我想追加到 table1:对于每个条目 table1.salary,

  • 将其与table2的范围table2.inclminval
  • 使用 table2.class 的匹配对应条目作为 table1 中的新计算列。

【问题讨论】:

  • 你能告诉我们想要的输出吗?你也有一些你尝试过的代码吗?

标签: excel powerquery


【解决方案1】:

在 table2 上,添加列 ... 自定义列 ... 列名称 custom 和公式 =1

一个表1,添加列...自定义列...列名custom和公式=1

主页 .. 合并查询 ...

使用完全外连接选择并匹配两个表中的 custom

使用新列顶部的箭头 [x] 展开 inclminvalexclmaxcalclassdisplay

添加列...自定义列,公式类似于

= if [salary]>=[inclminval] and [salary]<[exclmaxcal] then "keep" else "remove"

使用新列顶部的箭头过滤 [x] 保留

删除多余的列

table1 的示例代码

let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Added Custom" = Table.AddColumn(Source, "Custom", each 1),
#"Merged Queries" = Table.NestedJoin(#"Added Custom",{"Custom"},Table2,{"Custom"},"Table2",JoinKind.FullOuter),
#"Expanded Table2" = Table.ExpandTableColumn(#"Merged Queries", "Table2", {"inclminval", "exclmaxcal", "class", "display"}, {"inclminval", "exclmaxcal", "class", "display"}),
#"Added Custom1" = Table.AddColumn(#"Expanded Table2", "Custom.1", each if [salary]>=[inclminval] and [salary]<[exclmaxcal] then "keep" else "remove"),
#"Filtered Rows" = Table.SelectRows(#"Added Custom1", each ([Custom.1] = "keep")),
#"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Custom", "inclminval", "exclmaxcal", "display", "Custom.1"})
in #"Removed Columns"

【讨论】:

  • 我想我明白你在做什么。您扩展 table2 并将 min 和 max 之间的每个可能值添加到您与 table1 的薪水内部连接的附加列中。现在工资当然可以是分数,所以这个解决方案行不通?
  • 为什么不直接交叉连接呢? AddCustom = Table.AddColumn(Source, "Custom", each Table2)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-30
  • 1970-01-01
  • 2020-05-31
  • 1970-01-01
  • 1970-01-01
  • 2015-05-07
  • 1970-01-01
相关资源
最近更新 更多