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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
SBC
Helper III
Helper III

Need Help with Pivoting and Categorizing Data in Power BI

Hi,

I'm working on a Power BI report where I need to pivot a column and categorize values based on certain conditions. However, I'm encountering some issues while pivoting and would appreciate any help or insights you can provide.

 

Sample Data

Here's a sample of my data:
 
ProductNameTransactionDateProductCategoryGroupValue
Product A2024-01-01Category X112
Product B2024-01-02Category Y23
Product C2024-01-03Category Z34
Product D2024-01-04Category X45
Product E2024-01-05Category Y56
Product F2024-01-06Category Z67
Product G2024-01-07Category X78
Product H2024-01-08Category Y89
Product I2024-01-09Category Z910
Product J2024-01-10Category X1011
Product K2024-01-11Category Y1112
Product L2024-01-12Category Z1213

 

 

Desired Output

I want to pivot the 'Group' column and categorize the 'Value' column such that values less than 5 are shown under their respective group numbers, and values 5 or greater are shown under a single column labeled '5>='.
Here's how I want the output to look:
ProductNameTransactionDateProductCategory12345>=
Product A2024-01-01Category X12    
Product B2024-01-02Category Y 3   
Product C2024-01-03Category Z  4  
Product D2024-01-04Category X   5 
Product E2024-01-05Category Y    6
Product F2024-01-06Category Z    7
Product G2024-01-07Category X    8
Product H2024-01-08Category Y    9
Product I2024-01-09Category Z    10
Product J2024-01-10Category X    11
Product K2024-01-11Category Y    12
Product L2024-01-12Category Z    13

Issues Encountered

While trying to pivot the 'Group' column, I'm encountering errors. 
 
I would appreciate any advice on how to resolve the pivoting error and correctly categorize the 'Value' column. If you have any tips or solutions, please share them. Thank you in advance for your help!
 
 
Regards,
SBC
1 ACCEPTED SOLUTION
ThxAlot
Super User
Super User

Very strightforward,

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bZG7CsJAEEV/JWwdYWezeZUa31pYqiGFqFgGQiz8e2fSeGcQdos5cy5cmLZ1p6F/vO9jMnepCz7EmSd+PDS38fnqh09y5kEABdelv8ACAwEDF9nwz5TfoJ+hf53c1EXlL9GPtpCAXPkr9HPbR0Ch/DX6he0joFT+Bv3S9hFQKX+LfmX7CKiVv0O/tn0EkFeBPQR4ZS8mhEglDpggW4n+HPmIiWBLTYT4zN0X", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ProductName = _t, TransactionDate = _t, ProductCategory = _t, Group = _t, Value = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ProductName", type text}, {"TransactionDate", type date}, {"ProductCategory", type text}, {"Group", Int64.Type}, {"Value", Int64.Type}}),

    #"Transformed Column Group" = Table.TransformColumns(#"Changed Type", {"Group", each if _ < 5 then Text.From(_) else ">=5"}),
    #"Pivoted Column" = Table.Pivot(#"Transformed Column Group", List.Distinct(#"Transformed Column Group"[Group]), "Group", "Value", List.Sum)
in
    #"Pivoted Column"

ThxAlot_0-1738705624428.png



Expertise = List.Accumulate(


        {Days as from Today},


        {Skills and Knowledge},


        (Current, Everyday) => Current & Day.LearnAndPractise(Everyday)


)



View solution in original post

3 REPLIES 3
ThxAlot
Super User
Super User

Very strightforward,

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bZG7CsJAEEV/JWwdYWezeZUa31pYqiGFqFgGQiz8e2fSeGcQdos5cy5cmLZ1p6F/vO9jMnepCz7EmSd+PDS38fnqh09y5kEABdelv8ACAwEDF9nwz5TfoJ+hf53c1EXlL9GPtpCAXPkr9HPbR0Ch/DX6he0joFT+Bv3S9hFQKX+LfmX7CKiVv0O/tn0EkFeBPQR4ZS8mhEglDpggW4n+HPmIiWBLTYT4zN0X", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ProductName = _t, TransactionDate = _t, ProductCategory = _t, Group = _t, Value = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ProductName", type text}, {"TransactionDate", type date}, {"ProductCategory", type text}, {"Group", Int64.Type}, {"Value", Int64.Type}}),

    #"Transformed Column Group" = Table.TransformColumns(#"Changed Type", {"Group", each if _ < 5 then Text.From(_) else ">=5"}),
    #"Pivoted Column" = Table.Pivot(#"Transformed Column Group", List.Distinct(#"Transformed Column Group"[Group]), "Group", "Value", List.Sum)
