使用两个没有公共键的不同表创建矩阵

Creating matrix with two different tables with no common key

我想在 PowerBI 中基于两个没有公共键的表创建一个矩阵,非常感谢您的帮助。

Table 1 看起来像这样:

如您所见,它包含一个特定的等级。

Table 2 看起来像这样(摘录):

如您所见,每个类别、子类别和子子类别在 Table 2.

中都是独立的一列

目标是让来自 Table 1 的所有类别、子类别和子子类别及其各自的 hirachie 作为矩阵中的行,即客户(C1、C2、C3、C4、C5)在列中,并将 Table 2 中的值作为值。结果应如下所示:

我创建了一个包含示例数据的 PowerBI 文件:PowerBi file

有谁知道如何存档此解决方案?欢迎任何帮助。 ‌‌

此致, 法比

使用M/Powerquery:

表 1 的代码;使用索引

创建排序的层次结构
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Added Index" = Table.AddIndexColumn(Source, "Index", 0, 1, Int64.Type),
#"Added Index1" = Table.AddIndexColumn(#"Added Index", "Index.1", 0.1, 1, Int64.Type),
#"Added Index2" = Table.AddIndexColumn(#"Added Index1", "Index.2", 0.11, 1, Int64.Type),

#"Removed Duplicates" = Table.Distinct(#"Added Index2", {"Category"}),
#"Removed Other Columns" = Table.SelectColumns(#"Removed Duplicates",{"Category", "Index"}),
#"Added Custom" = Table.AddColumn(#"Removed Other Columns", "Level", each ""),

#"Removed Duplicates2" = Table.Distinct(#"Added Index2", {"Subcategory"}),
#"Removed Other Columns2" = Table.SelectColumns(#"Removed Duplicates2",{"Subcategory", "Index.1"}),
#"Added Custom1" = Table.AddColumn(#"Removed Other Columns2", "Level", each "     "),
#"Renamed Columns" = Table.RenameColumns(#"Added Custom1",{{"Index.1", "Index"}, {"Subcategory", "Category"}}),

#"Removed Other Columns3" = Table.SelectColumns(#"Added Index2",{"Subsubcategory", "Index.2"}),
#"Added Custom2" = Table.AddColumn(#"Removed Other Columns3", "Level", each "          "),
#"Renamed Columns1" = Table.RenameColumns(#"Added Custom2",{{"Index.2", "Index"}, {"Subsubcategory", "Category"}}),

combined =  #"Added Custom" & #"Renamed Columns" & #"Renamed Columns1",
#"Sorted Rows" = Table.Sort(combined,{{"Index", Order.Ascending}}),
#"Added Index3" = Table.AddIndexColumn(#"Sorted Rows", "Index.1", 0, 1, Int64.Type),
#"Added Custom3" = Table.AddColumn(#"Added Index3", "Hierarchy", each [Level]&[Category])

in #"Added Custom3"

表 2 的代码(成为输出)

let  Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"Customer"}, "Attribute", "Value"),
#"Merged Queries" = Table.NestedJoin(#"Unpivoted Other Columns", {"Attribute"}, Table1, {"Category"}, "Table1", JoinKind.LeftOuter),
#"Expanded Table2" = Table.ExpandTableColumn(#"Merged Queries", "Table1", {"Index.1", "Hierarchy"}, {"Index.1", "Hierarchy"}),
#"Pivoted Column" = Table.Pivot(#"Expanded Table2", List.Distinct(#"Expanded Table2"[Customer]), "Customer", "Value"),
#"Sorted Rows" = Table.Sort(#"Pivoted Column",{{"Index.1", Order.Ascending}}),
#"Removed Columns" = Table.RemoveColumns(#"Sorted Rows",{"Attribute", "Index.1"})
in  #"Removed Columns"

也可以将 Table2 编码为以 headers 开始的行,然后转置