Merge DAX:有选择地合并两个表(基于行中的值)
我有两张桌子:Merge DAX:有选择地合并两个表(基于行中的值),merge,powerbi,dax,powerquery,Merge,Powerbi,Dax,Powerquery,我有两张桌子: 带有部门和部门内实体的表 表中列出了每个实体或部门中所有实体的项目 一些项目特定于[组织],而其他项目则与[部门]中的所有[组织]相关。在这些情况下,使用“所有实体”值 我想基于[Division]合并这两个表,但仅针对Plan[Org.]=“所有实体”,而具有其他值(例如“France”、“Germany”)的行应该只是重复 生成的表应如下所示: 表之间的物理关系不起作用,因为组织表中缺少“所有实体”值,但如果需要,可以在项目表中添加额外的[Entity id]列 我更喜欢DA
谢谢大家! 在电源查询中遵循以下步骤- 步骤1:在表组织中创建自定义列*,如下所示- 这是输出- 第二步:从表项目中创建2复制表,并将其命名为-项目所有项目和项目其他项目 第三步:转到高级编辑器,查看表项目所有内容,并替换以下代码-
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXItLcovSAUy3IoS85JBjICi/KzU5BIFQwUFBaVYnWglI2Rl7qlFuYl5lUjqjGDqjPGrM4YqM0FW5piTo5CaV5JZkplajKTWBGamKRGKTaFqzYBizhmZeYkgPYnFJUhKzGDmmSOpwWGcuVJsLAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [id = _t, Division = _t, #"Org." = _t, #"Project " = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"id", Int64.Type}, {"Division", type text}, {"Org.", type text}, {"Project ", type text}}),
#"Trimmed Text" = Table.TransformColumns(#"Changed Type",{{"Division", Text.Trim, type text}, {"Org.", Text.Trim, type text}}),
#"Filtered Rows" = Table.SelectRows(#"Trimmed Text", each ([#"Org."] = "All entities")),
#"Merged Queries" = Table.NestedJoin(#"Filtered Rows", {"Division", "Org."}, Organization, {"Division", "Custom"}, "Organization", JoinKind.LeftOuter),
#"Expanded Organization" = Table.ExpandTableColumn(#"Merged Queries", "Organization", {"Entity "}, {"Organization.Entity "}),
#"Reordered Columns" = Table.ReorderColumns(#"Expanded Organization",{"id", "Division", "Org.", "Organization.Entity ", "Project "}),
#"Renamed Columns" = Table.RenameColumns(#"Reordered Columns",{{"Organization.Entity ", "Entity"}})
in
#"Renamed Columns"
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXItLcovSAUy3IoS85JBjICi/KzU5BIFQwUFBaVYnWglI2Rl7qlFuYl5lUjqjGDqjPGrM4YqM0FW5piTo5CaV5JZkplajKTWBGamKRGKTaFqzYBizhmZeYkgPYnFJUhKzGDmmSOpwWGcuVJsLAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [id = _t, Division = _t, #"Org." = _t, #"Project " = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"id", Int64.Type}, {"Division", type text}, {"Org.", type text}, {"Project ", type text}}),
#"Trimmed Text" = Table.TransformColumns(#"Changed Type",{{"Division", Text.Trim, type text}, {"Org.", Text.Trim, type text}}),
#"Filtered Rows" = Table.SelectRows(#"Trimmed Text", each [#"Org."] <> "All entities"),
#"Merged Queries" = Table.NestedJoin(#"Filtered Rows", {"Division", "Org."}, Organization, {"Division", "Entity "}, "Organization", JoinKind.LeftOuter),
#"Expanded Organization" = Table.ExpandTableColumn(#"Merged Queries", "Organization", {"Entity "}, {"Organization.Entity "}),
#"Reordered Columns" = Table.ReorderColumns(#"Expanded Organization",{"id", "Division", "Org.", "Organization.Entity ", "Project "}),
#"Renamed Columns" = Table.RenameColumns(#"Reordered Columns",{{"Organization.Entity ", "Entity"}})
in
#"Renamed Columns"
第4步:转到表项目的高级编辑器,并替换以下代码-
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXItLcovSAUy3IoS85JBjICi/KzU5BIFQwUFBaVYnWglI2Rl7qlFuYl5lUjqjGDqjPGrM4YqM0FW5piTo5CaV5JZkplajKTWBGamKRGKTaFqzYBizhmZeYkgPYnFJUhKzGDmmSOpwWGcuVJsLAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [id = _t, Division = _t, #"Org." = _t, #"Project " = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"id", Int64.Type}, {"Division", type text}, {"Org.", type text}, {"Project ", type text}}),
#"Trimmed Text" = Table.TransformColumns(#"Changed Type",{{"Division", Text.Trim, type text}, {"Org.", Text.Trim, type text}}),
#"Filtered Rows" = Table.SelectRows(#"Trimmed Text", each ([#"Org."] = "All entities")),
#"Merged Queries" = Table.NestedJoin(#"Filtered Rows", {"Division", "Org."}, Organization, {"Division", "Custom"}, "Organization", JoinKind.LeftOuter),
#"Expanded Organization" = Table.ExpandTableColumn(#"Merged Queries", "Organization", {"Entity "}, {"Organization.Entity "}),
#"Reordered Columns" = Table.ReorderColumns(#"Expanded Organization",{"id", "Division", "Org.", "Organization.Entity ", "Project "}),
#"Renamed Columns" = Table.RenameColumns(#"Reordered Columns",{{"Organization.Entity ", "Entity"}})
in
#"Renamed Columns"
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXItLcovSAUy3IoS85JBjICi/KzU5BIFQwUFBaVYnWglI2Rl7qlFuYl5lUjqjGDqjPGrM4YqM0FW5piTo5CaV5JZkplajKTWBGamKRGKTaFqzYBizhmZeYkgPYnFJUhKzGDmmSOpwWGcuVJsLAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [id = _t, Division = _t, #"Org." = _t, #"Project " = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"id", Int64.Type}, {"Division", type text}, {"Org.", type text}, {"Project ", type text}}),
#"Trimmed Text" = Table.TransformColumns(#"Changed Type",{{"Division", Text.Trim, type text}, {"Org.", Text.Trim, type text}}),
#"Filtered Rows" = Table.SelectRows(#"Trimmed Text", each [#"Org."] <> "All entities"),
#"Merged Queries" = Table.NestedJoin(#"Filtered Rows", {"Division", "Org."}, Organization, {"Division", "Entity "}, "Organization", JoinKind.LeftOuter),
#"Expanded Organization" = Table.ExpandTableColumn(#"Merged Queries", "Organization", {"Entity "}, {"Organization.Entity "}),
#"Reordered Columns" = Table.ReorderColumns(#"Expanded Organization",{"id", "Division", "Org.", "Organization.Entity ", "Project "}),
#"Renamed Columns" = Table.RenameColumns(#"Reordered Columns",{{"Organization.Entity ", "Entity"}})
in
#"Renamed Columns"
这是最终输出-
我已经更改了表
项目的列名称。函数CROSSJOIN
不希望有重复的列名<代码>组织机构
表相同
项目表
+------------+------------------+--------------+-----------+
| id_project | Division_project | Org. | Project |
+------------+------------------+--------------+-----------+
| 1 | Europe | France | Project 1 |
+------------+------------------+--------------+-----------+
| 2 | Europe | Germany | Project 2 |
+------------+------------------+--------------+-----------+
| 3 | Europe | Germany | Project 3 |
+------------+------------------+--------------+-----------+
| 4 | Europe | All entities | Project 4 |
+------------+------------------+--------------+-----------+
| 5 | Europe | All entities | Project 5 |
+------------+------------------+--------------+-----------+
| 6 | China | East | Project 6 |
+------------+------------------+--------------+-----------+
| 7 | China | All entities | Project 7 |
+------------+------------------+--------------+-----------+
代码使用两个变量\u directrel
和\u allentity
,它们的输出对应于表。其中的每一个都使用一个由FILTER语句包围的交叉连接。第一种方法使用部门和实体之间的直接关系。第二个变量仅使用分割作为链接,并仅对所有实体
过滤项目
在RETURN
语句之后,有一个UNION
语句,它在表之间进行联合,并由SELECTCOLUMNS
包围,以控制列的顺序
Merge =
VAR _directrel =
GROUPBY (
FILTER (
CROSSJOIN ( Organization, Project ),
[Division] = [Division_project]
&& [Entity] = [Org.]
),
[id_project],
[Division],
[Org.],
[Entity],
[Project]
)
VAR _allentity =
GROUPBY (
FILTER (
CROSSJOIN ( Organization, FILTER ( Project, [Org.] = "All Entities" ) ),
[Division] = [Division_project]
),
[id_project],
[Division],
[Org.],
[Entity],
[Project]
)
RETURN
SELECTCOLUMNS (
UNION ( _allentity, _directrel ),
"id", [id_project],
"Division", [Division],
"Org.", [Org.],
"Entity", [Entity],
"Project", [Project]
)
输出:
谢谢你,安杰洛!也需要解释一下。帮个大忙!谢谢你,Mkrabani!我可以遵循你的解决方案。我们也将尝试此解决方案。甚至可能会坚持这一点。谢谢希望您能通过这种方式找到您的预期输出。
+------------+------------------+--------------+-----------+
| id_project | Division_project | Org. | Project |
+------------+------------------+--------------+-----------+
| 1 | Europe | France | Project 1 |
+------------+------------------+--------------+-----------+
| 2 | Europe | Germany | Project 2 |
+------------+------------------+--------------+-----------+
| 3 | Europe | Germany | Project 3 |
+------------+------------------+--------------+-----------+
| 4 | Europe | All entities | Project 4 |
+------------+------------------+--------------+-----------+
| 5 | Europe | All entities | Project 5 |
+------------+------------------+--------------+-----------+
| 6 | China | East | Project 6 |
+------------+------------------+--------------+-----------+
| 7 | China | All entities | Project 7 |
+------------+------------------+--------------+-----------+
Merge =
VAR _directrel =
GROUPBY (
FILTER (
CROSSJOIN ( Organization, Project ),
[Division] = [Division_project]
&& [Entity] = [Org.]
),
[id_project],
[Division],
[Org.],
[Entity],
[Project]
)
VAR _allentity =
GROUPBY (
FILTER (
CROSSJOIN ( Organization, FILTER ( Project, [Org.] = "All Entities" ) ),
[Division] = [Division_project]
),
[id_project],
[Division],
[Org.],
[Entity],
[Project]
)
RETURN
SELECTCOLUMNS (
UNION ( _allentity, _directrel ),
"id", [id_project],
"Division", [Division],
"Org.", [Org.],
"Entity", [Entity],
"Project", [Project]
)