Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

Pivot Columns With Different Attributes

Hi all,

I am not sure if this is the right place to put this, but I am currently in the process of using Power Query to try and pivot several columns.

 

Currently my data format looks like the following:

 

NameJobsRole IDTotal Hours Worked
Sophie ThomsonCook120
Sophie ThomsonSinger210
Joe BloggsArtist323
Joe BloggsFood Critic434
Joe BloggsTV Star556
Alix MaxActor645
Max SampleChef723
Alix MaxArtist312
Max SampleFood Critic412

 

I am hoping to have an output (that I can preferably export to Excel; as the specification is that it is delivered in Excel Format) that looks a bit like this:

 

 

NameJob 1Job 1 HoursJob 2Job 2 HoursJob 3Job 3 Hours
Sophie ThomsonCook20Singer10  
Joe BloggsArtist23Food Critic34TV Star56
Alix MaxActor45Actor12  
Max SampleChef23Food Critic12  

 

I have over 150 different types of jobs in the data format above, and have tried to create merge queries and then pivot them, however, I get lots of names which end up blank.

 

What would be the best way to approach this so I can achieve the format in the second table?

 

Thanks in advance.

  • Hi Anonymous  ,

    According to your description, you could merge and combine  columns in powerbi query by the following steps:

    Step 1:Merge two columns with ,” (“Jobs” and “Total Hours Worked” column)

    Step2: Remove “Role ID” Column

    Step3: Write expression

    = Table.Group( #"Removed Columns", {"Name"}, {{"Job", each Text.Combine([Merged],","), type  text}})

    Step4: Split Column with “,”

    Step5: Rename Columns

    The final output is shown below:

    In addition: attach m language

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bY+7DsMgDEV/BTFnaJ6d00gdKnUi6hJlQCkFVBJHwJDPr50OjUQHy4Z7bF8PAxewGqtYb2AOsPCMdwBvTDlGceJj9gcRdtHKE0DgF7qBYhcHWgf8a320IWJRElSmwBXgyTpvo53wVRFZpVT/YCJKWlRTNDvROruxu9xozRSB1IZm1LuKAhNyXp2iU4x6YTr/TBybjx7zIu1OPRI1fgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Name = _t, Jobs = _t, #"Role ID" = _t, #"Total Hours Worked" = _t]),
        #"Merged Columns" = Table.CombineColumns(Table.TransformColumnTypes(Source, {{"Total Hours Worked", type text}}, "en-001"),{"Jobs", "Total Hours Worked"},Combiner.CombineTextByDelimiter(",", QuoteStyle.None),"Merged"),
        #"Removed Columns" = Table.RemoveColumns(#"Merged Columns",{"Role ID"}),
        Custom1 = Table.Group( #"Removed Columns", {"Name"}, {{"Job", each Text.Combine([Merged],","), type  text}}),
        #"Split Column by Delimiter" = Table.SplitColumn(Custom1, "Job", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), {"Job.1", "Job.2", "Job.3", "Job.4", "Job.5", "Job.6"}),
        #"Changed Type" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Name", type text}, {"Job.1", type text}, {"Job.2", Int64.Type}, {"Job.3", type text}, {"Job.4", Int64.Type}, {"Job.5", type text}, {"Job.6", Int64.Type}}),
        #"Renamed Columns" = Table.RenameColumns(#"Changed Type",{{"Job.2", "Job 1 Hours"}, {"Job.3", "Job 2"}, {"Job.4", "Job 2 Hours"}, {"Job.5", "Job 3"}, {"Job.1", "Job 1"}, {"Job.6", "Job 3 Hours"}})
    in
        #"Renamed Columns"

    Best Regards,
    Yalan Wu
    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

2 Replies

  • v-yalanwu-msft's avatar
    v-yalanwu-msft
    Icon for Community Support rankCommunity Support

    Hi Anonymous  ,

    According to your description, you could merge and combine  columns in powerbi query by the following steps:

    Step 1:Merge two columns with ,” (“Jobs” and “Total Hours Worked” column)

    Step2: Remove “Role ID” Column

    Step3: Write expression

    = Table.Group( #"Removed Columns", {"Name"}, {{"Job", each Text.Combine([Merged],","), type  text}})

    Step4: Split Column with “,”

    Step5: Rename Columns

    The final output is shown below:

    In addition: attach m language

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bY+7DsMgDEV/BTFnaJ6d00gdKnUi6hJlQCkFVBJHwJDPr50OjUQHy4Z7bF8PAxewGqtYb2AOsPCMdwBvTDlGceJj9gcRdtHKE0DgF7qBYhcHWgf8a320IWJRElSmwBXgyTpvo53wVRFZpVT/YCJKWlRTNDvROruxu9xozRSB1IZm1LuKAhNyXp2iU4x6YTr/TBybjx7zIu1OPRI1fgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Name = _t, Jobs = _t, #"Role ID" = _t, #"Total Hours Worked" = _t]),
        #"Merged Columns" = Table.CombineColumns(Table.TransformColumnTypes(Source, {{"Total Hours Worked", type text}}, "en-001"),{"Jobs", "Total Hours Worked"},Combiner.CombineTextByDelimiter(",", QuoteStyle.None),"Merged"),
        #"Removed Columns" = Table.RemoveColumns(#"Merged Columns",{"Role ID"}),
        Custom1 = Table.Group( #"Removed Columns", {"Name"}, {{"Job", each Text.Combine([Merged],","), type  text}}),
        #"Split Column by Delimiter" = Table.SplitColumn(Custom1, "Job", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), {"Job.1", "Job.2", "Job.3", "Job.4", "Job.5", "Job.6"}),
        #"Changed Type" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Name", type text}, {"Job.1", type text}, {"Job.2", Int64.Type}, {"Job.3", type text}, {"Job.4", Int64.Type}, {"Job.5", type text}, {"Job.6", Int64.Type}}),
        #"Renamed Columns" = Table.RenameColumns(#"Changed Type",{{"Job.2", "Job 1 Hours"}, {"Job.3", "Job 2"}, {"Job.4", "Job 2 Hours"}, {"Job.5", "Job 3"}, {"Job.1", "Job 1"}, {"Job.6", "Job 3 Hours"}})
    in
        #"Renamed Columns"

    Best Regards,
    Yalan Wu
    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.