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

Next up in the FabCon + SQLCon recap series: The roadmap for Microsoft SQL and Maximizing Developer experiences in Fabric. All sessions are available on-demand after the live show. Register now

Reply
unicorn19
New Member

How to convert row value to column

Hi,

Below is my data and i would like to have a single row for ID and company by pivoting other columns i.e if Business is finance, then its respecitve role,region and name. 

ID                Name Business   Role Region Company
100 JohnFinanceManagerWestWalmart
100ElliotSalesAnalystEastWalmart
100DavidDavidManagerWestWalmart


Expected output

IDCompanyFinance_roleFinance_RegionFinance_NameSales_roleSales_RegionSales_NamePhoto_rolePhoto_RegionPhoto_Name
100WalmartManagerWest JohnAnalystEastElliotManagerWestDavid

 

How can i acheive this in power bi? 

1 ACCEPTED SOLUTION
lbendlin
Super User
Super User

Why would you ever want to do this? Goes against what Power Bi is about.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("fY09CsJAEIWvErZOEY+gJoKCIlpYLCkeOiQLkxnJrkJu705stNBpvsf7Ybx327r4Ple64oCBTKweMQjF+HZPyjSTuqBiaq3DHTK5tvRuUVVm7bS3aBMEcrX6HoKOxqwuFJMBPGBMH6OGOahFZzDFzKWAp7nc4MemxjPcMo+9Jv37pn0B", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t, Column4 = _t, Column5 = _t, Column6 = _t]),
    #"Trimmed Text" = Table.TransformColumns(Source,{{"Column1", Text.Trim, type text}, {"Column2", Text.Trim, type text}, {"Column3", Text.Trim, type text}, {"Column4", Text.Trim, type text}, {"Column5", Text.Trim, type text}, {"Column6", Text.Trim, type text}}),
    #"Promoted Headers" = Table.PromoteHeaders(#"Trimmed Text", [PromoteAllScalars=true]),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Promoted Headers", {"ID", "Company", "Business"}, "Attribute", "Value"),
    #"Added Custom" = Table.AddColumn(#"Unpivoted Other Columns", "Column", each [Business] & "_" & [Attribute]),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Business", "Attribute"}),
    #"Pivoted Column" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[Column]), "Column", "Value")
in
    #"Pivoted Column"

How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done". Once you examined the code, replace the entire Source step with your own source.

View solution in original post

2 REPLIES 2
lbendlin
Super User
Super User

Why would you ever want to do this? Goes against what Power Bi is about.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("fY09CsJAEIWvErZOEY+gJoKCIlpYLCkeOiQLkxnJrkJu705stNBpvsf7Ybx327r4Ple64oCBTKweMQjF+HZPyjSTuqBiaq3DHTK5tvRuUVVm7bS3aBMEcrX6HoKOxqwuFJMBPGBMH6OGOahFZzDFzKWAp7nc4MemxjPcMo+9Jv37pn0B", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t, Column4 = _t, Column5 = _t, Column6 = _t]),
    #"Trimmed Text" = Table.TransformColumns(Source,{{"Column1", Text.Trim, type text}, {"Column2", Text.Trim, type text}, {"Column3", Text.Trim, type text}, {"Column4", Text.Trim, type text}, {"Column5", Text.Trim, type text}, {"Column6", Text.Trim, type text}}),
    #"Promoted Headers" = Table.PromoteHeaders(#"Trimmed Text", [PromoteAllScalars=true]),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Promoted Headers", {"ID", "Company", "Business"}, "Attribute", "Value"),
    #"Added Custom" = Table.AddColumn(#"Unpivoted Other Columns", "Column", each [Business] & "_" & [Attribute]),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Business", "Attribute"}),
    #"Pivoted Column" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[Column]), "Column", "Value")
in
    #"Pivoted Column"

How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done". Once you examined the code, replace the entire Source step with your own source.

i know but business wants this and asking to do in SQL which ends up creating lot of columns.. I wanted to know if we can do in power bi , so that we can pick only the columns that is asked.

Helpful resources

Announcements
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.