0
votes

I am trying to stack three columns like below on Power BI. My example dataset is following:

Processo Finalidade 1 Finalidade 2 Finalidade 3
1 Maçã Banana Mamão
2 Banana
3 Mamão Banana

However, I would like that it stay like this:

Processo Finalidade
1 Maçã
2 Banana
3 Mamão
1 Banana
2
3 Banana
1 Mamão
2
3

I tried to transpose columns but I can't this result.

2
In power query, select the 3 Finalidade columns and unpivot them. This will result with something closer to your desired format.Michael Vine

2 Answers

0
votes

Based on your input data and the hint in the comments I did it like that

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

Result looks like

enter image description here

But the solution strongly depends on the naming of the columns Finalidade 1 etc.

PS Another solution which is independet of the naming of the columns could look like that

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
0
votes

Replace null with some characters. Unpivot other columns. Replace characters with null. Remove extra column

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"