取消透视 Power BI table 以将列减少为仅度量

Unpivoting a Power BI table to reduce columns to only measures

我有一个针对我的数据源的查询,returns 一个维度(例如,团队名称)和几个与同一维度的交叉相关的度量(例如,胜利,失败)。例如:

+===========+===============+===============+===============+===============+===========+===========+
|           | Cardinals - W | Cardinals - L | Blue Jays - W | Blue Jays - L | Hawks - W | Hawks - L |
+===========+===============+===============+===============+===============+===========+===========+
| Cardinals | x             | x             | 5             | 10            | 1         | 2         |
+-----------+---------------+---------------+---------------+---------------+-----------+-----------+
| Blue Jays | 10            | 5             | x             | x             | 8         | 4         |
+-----------+---------------+---------------+---------------+---------------+-----------+-----------+
| Hawks     | 2             | 1             | 4             | 8             | x         | x         |
+===========+===============+===============+===============+===============+===========+===========+

我想做的是让这个逆透视给我 'A' 和 'B' 列以及非团队特定的 'W' 和 'L' 列.例如:

+===========+===========+====+====+
| Team A    | Team B    | W  | L  |
+===========+===========+====+====+
| Blue Jays | Cardinals | 10 | 5  |
+-----------+-----------+----+----+
| Blue Jays | Hawks     | 8  | 4  |
+-----------+-----------+----+----+
| Cardinals | Blue Jays | 5  | 10 |
+-----------+-----------+----+----+
| Cardinals | Hawks     | 1  | 2  |
+-----------+-----------+----+----+
| Hawks     | Blue Jays | 4  | 8  |
+-----------+-----------+----+----+
| Hawks     | Cardinals | 2  | 1  |
+===========+===========+====+====+

(因为我的用例实际上有点不同,所以我实际上也想要 {Team A=Blue Jays, Team B=Blue Jays, W=x, L=x} 的行)。

如果我在查询编辑器中执行 Unpivot,我最终会得到

+===========+===============+=======+
|           | Attribute     | Value |
+===========+===============+=======+
| Cardinals | Cardinals - W | x     |
+-----------+---------------+-------+
| Cardinals | Cardinals - L | x     |
+-----------+---------------+-------+
| Cardinals | Blue Jays - W | 5     |
+-----------+---------------+-------+
| Cardinals | Blue Jays - L | 10    |
+-----------+---------------+-------+
| Cardinals | Hawks - W     | 1     |
+-----------+---------------+-------+
| Cardinals | Hawks - L     | 2     |
+-----------+---------------+-------+
| Blue Jays | Cardinals - W | 10    |
+-----------+---------------+-------+
| Blue Jays | Cardinals - L | 5     |
+-----------+---------------+-------+
| Blue Jays | Blue Jays - W | x     |
+-----------+---------------+-------+
| (more rows removed for brevity)   |
+===========+===============+=======+

有什么合理的方法可以在我的数据集中以这种方式实现数据透视表吗?

要做到这一点:

我从查询编辑器中的表 1 开始:

然后我选择了 Column1 和 Unpivoted Other Columns:

...产生了这个:

然后我添加了两个新列:一个是从属性列中提取的 W 或 L,另一个是从属性列中提取的团队名称。我使用 Text.EndText.Start 进行这些提取:

然后我选择了“自定义”列并在其上旋转,并为关联值选择了“值”列:

...产生了这个:

然后我删除了属性列。 然后我选择了 W 列并填充。 然后我选择了 L 列并向下填充。 然后我选择了 W 列并过滤掉了 x。 然后我右键单击 table 的左上角并选择删除重复项。 最后,我将 Column1 和 Custom.1 列重命名为 Team A 和 Team B。

这是我的查询代码:

let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Cardinals - W", type any}, {"Cardinals - L", type any}, {"Blue Jays - W", type any}, {"Blue Jays - L", type any}, {"Hawks - W", type any}, {"Hawks - L", type any}}),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Column1"}, "Attribute", "Value"),
#"Added Custom" = Table.AddColumn(#"Unpivoted Other Columns", "Custom", each Text.End([Attribute],1)),
#"Added Custom1" = Table.AddColumn(#"Added Custom", "Custom.1", each Text.Start([Attribute], Text.Length([Attribute])-4)),
#"Pivoted Column" = Table.Pivot(#"Added Custom1", List.Distinct(#"Added Custom1"[Custom]), "Custom", "Value"),
#"Removed Columns" = Table.RemoveColumns(#"Pivoted Column",{"Attribute"}),
#"Filled Up" = Table.FillUp(#"Removed Columns",{"W"}),
#"Filled Down" = Table.FillDown(#"Filled Up",{"L"}),
#"Filtered Rows" = Table.SelectRows(#"Filled Down", each ([W] <> "x")),
#"Removed Duplicates" = Table.Distinct(#"Filtered Rows"),
#"Renamed Columns" = Table.RenameColumns(#"Removed Duplicates",{{"Column1", "Team A"}, {"Custom.1", "Team B"}})
in
#"Renamed Columns"

希望对您有所帮助。

与 Marc 的回答类似,但步骤少了一些。

从逆透视数据开始。

Select 属性列和 select 拆分列 -> 按分隔符。 PowerBI 通常足够智能,可以自动检测分隔符,但您可以根据需要在对话框中对其进行编辑。

然后 select Attribute.2 列和 select 数据透视列。将值列更改为值,然后在高级选项下,将聚合值函数更改为不聚合。

好了。