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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
joshua1990
Post Prodigy
Post Prodigy

Group Table with multiple conditions

Hi all!

I have a table structured in this way:

Sales Nr Sales Type Status
1 ABC-1 A
1 ABC-1 C
1 XYZ-1 A
2 ABC-5 A
3 ABC-9 A
3 ABC-9 B
4 XYZ-6 A

 

Now I would like to transform / group that tablw into this structure:

  1. Add 2 columns
    1. Display if Sales Type start with ABC or with XYZ
    2. Display if the Sales Type is still active
      1. Active = "A" or "C"
      2. In-Active = "B"

The result should look like this:

Sales Order Sales Type Order Is ABC Is XYZ Is Active XYZ Is Active ABC
1 ABC-1 TRUE FALSE TRUE TRUE
1 XYZ-1 FALSE TRUE TRUE TRUE
2 ABC-5 TRUE FALSE TRUE TRUE
3 ABC-9 TRUE FALSE FALSE FALSE

 

A Sales Order can have multiple Sales Type Orders as you can see in Sales Order "1".

For each Sales Order and Sales Type Order combination there should be just 1 row in the end.

 

How would you do that in PQ?

1 ACCEPTED SOLUTION
ImkeF
Community Champion
Community Champion

Hi @joshua1990 ,
I would do it like so:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXJ0ctYF00qxOqgiznCRiMgoJDVGUDWmcBFjqIglVhEnsIgJ1BwziJpYAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Sales Nr" = _t, #"Sales Type" = _t, Status = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Sales Nr", Int64.Type}, {"Sales Type", type text}, {"Status", type text}}),
    #"Grouped Rows" = Table.Group(
        #"Changed Type", 
        {"Sales Nr", "Sales Type"}, 
        {{"IsActive", each not List.Contains([Status], "B"), type nullable text}}
    ), 
    #"Inserted Text Before Delimiter" = Table.AddColumn(
        #"Grouped Rows", 
        "SalesType", 
        each Text.BeforeDelimiter([Sales Type], "-"), 
        type text
    ), 
    #"Added Custom" = Table.AddColumn(
        #"Inserted Text Before Delimiter", 
        "Custom", 
        each [
            IsABC       = [SalesType] = "ABC", 
            IsXYZ       = [SalesType] = "XYZ", 
            IsActiveXYZ = IsXYZ and [IsActive], 
            IsActiveABC = IsABC and [IsActive]
        ]
    ), 
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom", {"IsActive", "SalesType"}), 
    #"Expanded Custom" = Table.ExpandRecordColumn(
        #"Removed Columns", 
        "Custom", 
        {"IsABC", "IsXYZ", "IsActiveXYZ", "IsActiveABC"}
    )
in
    #"Expanded Custom"


Please also check enclosed file.

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

View solution in original post

1 REPLY 1
ImkeF
Community Champion
Community Champion

Hi @joshua1990 ,
I would do it like so:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXJ0ctYF00qxOqgiznCRiMgoJDVGUDWmcBFjqIglVhEnsIgJ1BwziJpYAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Sales Nr" = _t, #"Sales Type" = _t, Status = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Sales Nr", Int64.Type}, {"Sales Type", type text}, {"Status", type text}}),
    #"Grouped Rows" = Table.Group(
        #"Changed Type", 
        {"Sales Nr", "Sales Type"}, 
        {{"IsActive", each not List.Contains([Status], "B"), type nullable text}}
    ), 
    #"Inserted Text Before Delimiter" = Table.AddColumn(
        #"Grouped Rows", 
        "SalesType", 
        each Text.BeforeDelimiter([Sales Type], "-"), 
        type text
    ), 
    #"Added Custom" = Table.AddColumn(
        #"Inserted Text Before Delimiter", 
        "Custom", 
        each [
            IsABC       = [SalesType] = "ABC", 
            IsXYZ       = [SalesType] = "XYZ", 
            IsActiveXYZ = IsXYZ and [IsActive], 
            IsActiveABC = IsABC and [IsActive]
        ]
    ), 
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom", {"IsActive", "SalesType"}), 
    #"Expanded Custom" = Table.ExpandRecordColumn(
        #"Removed Columns", 
        "Custom", 
        {"IsABC", "IsXYZ", "IsActiveXYZ", "IsActiveABC"}
    )
in
    #"Expanded Custom"


Please also check enclosed file.

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

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

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.