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
ofir_n1
Frequent Visitor

unpivot column to N number of columns based on IDs

hi

i have a column that contain transaction ID , and another attribute column that contain user type,

the transaction id reapeat multiple times according to the user type

i wish to create the following table from the data 

 

ofir_n1_0-1620888949790.png

to:

transaction idtarget1target2target3
    
1 ACCEPTED SOLUTION
v-yingjl
Community Support
Community Support

Hi @ofir_n1 ,

You can try this query:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WKijOMiorT8svNi3MNCkyMUg0qTBX0lFyLCjIzCsuScxLTlWK1YlWKs3LzssvzwPKhBanFrkX5ZcWgMUj3YoKTc3LksvdPNINkwIsPX0y3MzSHR2dAk1IVItuI9xhVaV++UZVRh5wh4FMxaeEkDwum4rMMiwKMivDUnDbhKaEkDyKTbEA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Transaction ID" = _t, Targets = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Transaction ID", type text}, {"Targets", type text}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Transaction ID"}, {{"Data", each _, type table [Transaction ID=nullable text, Targets=nullable text]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.Column([Data],"Targets")),
    #"Extracted Values" = Table.TransformColumns(#"Added Custom", {"Custom", each Text.Combine(List.Transform(_, Text.From), ","), type text}),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Extracted Values", "Custom", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), {"Target 1", "Target 2", "Target 3"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Target 1", type text}, {"Target 2", type text}, {"Target 3", type text}}),
    #"Removed Columns" = Table.RemoveColumns(#"Changed Type1",{"Data"})
in
    #"Removed Columns"

re.png

 

Best Regards,
Community Support Team _ Yingjie Li
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

3 REPLIES 3
mah_priya94
Helper I
Helper I

Hi @ofir_n1 ,
Please try the following code.
Won't require you to manually set the Target number.


let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WKijOMiorT8svNi3MNCkyMUg0qTBX0lFyLCjIzCsuScxLTlWK1YlWKs3LzssvzwPKhBanFrkX5ZcWgMUj3YoKTc3LksvdPNINkwIsPX0y3MzSHR2dAk1IVItuI9xhVaV++UZVRh5wh4FMxaeEkDwum4rMMiwKMivDUnDbhKaEkDyKTbEA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Transaction ID" = _t, Targets = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Transaction ID", type text}, {"Targets", type text}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"Transaction ID"}, {{"Data", each _, type table [Transaction ID=nullable text, Targets=nullable text]}}),
#"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.AddIndexColumn([Data], "ID", 1)),
#"Expanded Custom" = Table.ExpandTableColumn(#"Added Custom", "Custom", {"Targets", "ID"}, {"Targets", "ID"}),
#"Changed Type1" = Table.TransformColumnTypes(#"Expanded Custom",{{"ID", type text}}),
#"Added Custom1" = Table.AddColumn(#"Changed Type1", "Custom", each "Target "&[ID]),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom1",{"Data", "ID"}),
#"Pivoted Column" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[Custom]), "Custom", "Targets")
in
#"Pivoted Column"

v-yingjl
Community Support
Community Support

Hi @ofir_n1 ,

You can try this query:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WKijOMiorT8svNi3MNCkyMUg0qTBX0lFyLCjIzCsuScxLTlWK1YlWKs3LzssvzwPKhBanFrkX5ZcWgMUj3YoKTc3LksvdPNINkwIsPX0y3MzSHR2dAk1IVItuI9xhVaV++UZVRh5wh4FMxaeEkDwum4rMMiwKMivDUnDbhKaEkDyKTbEA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Transaction ID" = _t, Targets = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Transaction ID", type text}, {"Targets", type text}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Transaction ID"}, {{"Data", each _, type table [Transaction ID=nullable text, Targets=nullable text]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.Column([Data],"Targets")),
    #"Extracted Values" = Table.TransformColumns(#"Added Custom", {"Custom", each Text.Combine(List.Transform(_, Text.From), ","), type text}),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Extracted Values", "Custom", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), {"Target 1", "Target 2", "Target 3"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Target 1", type text}, {"Target 2", type text}, {"Target 3", type text}}),
    #"Removed Columns" = Table.RemoveColumns(#"Changed Type1",{"Data"})
in
    #"Removed Columns"

re.png

 

Best Regards,
Community Support Team _ Yingjie Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

AnkitKukreja
Super User
Super User

Hi @ofir_n1 

 

Please have a look at the link below.

Pivot columns (Power Query) - Excel (microsoft.com)

 

Please mark it as a solution, if it answers your question.

 

Thanks and Regards,
Ankit
www.linkedin.com/in/ankit-kukreja1904

 

If my response has successfully addressed your question or concern, I kindly request that you mark this post as resolved. Additionally, if you found my assistance helpful, a thumbs-up would be greatly appreciated.
Thanks,
Ankit Kukreja
www.linkedin.com/in/ankit-kukreja1904

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.

Top Solution Authors