Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
baldhillbilly
New Member

Pivot/Unpivot after delimiter but keep all columns (even those with no data/values)

Hello all and thanks in advance.   Subject line is a good summary of my problem.    

 

1)  Have data such as:  

 

Johna|b|d
Jimb|e
Janea|b|e
Josef
Jilld|f
Joannea|b|e|f

 

2)  Use delimiter to yield:  

 

 abcdef
Johnabd   
Jimbe    
Janeabe   
Josef     
Jilldf    
Joanneabef  

 

3)  Desire to get data in matching column.  I've been able to use some pivot/unpivot options (based on this forum) to get to this:   

 abdef 
Johnabd   
Jim b e  
Janeab e  
Jose    f 
Jill  d f 
Joanneab ef 

 

4)  The problem:  Note that column "C" is missing.  I would like to keep it and I'm open to other options as well ..... i.e. reorganizing the data after step 2 to get the data in the matching column header.  

 

Thanks

Josh

1 ACCEPTED SOLUTION
mahoneypat
Employee
Employee

Here is one way to do it in the query editor.  I would suggest you unpivot your columns, but this gives your desired output I believe.  To see how it works, just create a blank query, go to Advanced Editor, and replace the text there with the M code below.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8srPyFPSUUqsSapJUYrVAQpk5gL5STWpEF5iXipUGiqQXwwSSIOqzckBclJqoNz8xDyEcpBgLAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Name = _t, Letters = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Name", type text}, {"Letters", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Lists", each Text.Split([Letters], "|")),
    Custom1 = #"Added Custom",
    #"Removed Other Columns" = Table.SelectColumns(Custom1,{"Name", "Lists"}),
    #"Expanded Lists" = Table.ExpandListColumn(#"Removed Other Columns", "Lists"),
    Custom2 = {"a", "b", "c", "d", "e", "f"},
    #"Converted to Table" = Table.FromList(Custom2, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Merged Queries" = Table.NestedJoin(#"Converted to Table", {"Column1"}, #"Expanded Lists", {"Lists"}, "Converted to Table", JoinKind.LeftOuter),
    #"Expanded Converted to Table" = Table.ExpandTableColumn(#"Merged Queries", "Converted to Table", {"Name", "Lists"}, {"Name", "Lists"}),
    #"Pivoted Column" = Table.Pivot(#"Expanded Converted to Table", List.Distinct(#"Expanded Converted to Table"[Column1]), "Column1", "Lists"),
    #"Filtered Rows" = Table.SelectRows(#"Pivoted Column", each ([Name] <> null)),
    #"Removed Other Columns1" = Table.SelectColumns(#"Filtered Rows",{"Name", "a", "b", "c", "d", "e", "f"})
in
    #"Removed Other Columns1"

 

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


View solution in original post

1 REPLY 1
mahoneypat
Employee
Employee

Here is one way to do it in the query editor.  I would suggest you unpivot your columns, but this gives your desired output I believe.  To see how it works, just create a blank query, go to Advanced Editor, and replace the text there with the M code below.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8srPyFPSUUqsSapJUYrVAQpk5gL5STWpEF5iXipUGiqQXwwSSIOqzckBclJqoNz8xDyEcpBgLAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Name = _t, Letters = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Name", type text}, {"Letters", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Lists", each Text.Split([Letters], "|")),
    Custom1 = #"Added Custom",
    #"Removed Other Columns" = Table.SelectColumns(Custom1,{"Name", "Lists"}),
    #"Expanded Lists" = Table.ExpandListColumn(#"Removed Other Columns", "Lists"),
    Custom2 = {"a", "b", "c", "d", "e", "f"},
    #"Converted to Table" = Table.FromList(Custom2, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Merged Queries" = Table.NestedJoin(#"Converted to Table", {"Column1"}, #"Expanded Lists", {"Lists"}, "Converted to Table", JoinKind.LeftOuter),
    #"Expanded Converted to Table" = Table.ExpandTableColumn(#"Merged Queries", "Converted to Table", {"Name", "Lists"}, {"Name", "Lists"}),
    #"Pivoted Column" = Table.Pivot(#"Expanded Converted to Table", List.Distinct(#"Expanded Converted to Table"[Column1]), "Column1", "Lists"),
    #"Filtered Rows" = Table.SelectRows(#"Pivoted Column", each ([Name] <> null)),
    #"Removed Other Columns1" = Table.SelectColumns(#"Filtered Rows",{"Name", "a", "b", "c", "d", "e", "f"})
in
    #"Removed Other Columns1"

 

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.