cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
daisyhu
Frequent Visitor

Combine rows with duplicate identifiers while adding additional columns

Before:

 

NameSpeciesActivityStart DateEnd Date
     
PusheenFelineSnacking10/1/201910/10/2019
PusheenFelineNapping10/2/201910/12/2019
PuppyDoggiePlaying10/3/201910/13/2019

 

After:

 

NameSpeciesActivity 1Start Date 1End Date 1Activity 2Start Date 2End Date 2
        
PusheenFelineSnacking10/1/201910/10/2019Napping10/2/201910/12/2019
PuppyDoggiePlaying10/3/201910/13/2019NANANA

 

How do I do this in Power BI Desktop? Thanks!

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Community Support
Community Support

Re: Combine rows with duplicate identifiers while adding additional columns

Hi daisyhu, 

You could try to use below M code to see whether it work or not.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8kvMTVXSUQouSE3OTC0GshyTSzLLMksqQYIliUUlCi6JJSAVrnkpEGasTrRSQGlxRmpqHlDYLTUnMw9sQl5icnZmXjqQaWigb6hvZGBoCWUbQDg4NPolFhTA9Rkh6zNC1ldQAHKSS356eiZIV0BOYiVclzGyLignNhYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [#"(blank)" = _t, #"(blank).1" = _t, #"(blank).2" = _t, #"(blank).3" = _t, #"(blank).4" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"(blank)", type text}, {"(blank).1", type text}, {"(blank).2", type text}, {"(blank).3", type text}, {"(blank).4", type text}}),
    #"Promoted Headers" = Table.PromoteHeaders(#"Changed Type", [PromoteAllScalars=true]),
    #"Merged Columns" = Table.CombineColumns(#"Promoted Headers",{"Activity", "Start Date", "End Date"},Combiner.CombineTextByDelimiter(",", QuoteStyle.None),"Merged"),
    #"Added Index" = Table.AddIndexColumn(#"Merged Columns", "Index", 0, 1),
    #"Grouped Rows1" = Table.Group(#"Added Index", {"Name", "Species"}, {{"ALL", each _, type table [Name=text, Species=text, Merged=text, Index=number]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows1", "Custom", each Table.AddIndexColumn([ALL], "GID", 1,1)),
    #"Removed Columns1" = Table.RemoveColumns(#"Added Custom",{"ALL"}),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Removed Columns1", "Custom", {"Merged", "GID"}, {"Custom.Merged", "Custom.GID"}),
    #"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(#"Expanded Custom", {{"Custom.GID", type text}}, "en-US"), List.Distinct(Table.TransformColumnTypes(#"Expanded Custom", {{"Custom.GID", type text}}, "en-US")[Custom.GID]), "Custom.GID", "Custom.Merged"),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Pivoted Column", "1", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), {"1.1", "1.2", "1.3"}),
    #"Split Column by Delimiter1" = Table.SplitColumn(#"Split Column by Delimiter", "2", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), {"2.1", "2.2", "2.3"})
in
    #"Split Column by Delimiter1"

 Then rename column name to you want.

Best Regards,
Zoe Zhi

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

 

View solution in original post

1 REPLY 1
Community Support
Community Support

Re: Combine rows with duplicate identifiers while adding additional columns

Hi daisyhu, 

You could try to use below M code to see whether it work or not.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8kvMTVXSUQouSE3OTC0GshyTSzLLMksqQYIliUUlCi6JJSAVrnkpEGasTrRSQGlxRmpqHlDYLTUnMw9sQl5icnZmXjqQaWigb6hvZGBoCWUbQDg4NPolFhTA9Rkh6zNC1ldQAHKSS356eiZIV0BOYiVclzGyLignNhYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [#"(blank)" = _t, #"(blank).1" = _t, #"(blank).2" = _t, #"(blank).3" = _t, #"(blank).4" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"(blank)", type text}, {"(blank).1", type text}, {"(blank).2", type text}, {"(blank).3", type text}, {"(blank).4", type text}}),
    #"Promoted Headers" = Table.PromoteHeaders(#"Changed Type", [PromoteAllScalars=true]),
    #"Merged Columns" = Table.CombineColumns(#"Promoted Headers",{"Activity", "Start Date", "End Date"},Combiner.CombineTextByDelimiter(",", QuoteStyle.None),"Merged"),
    #"Added Index" = Table.AddIndexColumn(#"Merged Columns", "Index", 0, 1),
    #"Grouped Rows1" = Table.Group(#"Added Index", {"Name", "Species"}, {{"ALL", each _, type table [Name=text, Species=text, Merged=text, Index=number]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows1", "Custom", each Table.AddIndexColumn([ALL], "GID", 1,1)),
    #"Removed Columns1" = Table.RemoveColumns(#"Added Custom",{"ALL"}),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Removed Columns1", "Custom", {"Merged", "GID"}, {"Custom.Merged", "Custom.GID"}),
    #"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(#"Expanded Custom", {{"Custom.GID", type text}}, "en-US"), List.Distinct(Table.TransformColumnTypes(#"Expanded Custom", {{"Custom.GID", type text}}, "en-US")[Custom.GID]), "Custom.GID", "Custom.Merged"),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Pivoted Column", "1", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), {"1.1", "1.2", "1.3"}),
    #"Split Column by Delimiter1" = Table.SplitColumn(#"Split Column by Delimiter", "2", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), {"2.1", "2.2", "2.3"})
in
    #"Split Column by Delimiter1"

 Then rename column name to you want.

Best Regards,
Zoe Zhi

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

 

View solution in original post

Helpful resources

Announcements
New Ranks Launched March 24th!

New Ranks Launched March 24th!

The time has come: We are finally able to share more details on the brand-new ranks coming to the Power BI Community!

‘Better Together’ Contest Finalists Announced!

‘Better Together’ Contest Finalists Announced!

Congrats to the finalists of our ‘Better Together’-themed T-shirt design contest! Click for the top entries.

Arun 'Triple A' Event Video, Q&A, and Slides

Arun 'Triple A' Event Video, Q&A, and Slides

Missed the Arun 'Triple A' event or want to revisit it? We've got you covered! Check out the video, Q&A, and slides now.

Join THE global Microsoft Power Platform event series.

Join THE global Power Platform event series.

Attend for two days of expert-led learning and innovation on topics like AI and Analytics, powered by Dynamic Communities.

Community Summit North America

Community Summit North America

Innovate, Collaborate, Grow. The top training and networking event across the globe for Microsoft Business Applications

Top Solution Authors
Top Kudoed Authors