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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Maha1
Helper II
Helper II

Pivot Error in Power Query

I am facing issue with pivoting the columns in power query. below is the error and my code:

 

Maha1_0-1709638300886.png

 

 

 

Maha1_0-1709638153987.png

 

 

 

 

let
    Source = Xml.Tables(Web.Contents("https://sv211140-cuic1XXXXX")),
    row = Source{0}[row],
    #"Expanded column" = Table.ExpandTableColumn(row, "column", {"Element:Text", "Attribute:index", "Attribute:name"}, {"Element:Text", "Attribute:index.1", "Attribute:name"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded column",{"Attribute:index.1", "Attribute:index"}),
    #"Pivoted Column" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[#"Attribute:name"]), "Attribute:name", "Element:Text")
in
    #"Pivoted Column"

 

 

 

 

1 ACCEPTED SOLUTION
v-xinruzhu-msft
Community Support
Community Support

Hi @Maha1 

The solution  @BA_Pete  provided is excellent, and i want to do some stretching for your solution @BA_Pete  so that can provide more information for the user.

You need to add an index column group by the attribute type.

let
    Source = Xml.Tables(Web.Contents("https://sv211140-cuic1XXXXX")),
    row = Source{0}[row],
    #"Expanded column" = Table.ExpandTableColumn(row, "column", {"Element:Text", "Attribute:index", "Attribute:name"}, {"Element:Text", "Attribute:index.1", "Attribute:name"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded column",{"Attribute:index.1", "Attribute:index"})
   #"Grouped Rows" = Table.Group(#"Removed Columns", {"Attribute:name"}, {{"Count", each Table.AddIndexColumn(_,"Index",1,1),type table}}),
    #"Expanded Count" = Table.ExpandTableColumn(#"Grouped Rows", "Count", {"Element:Text", "Index"}, {"Element:Text", "Index"}),
    #"Pivoted Column" = Table.Pivot(#"Expanded Count", List.Distinct(#"Expanded Count"[#"Attribute:name"]), "Attribute:name", "Element:Text")
    in
    #"Pivoted Column"

Then expanded it, and then pivot it.

 

If this help, please consider to mark the solution and the solution @BA_Pete  provided for more people to refer to.

 

Best Regards!

Yolo Zhu

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

View solution in original post

2 REPLIES 2
v-xinruzhu-msft
Community Support
Community Support

Hi @Maha1 

The solution  @BA_Pete  provided is excellent, and i want to do some stretching for your solution @BA_Pete  so that can provide more information for the user.

You need to add an index column group by the attribute type.

let
    Source = Xml.Tables(Web.Contents("https://sv211140-cuic1XXXXX")),
    row = Source{0}[row],
    #"Expanded column" = Table.ExpandTableColumn(row, "column", {"Element:Text", "Attribute:index", "Attribute:name"}, {"Element:Text", "Attribute:index.1", "Attribute:name"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded column",{"Attribute:index.1", "Attribute:index"})
   #"Grouped Rows" = Table.Group(#"Removed Columns", {"Attribute:name"}, {{"Count", each Table.AddIndexColumn(_,"Index",1,1),type table}}),
    #"Expanded Count" = Table.ExpandTableColumn(#"Grouped Rows", "Count", {"Element:Text", "Index"}, {"Element:Text", "Index"}),
    #"Pivoted Column" = Table.Pivot(#"Expanded Count", List.Distinct(#"Expanded Count"[#"Attribute:name"]), "Attribute:name", "Element:Text")
    in
    #"Pivoted Column"

Then expanded it, and then pivot it.

 

If this help, please consider to mark the solution and the solution @BA_Pete  provided for more people to refer to.

 

Best Regards!

Yolo Zhu

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

BA_Pete
Super User
Super User

Hi @Maha1 ,

 

You've not included the actual error description in your post, but my guess is it's a "not enough items to enumerate" error.

If so, then try adding an Index column to the table before you pivot.

 

Pete



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

Proud to be a Datanaut!




Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors
Top Kudoed Authors