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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
scott_86_
New Member

PQ question - change rows to columns

Hi,

Can anyone help me out with some applied steps for the below? I've tried a mixture of transpose, promote first row to header, unpivot, etc; however, so far am not having any luck!

In the sample data photo below, I am trying to achieve the following:

 

1727247521182.png

2 ACCEPTED SOLUTIONS
Vijay_A_Verma
Super User
Super User

After loading your table, you can insert this step where Source is your previous step

= Table.FromRecords(List.Transform(Table.Split(Source, 3), (x)=> Record.FromList(x[Column2], x[Column1])))

 

The sample code in action here

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCi4pTUnNK1HwS8xNVdJRclSK1YlWcs4vLSpOVQjJLMkBC0JEXRJLUhU8i4tLU1OAgoYG+oZG+kYGRiZgSTRznLCZ4+SExRwTfUNDnMY4YzPG2RmHc8DmmGIzxwWbOS4u+MwxVoqNBQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t]),
    Result = Table.FromRecords(List.Transform(Table.Split(Source, 3), (x)=> Record.FromList(x[Column2], x[Column1])))
in
    Result

 

If you are looking for a complete point and click solution, below is one which use Pivot feature

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCi4pTUnNK1HwS8xNVdJRclSK1YlWcs4vLSpOVQjJLMkBC0JEXRJLUhU8i4tLU1OAgoYG+oZG+kYGRiZgSTRznLCZ4+SExRwTfUNDnMY4YzPG2RmHc8DmmGIzxwWbOS4u+MwxVoqNBQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t]),
    #"Added Index" = Table.AddIndexColumn(Source, "Index", 1, 1, Int64.Type),
    #"Divided Column" = Table.TransformColumns(#"Added Index", {{"Index", each _ / 3, type number}}),
    #"Rounded Up" = Table.TransformColumns(#"Divided Column",{{"Index", Number.RoundUp, Int64.Type}}),
    #"Pivoted Column" = Table.Pivot(#"Rounded Up", List.Distinct(#"Rounded Up"[Column1]), "Column1", "Column2"),
    #"Removed Columns" = Table.RemoveColumns(#"Pivoted Column",{"Index"})
in
    #"Removed Columns"

 

View solution in original post

slorin
Super User
Super User

Hi @scott_86_ 

 

= #table(List.FirstN(Source[Column1],3), List.Split(Source[Column2],3))

Stéphane 

View solution in original post

6 REPLIES 6
Anonymous
Not applicable

Hi @scott_86_ ,

 

Pls has your problem been solved? If so, accept the reply as a solution. This will make it easier for the future people to find the answer quickly.

If not, please provide a more detailed description, preferably some virtual sample data, and the expected results.

 

Best Regards,

Stephen Tao

 

Omid_Motamedise
Super User
Super User

Interesting question.
Consider the next table as Source

 

Omid_Motamedise_0-1728635856110.png

 

 

 

the result of next formula would be alist includineg every three rows of Sourec table.
= Table.Split(Source,3)

 

Omid_Motamedise_1-1728635920298.png

 

 

while we neeed to transpose evert tables, so instead of the previous formula use the next formula which result in the next image.

 

= List.Transform(Table.Split(Source,3),each Table.PromoteHeaders(Table.Transpose(_)))

 

Omid_Motamedise_2-1728636015652.png

 

 

 

based on the above explanation, use the next formula instead of the previos formula which solve the problem as below.

 

= Table.Combine(List.Transform(Table.Split(Source,3),each Table.PromoteHeaders(Table.Transpose(_))))

 

Omid_Motamedise_3-1728636069876.png

 

 

 

 

 

 

If this answer helped resolve your issue, please consider marking it as the accepted answer. And if you found my response helpful, I'd appreciate it if you could give me kudos. Thank you!

If my answer helped solve your issue, please consider marking it as the accepted solution. It helps others in the community find answers faster—and keeps the community growing stronger!
You can also check out my YouTube channel for tutorials, tips, and real-world solutions in Power Query with the following link
https://youtube.com/@omidbi?si=96Bo-ZsSwOx0Z36h
AntrikshSharma
Super User
Super User

@scott_86_ 

let
    Source = RawData,
    GroupedRows = 
        Table.Group (
            Source,
            { "Column1" },
            {
                {
                    "Transformation",
                    each Table.TransformColumnTypes (
                        Table.PromoteHeaders (
                            Table.Transpose (
                                _,
                                type table [
                                    Student Name = text,
                                    Course Title = text,
                                    Date Issued = date
                                ]
                            )
                        ),
                        { { "Date Issued", type date } },
                        "en-GB"
                    ),
                    type table [ Student Name = text, Course Title = text, Date Issued = text ]
                }
            },
            GroupKind.Local,
            ( x, y ) => Number.From ( y[Column1] = "Student Name" )
        ),
    RemovedColumns = 
        Table.RemoveColumns ( GroupedRows, { "Column1" } ),
    ColumnNames = 
        Table.ColumnNames ( RemovedColumns[Transformation]{0} ),
    ExpandedCount = 
        Table.ExpandTableColumn (
            RemovedColumns,
            "Transformation",
            ColumnNames,
            ColumnNames
        )
