使用 Power Query 堆叠列
Stack columns with Power Query
我正在尝试在 Power BI 上堆叠如下所示的三列。
我的示例数据集如下:
Processo
Finalidade 1
Finalidade 2
Finalidade 3
1
Maçã
Banana
Mamão
2
Banana
3
Mamão
Banana
但是,我希望它保持这样:
Processo
Finalidade
1
Maçã
2
Banana
3
Mamão
1
Banana
2
3
Banana
1
Mamão
2
3
我尝试转置列,但我做不到这个结果。
根据您输入的数据和评论中的提示,我这样做了
let
Source = Excel.CurrentWorkbook(){[Name="tblData"]}[Content],
Step1 = Table.TransformColumnTypes(Source,{{"Processo", Int64.Type}, {"Finalidade 1", type text}, {"Finalidade 2", type text}, {"Finalidade 3", type text}}),
tblUnPivot = Table.ReplaceValue(Step1,null,"",Replacer.ReplaceValue,{"Finalidade 2"}),
replaceValues = Table.ReplaceValue(tblUnPivot,null,"",Replacer.ReplaceValue,{"Finalidade 3"}),
unpivot = Table.UnpivotOtherColumns(replaceValues, {"Processo"}, "Attribut", "Wert"),
sorted = Table.Sort(unpivot,{{"Attribut", Order.Ascending}, {"Processo", Order.Ascending}}),
removeCol = Table.RemoveColumns(sorted,{"Attribut"}),
renameCol = Table.RenameColumns(removeCol,{{"Wert", "Finalidade"}})
in
renameCol
结果看起来像
但解决方案在很大程度上取决于列的命名 Finalidade 1 等
PS 另一个独立于列命名的解决方案可能看起来像这样
let
Source = Excel.CurrentWorkbook(){[Name="tblData"]}[Content],
Step1 = Table.TransformColumnTypes(Source,{{"Processo", Int64.Type}, {"Finalidade 1", type text}, {"Finalidade 2", type text}, {"Finalidade 3", type text}}),
tblUnPivot = Table.ReplaceValue(Step1,null,"",Replacer.ReplaceValue,{"Finalidade 2"}),
replaceValues = Table.ReplaceValue(tblUnPivot,null,"",Replacer.ReplaceValue,{"Finalidade 3"}),
unpivot = Table.UnpivotOtherColumns(replaceValues, {"Processo"}, "Attribut", "Wert"),
addColumn = Table.AddColumn(unpivot, "colPos", each List.PositionOf(Table.ColumnNames(Step1),[Attribut])),
#"Neu angeordnete Spalten" = Table.ReorderColumns(addColumn,{"Processo", "colPos", "Attribut", "Wert"}),
removeCol1 = Table.RemoveColumns(#"Neu angeordnete Spalten",{"Attribut"}),
sortRows = Table.Sort(removeCol1,{{"colPos", Order.Ascending}, {"Processo", Order.Ascending}}),
renameCol = Table.RenameColumns(sortRows,{{"Wert", "Fianlidade"}}),
removeCol2 = Table.RemoveColumns(renameCol,{"colPos"})
in
removeCol2
用一些字符替换空值。逆透视其他列。用 null 替换字符。删除多余的列
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Replaced Value" = Table.ReplaceValue(Source,null,"~~~",Replacer.ReplaceValue,{"Finalidade 1", "Finalidade 2", "Finalidade 3"}),
#"Unpivoted Other Columns1" = Table.UnpivotOtherColumns(#"Replaced Value", {"Processo"}, "Attribute", "Value"),
#"Replaced Value1" = Table.ReplaceValue(#"Unpivoted Other Columns1","~~~",null,Replacer.ReplaceValue,{"Value"}),
#"Removed Columns" = Table.RemoveColumns(#"Replaced Value1",{"Attribute"})
in #"Removed Columns"
我正在尝试在 Power BI 上堆叠如下所示的三列。 我的示例数据集如下:
Processo | Finalidade 1 | Finalidade 2 | Finalidade 3 |
---|---|---|---|
1 | Maçã | Banana | Mamão |
2 | Banana | ||
3 | Mamão | Banana |
但是,我希望它保持这样:
Processo | Finalidade |
---|---|
1 | Maçã |
2 | Banana |
3 | Mamão |
1 | Banana |
2 | |
3 | Banana |
1 | Mamão |
2 | |
3 |
我尝试转置列,但我做不到这个结果。
根据您输入的数据和评论中的提示,我这样做了
let
Source = Excel.CurrentWorkbook(){[Name="tblData"]}[Content],
Step1 = Table.TransformColumnTypes(Source,{{"Processo", Int64.Type}, {"Finalidade 1", type text}, {"Finalidade 2", type text}, {"Finalidade 3", type text}}),
tblUnPivot = Table.ReplaceValue(Step1,null,"",Replacer.ReplaceValue,{"Finalidade 2"}),
replaceValues = Table.ReplaceValue(tblUnPivot,null,"",Replacer.ReplaceValue,{"Finalidade 3"}),
unpivot = Table.UnpivotOtherColumns(replaceValues, {"Processo"}, "Attribut", "Wert"),
sorted = Table.Sort(unpivot,{{"Attribut", Order.Ascending}, {"Processo", Order.Ascending}}),
removeCol = Table.RemoveColumns(sorted,{"Attribut"}),
renameCol = Table.RenameColumns(removeCol,{{"Wert", "Finalidade"}})
in
renameCol
结果看起来像
但解决方案在很大程度上取决于列的命名 Finalidade 1 等
PS 另一个独立于列命名的解决方案可能看起来像这样
let
Source = Excel.CurrentWorkbook(){[Name="tblData"]}[Content],
Step1 = Table.TransformColumnTypes(Source,{{"Processo", Int64.Type}, {"Finalidade 1", type text}, {"Finalidade 2", type text}, {"Finalidade 3", type text}}),
tblUnPivot = Table.ReplaceValue(Step1,null,"",Replacer.ReplaceValue,{"Finalidade 2"}),
replaceValues = Table.ReplaceValue(tblUnPivot,null,"",Replacer.ReplaceValue,{"Finalidade 3"}),
unpivot = Table.UnpivotOtherColumns(replaceValues, {"Processo"}, "Attribut", "Wert"),
addColumn = Table.AddColumn(unpivot, "colPos", each List.PositionOf(Table.ColumnNames(Step1),[Attribut])),
#"Neu angeordnete Spalten" = Table.ReorderColumns(addColumn,{"Processo", "colPos", "Attribut", "Wert"}),
removeCol1 = Table.RemoveColumns(#"Neu angeordnete Spalten",{"Attribut"}),
sortRows = Table.Sort(removeCol1,{{"colPos", Order.Ascending}, {"Processo", Order.Ascending}}),
renameCol = Table.RenameColumns(sortRows,{{"Wert", "Fianlidade"}}),
removeCol2 = Table.RemoveColumns(renameCol,{"colPos"})
in
removeCol2
用一些字符替换空值。逆透视其他列。用 null 替换字符。删除多余的列
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Replaced Value" = Table.ReplaceValue(Source,null,"~~~",Replacer.ReplaceValue,{"Finalidade 1", "Finalidade 2", "Finalidade 3"}),
#"Unpivoted Other Columns1" = Table.UnpivotOtherColumns(#"Replaced Value", {"Processo"}, "Attribute", "Value"),
#"Replaced Value1" = Table.ReplaceValue(#"Unpivoted Other Columns1","~~~",null,Replacer.ReplaceValue,{"Value"}),
#"Removed Columns" = Table.RemoveColumns(#"Replaced Value1",{"Attribute"})
in #"Removed Columns"