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
Zosy
Helper II
Helper II

Pivot columns returns errors and duplicates rows

Hi,

I appreciate if anyone could help.

I am trying to pivot a dataset but PQ returns errors. I did try to insert an index column, but it didn't seem to help. Maybe I added it wrong.
This is the table I have:
Original dataset.JPG
What I want to achieve by pivoting the columns.

Pivot Columns.JPG


Dataset with the steps I did:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("zZXRboIwFIZfpeF2im2lgJeoxJBF1wjuxnlBXFWyWRZgxsffqZkmyhYM2rGrc5o0/fK1f9v53KAd6nYopl2jZQRyhzgn0DmYEAyVMuJCUa2//xAyF9AN0q1q4yJJJYIxT/MikWvo+rMwmPhhiGaTIArV1MhYtO4MGfrcm0ZjfxLBYPwYjZB1BaRLLNNht7iEng5MUzbe87+woWplm7qm1pjVotRzcVzT1pmyOpBmTKoS9icmlqpuj92mUpWvWpR6LoQQs6f1HauHacqmKmX6bEYY4+7h4DFm5HtxbBJ2xvD3y00s1wINk9VKZEIuRX5u9B7Lt1O9CtK+D8U+QjgP2sDpWWrgMHr4jonDcGnHvNedyIokh0XRyyfG1EY8S7ep2sEL4K93SCu2dHCNuJ6uWAOupMNK4QHO4R5YP/2AfUgEGmzibC3yIzGQBaQoL6rexacp1w09sxw9wM4uvgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [postingDate = _t, documentNumber = _t, accountNumber = _t, debitAmount = _t, creditAmount = _t, account.category = _t, account.subCategory = _t, account.accountType = _t, dimensionSetLines.displayName = _t, dimensionSetLines.valueCode = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"postingDate", type date}, {"documentNumber", type text}, {"accountNumber", Int64.Type}, {"debitAmount", type number}, {"creditAmount", type number}, {"account.category", type text}, {"account.subCategory", type text}, {"account.accountType", type text}, {"dimensionSetLines.displayName", type text}, {"dimensionSetLines.valueCode", type text}}),
    #"Grouped Rows1" = Table.Group(#"Changed Type", {"postingDate", "documentNumber", "accountNumber", "debitAmount", "creditAmount", "account.category", "account.subCategory", "account.accountType", "dimensionSetLines.displayName"}, {{"Dep2", each _, type table [postingDate=nullable date, documentNumber=nullable text, accountNumber=nullable number, debitAmount=nullable number, creditAmount=nullable number, account.category=nullable text, account.subCategory=nullable text, account.accountType=nullable text, dimensionSetLines.displayName=nullable text, dimensionSetLines.valueCode=nullable text]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows1", "Index", each Table.AddIndexColumn([Dep2],"Index",1)),
    #"Expanded Index" = Table.ExpandTableColumn(#"Added Custom", "Index", {"dimensionSetLines.valueCode", "Index"}, {"Index.dimensionSetLines.valueCode", "Index.Index"}),
    #"Removed Columns5" = Table.RemoveColumns(#"Expanded Index",{"Dep2"}),
    #"Pivoted Column1" = Table.Pivot(#"Removed Columns5", List.Distinct(#"Removed Columns5"[dimensionSetLines.displayName]), "dimensionSetLines.displayName", "Index.dimensionSetLines.valueCode")
in
    #"Pivoted Column1"

 

 

Kind regards,
Zosy

1 ACCEPTED SOLUTION
HotChilli
Super User
Super User

This one took a bit of working out.

The code you have posted works (i.e. no errors) but doesn't give the result you want.  I think this has confused people, it confused me) because I was expecting code with the error.

Anyway,  the code posted is your attempt to fix the problem and I think you have the right approach.

However  after the 'Index Expanded' step, the data types on the 2 last columns are 'Any' and this prevents the Pivot from working in the way you want.

So assign the correct data type on those two last columns and I think it will work.

I haven't checked it too closely so you'll still need to do your own testing.

Let me know how it goes

View solution in original post

4 REPLIES 4
HotChilli
Super User
Super User

This one took a bit of working out.

The code you have posted works (i.e. no errors) but doesn't give the result you want.  I think this has confused people, it confused me) because I was expecting code with the error.

Anyway,  the code posted is your attempt to fix the problem and I think you have the right approach.

However  after the 'Index Expanded' step, the data types on the 2 last columns are 'Any' and this prevents the Pivot from working in the way you want.

So assign the correct data type on those two last columns and I think it will work.

I haven't checked it too closely so you'll still need to do your own testing.

Let me know how it goes

Apologies for the confusion. Indeed it was just the data type for the index column. I really apperciate your help.

rajulshah
Resident Rockstar
Resident Rockstar

@Zosy 

 

If you will click on the error, you would get the error message where you may get an idea as what might be the issue.

I did look to see what was wrong and the started searching online how could I fix it. In the majority of  cases people fixed it by adding an Index column and Group by function. I tried it too but it did not return what I need.
Zosy_0-1679145056515.png



Helpful resources

Announcements
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.

Top Kudoed Authors