DAX:有选择地合并两个表(基于一行中的值)
Posted
技术标签:
【中文标题】DAX:有选择地合并两个表(基于一行中的值)【英文标题】:DAX: Merge two tables selectively (based on a value in a row) 【发布时间】:2020-09-28 13:35:02 【问题描述】:我有两张桌子:
-
包含部门和部门内实体的表
表格包含每个实体或部门中所有实体确定的项目。
**Table 1: Organization** id Division Entity - - - - - - - - - - - - - 0 Europe France 1 Europe Germany 2 Europe Italy 3 Europe Spain 4 China North 5 China East 6 China West 7 China South
**Table 2: Project** id Division 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
有些项目是特定于 [Org.] 的,而其他项目则与 [Division] 中的所有 [Org.] 相关。在这些情况下,使用值“所有实体”。
我想基于 [Division] 合并两个表,但仅针对 Plan[Org.] = "Allentities",而具有其他值(例如 "France"、"Germany")的行应该重复。
结果表应如下所示:
**Table 3: Merge** id Division Org. Entity Project - - - - - - - - - - - - - - - - - - - - - - - - - - 1 Europe France France Project 1 2 Europe Germany Germany Project 2 3 Europe Germany Germany Project 3 4 Europe All entities France Project 4 4 Europe All entities Germany Project 4 4 Europe All entities Spain Project 4 4 Europe All entities Italy Project 4 5 Europe All entities France Project 5 5 Europe All entities Germany Project 5 5 Europe All entities Spain Project 5 5 Europe All entities Italy Project 5 6 China East East Project 6 7 China All entities North Project 7 7 China All entities East Project 7 7 China All entities South Project 7 7 China All entities West Project 7
由于组织表中缺少“所有实体”值,表之间的物理关系不起作用,但如果需要,可以在项目表中使用额外的 [Entity id] 列添加它。
我更喜欢 DAX。 PowerQuery 中的解决方案不太受欢迎,因为我对 M 不是很流利,但如果 可用。
谢谢!
【问题讨论】:
【参考方案1】:在 Power Query-
中执行以下步骤第 1 步:在Organization表中创建一个自定义列*,如下所示-
这是输出-
步骤 2: 从表 Project 创建 2 复制 表并将它们命名为 - Project_all_emtities 和 Project_others
第 3 步:转到表 Project_all_emtities 的高级编辑器并替换以下代码-
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"
第 4 步:转到高级编辑器表Project_others并替换以下代码-
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"
第 5 步:通过在 Project_all_emtities 和 Project_others 中附加以下代码来创建新表-
let
Source = Table.Combine(Project_all_emtities, Project_others),
#"Sorted Rows" = Table.Sort(Source,"Division", Order.Descending, "Project ", Order.Ascending)
in
#"Sorted Rows"
这是最终的输出-
【讨论】:
谢谢 mkRabbani!可以按照你的解决方案。也会尝试这个解决方案。甚至可以坚持这一点。谢谢!! 希望您能通过这种方式找到预期的输出。【参考方案2】:我已经更改了表 Project
的列名。函数CROSSJOIN
不喜欢有重复的列名。 Organization
表是一样的。
项目表
+------------+------------------+--------------+-----------+
| 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 |
+------------+------------------+--------------+-----------+
代码使用两个变量_directrel
和_allentity
,它们的输出对应于表格。其中每一个都使用由 FILTER 语句包围的CROSSJOIN
。第一个使用部门和实体之间的直接关系。第二个变量仅使用除法作为链接,并仅针对All Entities
过滤Project
。
在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]
)
输出:
【讨论】:
谢谢安吉洛!也是为了解释。大有帮助!以上是关于DAX:有选择地合并两个表(基于一行中的值)的主要内容,如果未能解决你的问题,请参考以下文章