Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
3 years ago
Solved

Facing challenges in pivoting the data

Hi Team,

Please help me to transform the below data in required format

I have some data in the below format.

AttributeValue
Emp nameJohn
DeptMathematics
Emp ID123
Emp nameDaniel
DeptScience
Emp ID124
Emp nameMohammed
DeptHistory
Emp ID125
Emp nameAndreas
DeptEnglish
Emp ID126
Emp nameRichard
DeptGeography
Emp ID127

 

Now i need to transform this as below.

Emp IDEmp nameDept
JohnMathematics123
DanielScience124
MohammedHistory125
AndreasEnglish126
RichardGeography127

 

Regards,

Priyanga

  • Hi Anonymous ,
    that is basically an unstacking exercise where you need an ID column for each row. You can create that as follows:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Zc0/C8IwEAXwryI3d/H/LLRohS46hgxHejQHzSUkWfz2VhGJcbx378dTCjoXVoKOoIGrtwK6UdBSyMs9YLbkMLNJ7/hV7dvlsd5sv8HHtihMc6nvhkkM1XJXy8FbdI7G0l44ZR8ftd3X9iRjJEwl7WSaOdmaHmp6Y2Mx/qyeyU8Rg/3bPYLWTw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Attribute = _t, Value = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Attribute", type text}, {"Value", type text}}),
        #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
        #"Integer-Divided Column" = Table.TransformColumns(#"Added Index", {{"Index", each Number.IntegerDivide(_, 3), Int64.Type}}),
        #"Pivoted Column" = Table.Pivot(#"Integer-Divided Column", List.Distinct(#"Integer-Divided Column"[Attribute]), "Attribute", "Value")
    in
        #"Pivoted Column"

    See steps: "Add Index" and "Integer-Divide Column".

3 Replies

  • ImkeF's avatar
    ImkeF
    Community Champion

    Hi Anonymous ,
    that is basically an unstacking exercise where you need an ID column for each row. You can create that as follows:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Zc0/C8IwEAXwryI3d/H/LLRohS46hgxHejQHzSUkWfz2VhGJcbx378dTCjoXVoKOoIGrtwK6UdBSyMs9YLbkMLNJ7/hV7dvlsd5sv8HHtihMc6nvhkkM1XJXy8FbdI7G0l44ZR8ftd3X9iRjJEwl7WSaOdmaHmp6Y2Mx/qyeyU8Rg/3bPYLWTw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Attribute = _t, Value = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Attribute", type text}, {"Value", type text}}),
        #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
        #"Integer-Divided Column" = Table.TransformColumns(#"Added Index", {{"Index", each Number.IntegerDivide(_, 3), Int64.Type}}),
        #"Pivoted Column" = Table.Pivot(#"Integer-Divided Column", List.Distinct(#"Integer-Divided Column"[Attribute]), "Attribute", "Value")
    in
        #"Pivoted Column"

    See steps: "Add Index" and "Integer-Divide Column".

  • wdx223_Daniel's avatar
    wdx223_Daniel
    Community Champion

    =Table.Combine(List.Transform(Table.Split(PreviousStepName,3),each Table.PromoteHeaders(Table.Transpose(_))))