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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Anonymous
Not applicable

Transform multiple columns into a single row

I have data which is categorized as attributes of New and Used vehicles ie.(New SRPIS, Used SRPIS, New VDPS Used VDPS); by date and client.  I need to perserve the date and client but transform the attribute columns so I have two rows (New and Used) with no repeating attribute columns.  Row = New, columns = SRPIS, VDPS

Row = Used, columns = SRPIS, VDPS

In the visialization I need to show as a total and allow for slicing by New and Used.

Current:

MODIFIED_DATECUSTOMER_IDNEW_SRPISNEW_VDPSNEW_EMAILSUSED_SRPISUSED_VDPSUSED_EMAILS
6/2/2020 0:00100000120352883066371050
5/2/2020 0:00100000120303763051841180
4/2/2020 0:0010000012021933102854531
3/2/2020 0:0010000012016893013450570
2/2/2020 0:0010000012024537414067981
1/2/2020 0:0010000012022053513261600

 

Needed

MODIFIED_DATECUSTOMER_IDNew/UsedSRPISVDPSEMAILS
6/2/2020 0:00100000120New3528830
6/2/2020 0:00100000120Used66371050
4/2/2020 0:00100000120New2193310
4/2/2020 0:00100000120Used51841181
2/2/2020 0:00100000120New2453741
2/2/2020 0:00100000120Used2854530
1 ACCEPTED SOLUTION
Ashish_Mathur
Super User
Super User

Hi,

This M code works

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("fZBLDoUgDEW38sJYk9sv9W3FuP9tSKuGGQxoIb2HE86zwXfwzmD8gD/QtkbIRZy9GMcoIWPLs7v0GrG6uLZBsDUBlfCPYBSaIxSToEsC05FhoZfAYUmwvKQHIEsAeRxlUoHRqJVJnwa8NtB6rOsLUHhmj5gGtAZwfZjYZ8CexfEYXDc=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [MODIFIED_DATE = _t, CUSTOMER_ID = _t, NEW_SRPIS = _t, NEW_VDPS = _t, NEW_EMAILS = _t, USED_SRPIS = _t, USED_VDPS = _t, USED_EMAILS = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"MODIFIED_DATE", type datetime}, {"CUSTOMER_ID", Int64.Type}, {"NEW_SRPIS", Int64.Type}, {"NEW_VDPS", Int64.Type}, {"NEW_EMAILS", Int64.Type}, {"USED_SRPIS", Int64.Type}, {"USED_VDPS", Int64.Type}, {"USED_EMAILS", Int64.Type}}),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"MODIFIED_DATE", "CUSTOMER_ID"}, "Attribute", "Value"),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Unpivoted Other Columns", "Attribute", Splitter.SplitTextByDelimiter("_", QuoteStyle.Csv), {"Attribute.1", "Attribute.2"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Attribute.1", type text}, {"Attribute.2", type text}}),
    #"Pivoted Column" = Table.Pivot(#"Changed Type1", List.Distinct(#"Changed Type1"[Attribute.2]), "Attribute.2", "Value")
in
    #"Pivoted Column"

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

3 REPLIES 3
Ashish_Mathur
Super User
Super User

Hi,

This M code works

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("fZBLDoUgDEW38sJYk9sv9W3FuP9tSKuGGQxoIb2HE86zwXfwzmD8gD/QtkbIRZy9GMcoIWPLs7v0GrG6uLZBsDUBlfCPYBSaIxSToEsC05FhoZfAYUmwvKQHIEsAeRxlUoHRqJVJnwa8NtB6rOsLUHhmj5gGtAZwfZjYZ8CexfEYXDc=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [MODIFIED_DATE = _t, CUSTOMER_ID = _t, NEW_SRPIS = _t, NEW_VDPS = _t, NEW_EMAILS = _t, USED_SRPIS = _t, USED_VDPS = _t, USED_EMAILS = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"MODIFIED_DATE", type datetime}, {"CUSTOMER_ID", Int64.Type}, {"NEW_SRPIS", Int64.Type}, {"NEW_VDPS", Int64.Type}, {"NEW_EMAILS", Int64.Type}, {"USED_SRPIS", Int64.Type}, {"USED_VDPS", Int64.Type}, {"USED_EMAILS", Int64.Type}}),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"MODIFIED_DATE", "CUSTOMER_ID"}, "Attribute", "Value"),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Unpivoted Other Columns", "Attribute", Splitter.SplitTextByDelimiter("_", QuoteStyle.Csv), {"Attribute.1", "Attribute.2"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Attribute.1", type text}, {"Attribute.2", type text}}),
    #"Pivoted Column" = Table.Pivot(#"Changed Type1", List.Distinct(#"Changed Type1"[Attribute.2]), "Attribute.2", "Value")
in
    #"Pivoted Column"

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
amitchandak
Super User
Super User

@Anonymous , How doubt this unpivot twice case, if so refer

https://kohera.be/blog/power-bi/how-to-unpivot-twice/ 

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube
parry2k
Super User
Super User

@Anonymous I have addressed a similar question here in my post. Take a look.

 

I would  Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make efforts to give Kudos whoever helped to solve your problem. It is a token of appreciation!

Visit us at https://perytus.com, your one-stop shop for Power BI related projects/training/consultancy.

 



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.