in
    #"Pivoted Column"

ThxAlot_0-1738705624428.png



Expertise = List.Accumulate(


        {Days as from Today},


        {Skills and Knowledge},


        (Current, Everyday) => Current & Day.LearnAndPractise(Everyday)


)



ZhangKun
Super User
Super User

Add an auxiliary column to calculate whether it is greater than or equal to 5, and then pivot this auxiliary column.

let
    源 = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bZG7CsJAEEV/JWwdYWezeZUa31pYqiGFqFgGQiz8e2fSeGcQdos5cy5cmLZ1p6F/vO9jMnepCz7EmSd+PDS38fnqh09y5kEABdelv8ACAwEDF9nwz5TfoJ+hf53c1EXlL9GPtpCAXPkr9HPbR0Ch/DX6he0joFT+Bv3S9hFQKX+LfmX7CKiVv0O/tn0EkFeBPQR4ZS8mhEglDpggW4n+HPmIiWBLTYT4zN0X", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ProductName = _t, TransactionDate = _t, ProductCategory = _t, Group = _t, Value = _t]),
    更改的类型 = Table.TransformColumnTypes(源,{{"ProductName", type text}, {"TransactionDate", type date}, {"ProductCategory", type text}, {"Group", Int64.Type}, {"Value", Int64.Type}}),
    
    已添加自定义 = Table.AddColumn(更改的类型, "自定义", each if [Group] >= 5 then "5>=" else Text.From([Group])),
    已透视列 = Table.Pivot(已添加自定义, List.Distinct(已添加自定义[自定义]), "自定义", "Value", List.Sum)
in
    已透视列
BeaBF
Super User
Super User

@SBC Hi! try with:

 

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bZG7CsJAEEV/JWwdYWezeZUa31pYqiGFqFgGQiz8e2fSeGcQdos5cy5cmLZ1p6F/vO9jMnepCz7EmSd+PDS38fnqh09y5kEABdelv8ACAwEDF9nwz5TfoJ+hf53c1EXlL9GPtpCAXPkr9HPbR0Ch/DX6he0joFT+Bv3S9hFQKX+LfmX7CKiVv0O/tn0EkFeBPQR4ZS8mhEglDpggW4n+HPmIiWBLTYT4zN0X", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ProductName = _t, TransactionDate = _t, ProductCategory = _t, Group = _t, Value = _t]),

#"Changed Type" = Table.TransformColumnTypes(Source,{{"ProductName", type text}, {"TransactionDate", type date}, {"ProductCategory", type text}, {"Group", Int64.Type}, {"Value", Int64.Type}}),

// Step 1: Create a New Group Column for Pivoting
CategorizedTable = Table.AddColumn(#"Changed Type", "GroupCategory", each if [Group] >= 5 then "5>=" else Text.From([Group])),

// Step 2: Remove the 'Group' Column Since It's No Longer Needed
RemovedGroupColumn = Table.RemoveColumns(CategorizedTable, {"Group"}),

// Step 3: Pivot Table Using GroupCategory
PivotedTable = Table.Pivot(
RemovedGroupColumn,
List.Distinct(RemovedGroupColumn[GroupCategory]), // Ensure all columns are included
"GroupCategory",
"Value",
List.Sum
),

// Step 4: Ensure All Expected Columns Exist (Even if Missing in Data)
FinalTable = Table.SelectColumns(
PivotedTable,
{"ProductName", "TransactionDate", "ProductCategory", "1", "2", "3", "4", "5>="},
MissingField.UseNull // This ensures missing columns are still included
)
in
FinalTable

 

BBF

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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

Top Solution Authors