in
    ExpandedCount
collinsg
Super User
Super User

Good day scott_86_

You already have solutions. I'll try to explain what is happening intuitively in the pivot.

 

In laying out a pivot table you consider,

  1. What is to go in rows?
  2. What is to go in columns?
  3. What is to go in values?

In your case these would be

  1. Rows: students
  2. Columns: attributes of the students ("Course Title" and "Date Issued")
  3. Values: the values of the attributes

Intuitively the problem is that three things need specified but you are starting with only two columns. "Column 1" entangles what you want in rows (students as represented by "Student Name") and the attributes of the students.

 

The pivot solution is to disentangle "Column 1".

  1. Add a column just for "Student Name" (steps "Added Custom" and "Filled Down" in code sample).
    collinsg_0-1728632218631.png
  2. Disentangle "Student Name" from "Column 1" by filtering to remove rows containing "Student Name" in "Column 1".
    collinsg_1-1728632235656.png
    The Table.Pivot function [Table.Pivot(table as table, pivotValues as list, attributeColumn as text, valueColumn as text, optional aggregationFunction as nullable function) as table] can now be fed with the parameters it wants from your column names, pivotValues is the Student Name column (or to be precise, the distinct values in that column),  attributeColumn is "Column 1" and valueColumn is "Column 2".
  3. Pivot
    collinsg_2-1728632255174.png

     

Here is sample code. It will be flexible as regards the number of students as there is no hard-wiring of the number of students.

Here is sample code:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCi4pTUnNK1HwS8xNVdJRCk7OLylRitWJVnLOLy0qTlUIySzJAUkEuDmChV0SS1IVPIuLS1NTgKIGhvpAZGRgZKhgYGAFRmBVaKa6JJZlppBhqhFeU30zs1PJMNQYydBYAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Column 1" = _t, #"Column 2" = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Column 1", type text}, {"Column 2", type text}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "Student Name", each if [Column 1] = "Student Name" then [Column 2] else null),
#"Filled Down" = Table.FillDown(#"Added Custom",{"Student Name"}),
#"Filtered Rows" = Table.SelectRows(#"Filled Down", each ([Column 1] <> "Student Name")),
#"Pivoted Column" = Table.Pivot(#"Filtered Rows", List.Distinct(#"Filtered Rows"[Column 1]), "Column 1", "Column 2"),
#"Changed Type1" = Table.TransformColumnTypes(#"Pivoted Column",{{"Student Name", type text}, {"Course Title", type text}, {"Date Issued", type datetime}}),
#"Changed Type2" = Table.TransformColumnTypes(#"Changed Type1",{{"Date Issued", type date}})
in
#"Changed Type2"

Hope this helps

slorin
Super User
Super User

Hi @scott_86_ 

 

= #table(List.FirstN(Source[Column1],3), List.Split(Source[Column2],3))

Stéphane 

Vijay_A_Verma
Super User
Super User

After loading your table, you can insert this step where Source is your previous step

= Table.FromRecords(List.Transform(Table.Split(Source, 3), (x)=> Record.FromList(x[Column2], x[Column1])))

 

The sample code in action here

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCi4pTUnNK1HwS8xNVdJRclSK1YlWcs4vLSpOVQjJLMkBC0JEXRJLUhU8i4tLU1OAgoYG+oZG+kYGRiZgSTRznLCZ4+SExRwTfUNDnMY4YzPG2RmHc8DmmGIzxwWbOS4u+MwxVoqNBQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t]),
    Result = Table.FromRecords(List.Transform(Table.Split(Source, 3), (x)=> Record.FromList(x[Column2], x[Column1])))
in
    Result

 

If you are looking for a complete point and click solution, below is one which use Pivot feature

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCi4pTUnNK1HwS8xNVdJRclSK1YlWcs4vLSpOVQjJLMkBC0JEXRJLUhU8i4tLU1OAgoYG+oZG+kYGRiZgSTRznLCZ4+SExRwTfUNDnMY4YzPG2RmHc8DmmGIzxwWbOS4u+MwxVoqNBQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t]),
    #"Added Index" = Table.AddIndexColumn(Source, "Index", 1, 1, Int64.Type),
    #"Divided Column" = Table.TransformColumns(#"Added Index", {{"Index", each _ / 3, type number}}),
    #"Rounded Up" = Table.TransformColumns(#"Divided Column",{{"Index", Number.RoundUp, Int64.Type}}),
    #"Pivoted Column" = Table.Pivot(#"Rounded Up", List.Distinct(#"Rounded Up"[Column1]), "Column1", "Column2"),
    #"Removed Columns" = Table.RemoveColumns(#"Pivoted Column",{"Index"})
in
    #"Removed Columns"

 

Helpful resources

Announcements
July PBI25 Carousel

Power BI Monthly Update - July 2025

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

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 community update carousel

Fabric Community Update - June 2025

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