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

Learn from the best! Meet the four finalists headed to the FINALS of the Power BI Dataviz World Championships! Register now

Reply
YasserEE
New Member

Pivot column with duplicate attribute

Hi there Power BI Experts,

I am running into an issue where I have duplicate "attribute" values where I cannot convert them into columns.

I'm sharing just an example of this, in reality, I have over 30 other Attributes with the same issue.

 

Current format

 

companyid  value attribute
5608101CompLegalCountry
5608101CompLocationCountry
560813001 CompIndustry
560822CompStockId
560823CompStockId

 

 

Wanted Format

 

companyid CompLegalCountry CompLocationCountry CompIndustry CompStockId CompStockId.1
5608101101130012223

 

Any help is much appreciated:)

1 ACCEPTED SOLUTION
BA_Pete
Super User
Super User

Hi @YasserEE ,

 

It's a bit fiddly, but here's how I approached it:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjUzsFDSUTI0MASSzvm5BT6p6Yk5zvmleSVFlUqxOthU5CcnlmTm52FTZGxgYKgAVeeZl1JajKbAyAgqGVySn5ztmYIiZ4wuFwsA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"companyid " = _t, value = _t, attribute = _t]),
    groupCompAndAttrib = Table.Group(Source, {"companyid ", "attribute"}, {{"data", each _, type table [#"companyid "=nullable text, #" value"=nullable text, #" attribute"=nullable text]}}),
    addNestedIndex = Table.TransformColumns(groupCompAndAttrib, {"data", each Table.AddIndexColumn(_, "index", 0, 1)}),
    expandNestedData = Table.ExpandTableColumn(addNestedIndex, "data", {"value", "index"}, {"value", "index"}),
    mergeAttribAndIndex = Table.CombineColumns(Table.TransformColumnTypes(expandNestedData, {{"index", type text}}, "en-GB"),{"attribute", "index"},Combiner.CombineTextByDelimiter(".", QuoteStyle.None),"attribute"),
    pivotAttribute = Table.Pivot(mergeAttribAndIndex, List.Distinct(mergeAttribAndIndex[attribute]), "attribute", "value")
in
    pivotAttribute

 

 

Summary:

1) Group table on [companyid] and [attribute] using 'All Rows' aggregator to create nested tables.

2) Add index column to nested tables to identify different instances of the same [attribute].

3) Expand nested tables back out and merge [attribute] with [index] with a '.' delimiter

4) Pivot [attribute] using [value] as value and set as 'Do not aggregate'.

 

This gives me the following output:

BA_Pete_0-1644920870523.png

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




View solution in original post

2 REPLIES 2
BA_Pete
Super User
Super User

Hi @YasserEE ,

 

It's a bit fiddly, but here's how I approached it:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjUzsFDSUTI0MASSzvm5BT6p6Yk5zvmleSVFlUqxOthU5CcnlmTm52FTZGxgYKgAVeeZl1JajKbAyAgqGVySn5ztmYIiZ4wuFwsA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"companyid " = _t, value = _t, attribute = _t]),
    groupCompAndAttrib = Table.Group(Source, {"companyid ", "attribute"}, {{"data", each _, type table [#"companyid "=nullable text, #" value"=nullable text, #" attribute"=nullable text]}}),
    addNestedIndex = Table.TransformColumns(groupCompAndAttrib, {"data", each Table.AddIndexColumn(_, "index", 0, 1)}),
    expandNestedData = Table.ExpandTableColumn(addNestedIndex, "data", {"value", "index"}, {"value", "index"}),
    mergeAttribAndIndex = Table.CombineColumns(Table.TransformColumnTypes(expandNestedData, {{"index", type text}}, "en-GB"),{"attribute", "index"},Combiner.CombineTextByDelimiter(".", QuoteStyle.None),"attribute"),
    pivotAttribute = Table.Pivot(mergeAttribAndIndex, List.Distinct(mergeAttribAndIndex[attribute]), "attribute", "value")
in
    pivotAttribute

 

 

Summary:

1) Group table on [companyid] and [attribute] using 'All Rows' aggregator to create nested tables.

2) Add index column to nested tables to identify different instances of the same [attribute].

3) Expand nested tables back out and merge [attribute] with [index] with a '.' delimiter

4) Pivot [attribute] using [value] as value and set as 'Do not aggregate'.

 

This gives me the following output:

BA_Pete_0-1644920870523.png

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




If you want the attribute headers to be exactly how you described then it's a bit more fiddly again:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjUzsFDSUTI0MASSzvm5BT6p6Yk5zvmleSVFlUqxOthU5CcnlmTm52FTZGxgYKgAVeeZl1JajKbAyAgqGVySn5ztmYIiZ4wuFwsA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"companyid " = _t, value = _t, attribute = _t]),
    groupCompAndAttrib = Table.Group(Source, {"companyid ", "attribute"}, {{"data", each _, type table [#"companyid "=nullable text, #" value"=nullable text, #" attribute"=nullable text]}}),
    addNestedIndex = Table.TransformColumns(groupCompAndAttrib, {"data", each Table.AddIndexColumn(_, "index", 0, 1)}),
    expandNestedData = Table.ExpandTableColumn(addNestedIndex, "data", {"value", "index"}, {"value", "index"}),
    replaceIfDuplicated = Table.ReplaceValue(expandNestedData, each [attribute], each if [index] > 0 then Text.Combine({[attribute], ".", Text.From([index])}) else [attribute], Replacer.ReplaceText,{"attribute"}),
    removeIndex = Table.RemoveColumns(replaceIfDuplicated,{"index"}),
    pivotAttribute = Table.Pivot(removeIndex, List.Distinct(removeIndex[attribute]), "attribute", "value")
in
    pivotAttribute

 

This uses a conditional replace instead of a column merge, but gives cleaner output:

BA_Pete_0-1644921495281.png

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

February Power BI Update Carousel

Power BI Monthly Update - February 2026

Check out the February 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.