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

The Power BI DataViz World Championships are on! With four chances to enter, you could win a spot in the LIVE Grand Finale in Las Vegas. Show off your skills.

Reply
howeixiong
Regular Visitor

Unpivot Rows to Column

Dear All

 

I would like to know if the following is possible?

I would have a large set of data and the number of columns depends on the no. of rows the data appears.

Some can be 1, 2, 3, 4 or 5 columns.

 

Thank you in advance!

 

Data 
Unique ReferenceColumn 2
2404124231223097TMPN
24041242312230973770808102FS
24041242312230809135793102FS
2404124231223085REPAYMENT 
2404124231223085/BOOK
24041242312230853254675102FS
2404124231223084USD10
2404124231223084REPAYMENT 
2404124231223084039190
24041242312230841447267102FS
2404124231223094BORROWING
2404124231223094//BASE
24041242312230945971952102FS
2404124231223094/ACC
240412423122309400005ILC240004

 

Result     
Unique ReferenceColumn 1Column 2Column 3Column 4Column 5
2404124231223097TMPN3770808102FS   
24041242312230809135793102FS    
2404124231223085REPAYMENT /BOOK3254675102FS  
2404124231223084USD10REPAYMENT 0391901447267102FS 
2404124231223094BORROWING//BASE5971952102FS/ACC00005ILC240004
1 ACCEPTED SOLUTION
ronrsnfld
Super User
Super User

  • Group by Reference
  • Create a Record from each subgroup, generating the column names dynamically
  • Create a unique, sorted list of all the column names
    • If there are more than 9, may have to do a custom sort
let
    Source = Excel.CurrentWorkbook(){[Name="Table3"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Unique Reference", type text}, {"Column 2", type text}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Unique Reference"}, {
        {"Records", each Record.FromList([Column 2], List.Transform(List.Numbers(1, List.Count([Column 2])), each "Column " & Text.From(_))) }}),
    #"Col Names" = List.Sort(List.Distinct(List.Accumulate(
                        #"Grouped Rows"[Records],
                        {},
                        (s,c)=>s & Record.FieldNames(c)
                        ))),
    #"Expand Records" = Table.ExpandRecordColumn(#"Grouped Rows","Records", #"Col Names")

in
    #"Expand Records"

 

Result from your data above:

ronrsnfld_0-1713151690758.png

 

 

View solution in original post

4 REPLIES 4
dufoq3
Super User
Super User

Hi @howeixiong, different one here.

 

Result

dufoq3_0-1713548171608.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("fc7BCoMwDMbxVxk9C03S1JijOjdk04o6xhDf/zVW78Ze/7/wddscMTASU0CiACqucOswjW4vTlsQgQoqBHosJ6aCbBRDFA2midnM3VT/hm5cb5bwTUovKwaKXEo0Jzibz3JHsOL1/iEgKKp5j8xCpVgf0MM0aZ7Ttx+fFvC+qZfOqlEFNdLlhK/b1mqQX+zfbU4A7Pb9Dw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Unique Reference" = _t, #"Column 2" = _t]),
    GroupedRows = Table.Group(Source, {"Unique Reference"}, {{"All", each Table.FromRows(Table.ToColumns(Table.SelectColumns(_, {"Column 2"}))), type table}}),
    ExpandedAll = Table.ExpandTableColumn(GroupedRows, "All", List.Transform({"1"..Text.From(Table.RowCount(GroupedRows))}, each "Column" & _))
in
    ExpandedAll

Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

AlienSx
Super User
Super User

let
    Source = your_table,
    to_list = Table.ToList(
        Table.Group(Source, {"Unique Reference"}, {{"x", each [Column 2]}}),
        (x) => {x{0}} & x{1}
    ),
    cols = List.Max(List.Transform(to_list, List.Count)),
    to_table = Table.FromList(to_list, (x) => x, cols)
in
    to_table
ronrsnfld
Super User
Super User

  • Group by Reference
  • Create a Record from each subgroup, generating the column names dynamically
  • Create a unique, sorted list of all the column names
    • If there are more than 9, may have to do a custom sort
let
    Source = Excel.CurrentWorkbook(){[Name="Table3"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Unique Reference", type text}, {"Column 2", type text}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Unique Reference"}, {
        {"Records", each Record.FromList([Column 2], List.Transform(List.Numbers(1, List.Count([Column 2])), each "Column " & Text.From(_))) }}),
    #"Col Names" = List.Sort(List.Distinct(List.Accumulate(
                        #"Grouped Rows"[Records],
                        {},
                        (s,c)=>s & Record.FieldNames(c)
                        ))),
    #"Expand Records" = Table.ExpandRecordColumn(#"Grouped Rows","Records", #"Col Names")

in
    #"Expand Records"

 

Result from your data above:

ronrsnfld_0-1713151690758.png

 

 

wdx223_Daniel
Super User
Super User

=Table.FromColumns(List.Zip(Table.Group(YourTableName,"Unique Reference",{"n",each {[Unique Reference]{0}}&[Column 2]})[n]))

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

FebPBI_Carousel

Power BI Monthly Update - February 2025

Check out the February 2025 Power BI update to learn about new features.

Feb2025 NL Carousel

Fabric Community Update - February 2025

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

Top Solution Authors