Forum Discussion
Assign custom labels to unpivoted data
- 3 years ago
Here you go PowerBI_Query
Here is the code:let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content], #"Promoted Headers" = Table.PromoteHeaders(Source, [PromoteAllScalars=true]), #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Promoted Headers", {"Heading 1"}, "Attribute", "Value"), RecordCount = Table.RowCount(#"Unpivoted Other Columns"), CategoryList = List.Repeat(CategoryLabels, RecordCount/373), TableWithCategory = Table.ToColumns(#"Unpivoted Other Columns") & {CategoryList}, #"Final Table" = Table.FromColumns(TableWithCategory, Table.ColumnNames(#"Unpivoted Other Columns") & {"Category"}) in #"Final Table"For clarity I got rid of the Changed Type step as it had 300+ change types. If you can get away with that, do that after the unpivot, but not the end of the world if done before.
Here is what I did:- Got the number of records
- Repeated the list of category labels in a separate list by the number of records divided by 373. If that is not evenly divisible by 373 you will need to add additional logic to truncate the last time it happens.
- Converted the original table to a list of columns, then appended the category list.
- Converted that list of columns back to a table and got the column names and reapplied them.
You can add an index column using the toolbar, starting at 1, and then just do if ranges, like
Table.AddColumn(PriorStepOrTableName, "Category", each if [Index] > 300 then "Cat5" else if [Index] > 250 then "Cat4" else if [Index] > 12 "Cat3" else if [Index] > 2 then "Cat2" else "Cat1", type text)
--Nate
The labels have to cycle every 373 rows
- edhans3 years ago
Community Champion
Here you go PowerBI_Query
Here is the code:let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content], #"Promoted Headers" = Table.PromoteHeaders(Source, [PromoteAllScalars=true]), #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Promoted Headers", {"Heading 1"}, "Attribute", "Value"), RecordCount = Table.RowCount(#"Unpivoted Other Columns"), CategoryList = List.Repeat(CategoryLabels, RecordCount/373), TableWithCategory = Table.ToColumns(#"Unpivoted Other Columns") & {CategoryList}, #"Final Table" = Table.FromColumns(TableWithCategory, Table.ColumnNames(#"Unpivoted Other Columns") & {"Category"}) in #"Final Table"For clarity I got rid of the Changed Type step as it had 300+ change types. If you can get away with that, do that after the unpivot, but not the end of the world if done before.
Here is what I did:- Got the number of records
- Repeated the list of category labels in a separate list by the number of records divided by 373. If that is not evenly divisible by 373 you will need to add additional logic to truncate the last time it happens.
- Converted the original table to a list of columns, then appended the category list.
- Converted that list of columns back to a table and got the column names and reapplied them.