我有一个针对我的数据源的查询,该数据源返回一个维度(例如,团队名称),以及与相同维度的十字架相关联的若干度量(例如,胜利,亏损)。例如:
+===========+===============+===============+===============+===============+===========+===========+
| | 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) |
+===========+===============+=======+
有没有合理的方法在我的数据集中以这种方式实现数据透视?
答案 0 :(得分:2)
与Marc的答案类似,但步数较少。
从不透明的数据开始。
选择属性列,然后选择拆分列 - >通过Delimiter。 PowerBI通常足够智能自动检测分隔符,但如有必要,您可以在对话框中对其进行编辑。
然后选择Attribute.2列并选择Pivot Column。将值列更改为值,并在高级选项下,将聚合值函数更改为不聚合。
然后你去。
答案 1 :(得分:1)
要做到这一点:
我在查询编辑器中以Table1开头:
然后我选择了Column1和Unpivoted Other Columns:
...产生了这个:
然后我添加了两个新列:一个是从属性列中提取的W或L,另一个是仅从属性列中提取的团队名称。我使用Text.End
和Text.Start
进行了这些提取:
然后我选择了自定义列并对其进行了旋转,并为关联值选择了“值”列:
...产生了这个:
然后我删除了属性列。 然后我选择了W列并填满了。 然后我选择了L列和Filled Down。 然后我选择了W列并过滤掉了x。 然后我右键单击表的左上角并选择Remove Duplicates。 最后,我将Column1和Custom.1列重命名为A队和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"
我希望这会有所帮助。