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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
jose_cruzrodz
Frequent Visitor

Group table by specific column and identify if that particular group has a specific value

Greetings,

 

Here is a sample of my data:

IDSignedByPrice
ABC123Alondra$2,500
ABC123Peter$2,500
ABC123Roman$2,500
ZXCV4657Steven$100
HJKL4321John$100
HJKL4321Alexa$100
KMEF8293Mildred$47,890
KMEF8293Maria$47,890

 

Expected results:

IDSignedByPriceStatus
ABC123Alondra$2,500Passed
ABC123Peter$2,500Passed
ABC123Roman$2,500Passed
ZXCV4657Steven$100N/A
HJKL4321John$500N/A
HJKL4321Alexa$500N/A
KMEF8293Mildred$47,890Failed
KMEF8293Maria$47,890Failed

 

Compliance rules:

  1. If PRICE is >= $2,000 & SIGNEDBY = "Peter" --> STATUS = "Passed"
  2. If PRICE is < $2,000 --> STATUS = "N/A"
  3. Anything else --> STATUS = "Failed"

 General idea:

  • ID is a document, so all rows that belong to the same ID should be evaluated as a group to check if any row of the same group complies or not. For example, for ID ABC123, it has three rows of data, so since one of those rows match compliance rule A, then all rows of ID ABC123 should be labeled as "Passed". Rows of IDs ZXCV4657 and HJKL4321 match compliance rule B, due to the price, so each group should be labeled as "N/A". Rows of ID KMEF8293 failed because although the price is right, Peter did not show up in the SIGNEDBY column, so all rows of the group should be labeled as "Failed".

I have attempted adding a custom column to the main table in Power BI using IF, Countrows, Filter and Earlier, but I have had no luck. If the solution can be built in Power Query, better; but I am open to all your great solutions. Thanks!!! 😁

1 ACCEPTED SOLUTION
KNP
Super User
Super User

Give this code a try:

 

let
    Source = Table,
    GR1 = Table.Group(
        Source,
        {"ID"},
        {
            {"count", each Table.RowCount(_), Int64.Type},
            {"all", each _, type table [ID = nullable text, SignedBy = nullable text, Price = nullable number]},
            {"max", each List.Max([Price]), type nullable number}
        }
    ),
    AllSignedBy = Table.AggregateTableColumn(GR1, "all", {{"SignedBy", Text.Combine, "SignedBy"}}),
    AddStatus = Table.AddColumn(
        AllSignedBy,
        "Status",
        each
            if [max] >= 2000 and Text.Contains(Text.Lower([SignedBy]), "peter") then
                "Passed"
            else if [max] < 2000 then
                "N/A"
            else
                "Failed",
        type text
    ),
    ROC1 = Table.SelectColumns(AddStatus, {"ID", "Status"}),
    Merge1 = Table.NestedJoin(ROC1, {"ID"}, Source, {"ID"}, "Removed Other Columns", JoinKind.LeftOuter),
    Expand1 = Table.ExpandTableColumn(Merge1, "Removed Other Columns", {"SignedBy", "Price"}, {"SignedBy", "Price"})
in
    Expand1

 

Table = The source table you provided above. 

 

It's a little crude and can be improved upon but it should help get you what you're looking for.

 

Have I solved your problem?
Please click Accept as Solution so I don't keep coming back to this post, oh yeah, others may find it useful also ;).
chrome-9xf-Zagzel-B

If you found this post helpful, please give Kudos.
It gives me a sense of instant gratification and, if you give me Kudos enough times, magical unicorns will appear on your screen.
If you find my signature vaguely amusing, please give Kudos.
KIfp67uy-Sr
Proud to be a Super User!PBI-Super-User-Rank-30x30-1x

View solution in original post

2 REPLIES 2
KNP
Super User
Super User

Give this code a try:

 

let
    Source = Table,
    GR1 = Table.Group(
        Source,
        {"ID"},
        {
            {"count", each Table.RowCount(_), Int64.Type},
            {"all", each _, type table [ID = nullable text, SignedBy = nullable text, Price = nullable number]},
            {"max", each List.Max([Price]), type nullable number}
        }
    ),
    AllSignedBy = Table.AggregateTableColumn(GR1, "all", {{"SignedBy", Text.Combine, "SignedBy"}}),
    AddStatus = Table.AddColumn(
        AllSignedBy,
        "Status",
        each
            if [max] >= 2000 and Text.Contains(Text.Lower([SignedBy]), "peter") then
                "Passed"
            else if [max] < 2000 then
                "N/A"
            else
                "Failed",
        type text
    ),
    ROC1 = Table.SelectColumns(AddStatus, {"ID", "Status"}),
    Merge1 = Table.NestedJoin(ROC1, {"ID"}, Source, {"ID"}, "Removed Other Columns", JoinKind.LeftOuter),
    Expand1 = Table.ExpandTableColumn(Merge1, "Removed Other Columns", {"SignedBy", "Price"}, {"SignedBy", "Price"})
in
    Expand1

 

Table = The source table you provided above. 

 

It's a little crude and can be improved upon but it should help get you what you're looking for.

 

Have I solved your problem?
Please click Accept as Solution so I don't keep coming back to this post, oh yeah, others may find it useful also ;).
chrome-9xf-Zagzel-B

If you found this post helpful, please give Kudos.
It gives me a sense of instant gratification and, if you give me Kudos enough times, magical unicorns will appear on your screen.
If you find my signature vaguely amusing, please give Kudos.
KIfp67uy-Sr
Proud to be a Super User!PBI-Super-User-Rank-30x30-1x

Hello. Thanks for the input. It almost worked out of the box! The single modification that I had to do, after an hour or so of looking for what may be wrong and trying tons of stuff, was removing the Text.Lower portion of the AddStatus step. It works great. Now, I am modifying stuff a little bit to just add the Status column to existing main table, which has over 30 more columns. Thanks for your help!!!

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Prices go up Feb. 11th.

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

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