Forum Discussion

SBC's avatar
SBC
Helper III
1 year ago
Solved

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
  • 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"

3 Replies

  • 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
        已透视列
  • 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"

  • BeaBF's avatar
    BeaBF
    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