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

Vote for your favorite vizzies from the Power BI Dataviz World Championship submissions. Vote now!

Reply
rfernandez
Regular Visitor

Transform table with duplicate values

Hello
I need to transform a table where there are duplicate values, the idea is to move from the first table to the third table, where the first and second columns do not have duplicate values ​​and  last column becomes a row.
Thank you.

namezonecode
ASouthX
ASouthY
ASouthZ
ASouthXX
BWestX
BWest 
BWestYY
CSouthX
CSouthXX
CSouth 
CSouthZZ

FIRST TABLE

 

 

namezoneXYZXXYYZZ
ASouth1     
ASouth 1    
ASouth  1   
ASouth   1  
BWest1     
BWest      
BWest    1 
CSouth1     
CSouth   1  
CSouth      
CSouth     1

SECOND TABLE

 

 

 

namezoneXYZXXYYZZ
ASouth1111  
BWest1   1 
CSouth1  1 1

THIRD TABLE

 

1 ACCEPTED SOLUTION
HotChilli
Community Champion
Community Champion

Duplicate the 'zone' column then pivot on the 'code' column using 'zone-Copy' for values.  You'll have to remove one of the columns after that to tidy up your results.

 

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUQrOLy3JANIRSrE6qCKRGCJRGCIREG1OQGZ4anEJ3BwkAQV0gUiIwc4YljtjGowspIAhEgV0UCwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [name = _t, zone = _t, code = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"name", type text}, {"zone", type text}, {"code", type text}}),
#"Duplicated Column" = Table.DuplicateColumn(#"Changed Type", "zone", "zone - Copy"),
#"Pivoted Column" = Table.Pivot(#"Duplicated Column", List.Distinct(#"Duplicated Column"[code]), "code", "zone - Copy", List.Count),
#"Removed Columns" = Table.RemoveColumns(#"Pivoted Column",{" "})
in
#"Removed Columns"

View solution in original post

3 REPLIES 3
Ashish_Mathur
Super User
Super User

Hi,

 

This M code works as well

 

let
    Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"name", type text}, {"zone", type text}, {"code", type text}}),
    #"Filtered Rows" = Table.SelectRows(#"Changed Type", each ([code] <> null)),
    #"Duplicated Column" = Table.DuplicateColumn(#"Filtered Rows", "code", "code - Copy"),
    #"Pivoted Column" = Table.Pivot(#"Duplicated Column", List.Distinct(#"Duplicated Column"[code]), "code", "code - Copy", List.Count)
in
    #"Pivoted Column"


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

Duplicate the 'zone' column then pivot on the 'code' column using 'zone-Copy' for values.  You'll have to remove one of the columns after that to tidy up your results.

 

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUQrOLy3JANIRSrE6qCKRGCJRGCIREG1OQGZ4anEJ3BwkAQV0gUiIwc4YljtjGowspIAhEgV0UCwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [name = _t, zone = _t, code = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"name", type text}, {"zone", type text}, {"code", type text}}),
#"Duplicated Column" = Table.DuplicateColumn(#"Changed Type", "zone", "zone - Copy"),
#"Pivoted Column" = Table.Pivot(#"Duplicated Column", List.Distinct(#"Duplicated Column"[code]), "code", "zone - Copy", List.Count),
#"Removed Columns" = Table.RemoveColumns(#"Pivoted Column",{" "})
in
#"Removed Columns"

that worked perfectly thanks 🙂

Helpful resources

Announcements
Sticker Challenge 2026 Carousel

Join our Community Sticker Challenge 2026

If you love stickers, then you will definitely want to check out our Community Sticker Challenge!

January Power BI Update Carousel

Power BI Monthly Update - January 2026

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

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.