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

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

Reply
Anonymous
Not applicable

Pivoting a table in Power Query

Hi Team,

I have a table like below

Nikhil_567_0-1736855020114.png

And I want to Pivot this table with aggregating, so the final table which is required after transformation in Power Query is like below-

ABC
123
456

 

How can we achieve this? I am getting an error because of duplicate values in Column1. 

11 REPLIES 11
Anonymous
Not applicable

Thanks for danextian, speedramps and bhanu_gautam's concern about this issue.

  

Hi, @Anonymous 

Maybe you can try the following M code, hope it helps:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTJUitWJVnICsozALGcgyxjMAsmawGVN4bJmSrGxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Column2", Int64.Type}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
    ColumnCount = Table.RowCount(Table.Distinct(Table.SelectColumns(#"Changed Type", {"Column1"}))),
    IndexGrouping = Table.AddColumn(#"Added Index", "IndexGrouping", each Number.IntegerDivide([Index], ColumnCount), Int64.Type),
    #"Removed Columns" = Table.RemoveColumns(IndexGrouping,{"Index"}),
    PivotedTable = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[Column1]), "Column1", "Column2"),
    #"Removed Columns1" = Table.RemoveColumns(PivotedTable,{"IndexGrouping"})
in
    #"Removed Columns1"

 

vfenlingmsft_0-1736907514526.png

 

 

I have attached the corresponding pbix file below.

 

 

I hope my suggestions give you good ideas, if you have any more questions, please clarify in a follow-up reply.
Best Regards,
Fen Ling,
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Anonymous
Not applicable

Hi, 

Thanks for taking out time and replying me a solution, however this doesn't help.

I have attached the actual data in the given path here 

Please note that we need to Pivot Column K (failed_record.1) for which the Values will be Column L (failed_record.2)

danextian
Super User
Super User

Hi @Anonymous 

 

Please try this:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTJUitWJVnICsozALGcgyxjMAsmawGVN4bJmSrGxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Column2", Int64.Type}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
    IndexGrouping = Table.AddColumn(#"Added Index", "IndexGrouping", each Number.IntegerDivide([Index], 3), Int64.Type),
    #"Removed Columns" = Table.RemoveColumns(IndexGrouping,{"Index"}),
    PivotedTable = Table.Pivot( #"Removed Columns", List.Distinct(#"Removed Columns"[Column1]), "Column1","Column2" ),
    #"Removed Columns1" = Table.RemoveColumns(PivotedTable,{"IndexGrouping"})
in
    #"Removed Columns1"

danextian_0-1736863241585.png

 

 





Dane Belarmino | Microsoft MVP | Proud to be a Super User!

Did I answer your question? Mark my post as a solution!


"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.
Anonymous
Not applicable

hello @danextian ,

you have used number 3 while grouping of indexes.

In the given example, there were 3 columns, however in the actual PROD data it's not fixed, can be 30, 80 or so on.

Any improvisations please?

Hi @

 

Please see the updated code below

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTJUitWJVnICsozALGcgyxjMAsmawGVN4bJmSrGxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Column2", Int64.Type}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
    IndexGrouping = Table.AddColumn(#"Added Index", "IndexGrouping", each
let 
DistinctRows = List.Count(List.Distinct(#"Added Index"[Column1]))
in
 Number.IntegerDivide([Index], DistinctRows), Int64.Type),
    #"Removed Columns" = Table.RemoveColumns(IndexGrouping,{"Index"}),
    PivotedTable = Table.Pivot( #"Removed Columns", List.Distinct(#"Removed Columns"[Column1]), "Column1","Column2" ),
    #"Removed Columns1" = Table.RemoveColumns(PivotedTable,{"IndexGrouping"})
in
    #"Removed Columns1"

The update is on the IndexGrouping applied step, with an additional variable that counts the distinct values of Column1 from the previous step. If this doesn’t resolve the issue, please provide sample data that accurately represents the actual data, rather than an overly simplified example. Include any other relevant information.





Dane Belarmino | Microsoft MVP | Proud to be a Super User!

Did I answer your question? Mark my post as a solution!


"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.
Anonymous
Not applicable

Hi @danextian ,

Your solution has taken me very close to the solution, however not sure why some rows are not populating and throwing error. Screenshot below

Nikhil_567_0-1736941159621.png

I have attached the actual data in the given path here 

Please note that we need to Pivot Column K (failed_record.1) for which the Values will be Column L (failed_record.2)

 

The solution assumes that you wil have this pattern in column1: ABCD..ABCD...ABCD..ABCD not ABC..ABCD..ACD...BCA





Dane Belarmino | Microsoft MVP | Proud to be a Super User!

Did I answer your question? Mark my post as a solution!


"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.
Anonymous
Not applicable

Oh @danextian  then any thoughts on the expected outcome as shared in the image?
Can we achieve this?

speedramps
Super User
Super User

Try this

 

create 2 measures 

 

min amount = MIN(yourdata[Column2])
max amount = MAX(yourdata[Column2])

 

 

create a matrix visual with

speedramps_0-1736858841541.png

Switch values to rows

speedramps_1-1736858930808.png

 

Remove totals

speedramps_2-1736858959400.png

Rename values to a single spaces

speedramps_3-1736859024011.png

Please click the [accept solution] and thumbs uup buttons please

 

 

 

Anonymous
Not applicable

have to do it in Power Query

bhanu_gautam
Super User
Super User

@Anonymous , Try using this query

 

let
Source = YourTable, // Replace with your actual table name
GroupedRows = Table.Group(Source, {"Column1"}, {{"AllData", each _, type table [Column1=nullable text, Column2=nullable number]}}),
AddedIndex = Table.TransformColumns(GroupedRows, {"AllData", each Table.AddIndexColumn(_, "Index", 1, 1, Int64.Type)}),
ExpandedTable = Table.ExpandTableColumn(AddedIndex, "AllData", {"Column1", "Column2", "Index"}),
PivotedTable = Table.Pivot(ExpandedTable, List.Distinct(ExpandedTable[Column1]), "Column1", "Column2")
in
PivotedTable




Did I answer your question? Mark my post as a solution! And Kudos are appreciated

Proud to be a Super User!




LinkedIn






Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

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