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

Get inspired! Check out the entries from the Power BI DataViz World Championships preliminary rounds and give kudos to your favorites. View the vizzies.

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
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 Kudoed Authors