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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
Isambard
Helper I
Helper I

Pivot to add lots of index columns

Hi,

 

I have a table of categories in column1, and values in column2. There are hundreds of categories but only about 5 values, which are the same across every category. I've indexed the values by merging a sort column with values 1-5 depending on the value in column2.

 

I'd like to pivot the table out so my headings are [category1value], [category1index], [category2value], [category2index], etc however I'm struggling to make this work. Pivoting seems to give an error try to sum or count the values, and when set to "don't agregate is throwing up an error, "Expression.Error: There were too many elements in the enumeration to complete the operation.
Details"

 

Thanks for any help.

4 REPLIES 4
wardy912
Resolver III
Resolver III

Hi @Isambard 

 

First, add a custom column to use as a unique identifier

[Category] & [Value]

wardy912_0-1752232132371.png

Then select the value and index columns and unpivot

 

wardy912_1-1752232244343.png

Add another custom column

[Category] & [Attribute]

wardy912_2-1752232353142.png

 

Remove the columns "Category", "Custom", "Attribute" and unpivot on the custom.1 column

wardy912_3-1752232426782.png

 

I hope this helps! Please give a thumbs up and mark as solved if it does, thanks!

SundarRaj
Solution Supplier
Solution Supplier

Hi @Isambard ,

Please put out the sample data and the output data you want. Thanks

Sundar Rajagopalan

Thanks,

 

Current format is:

Category1A1
Category1B2
Category1C3
Category2A1
Category2A1
Category2C3
...  

 

I would like to rearange it like:

Category1ValueCategory1IndexCategory2ValueCategory2Index
A1A1
B2A1
C3C3

Hi @Isambard ,

Here's a dynamic PQ solution with a dynamic column headers solution. I'll attach the file link for your reference. I'll also the attach the images and the M code used. Thanks

File Link:
https://docs.google.com/spreadsheets/d/1Q67CS_Va2MNc7KqmTroQDoIhAq-Ikzoc/edit?usp=sharing&ouid=10475...

SundarRaj_0-1752233544717.png

 

Here's the code:
let
Source = #table(
{"Category", "Subcategory", "Value"},
{
{"Category1", "A", 1},
{"Category1", "B", 2},
{"Category1", "C", 3},
{"Category2", "A", 1},
{"Category2", "A", 1},
{"Category2", "C", 3}
}
),
Group = Table.Group(
Source,
{"Category"},
{
{
"Group",
each _[[Subcategory], [Value]],
type table [Category = text, Subcategory = text, Value = number]
}
}
),
FromCols = Table.TransformColumns(Group, {"Group", each Table.FromColumns(Table.ToRows(_))}),
Expand = Table.ExpandTableColumn(
FromCols,
"Group",
{"Column1", "Column2", "Column3"},
{"Column1", "Column2", "Column3"}
),
Transpose = Table.PromoteHeaders(Table.Transpose(Expand)),
DynamicColNames = List.Combine(
List.Transform(List.Distinct(Source[Category]), each {_, _ & "Index"})
),
Custom1 = Table.RenameColumns(
Transpose,
List.Zip({Table.ColumnNames(Transpose), DynamicColNames})
)
in
Custom1

Sundar Rajagopalan

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

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