Reply
shayla
Frequent Visitor
Partially syndicated - Outbound

Filter Column A based on another Filter on Column B

Hi All, 

 

I would like to filter Quote ID based on the item column.

 

I would like to have all the lines from a quote which has AAA or DDD in the quote. 

 

How could I achive this in Power Query?

 

Thanks,

  

shayla_0-1721696512063.png

 

2 ACCEPTED SOLUTIONS
jennratten
Super User
Super User

Syndicated - Outbound

@shayla - here you are.  The primary difference between this solution and the one posted by @bhanu_gautam is looking to match cells containing AAA and DDD instead of an exact match.  There must be some white space characters in the cells with the Item values.  

Please try this:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXJ0dFRQitWBcJycnOBsZ2dnONvFxQXMNkJSY4SkxhhiEJwNEzdBtsAESbMpzNBYAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Quote ID" = _t, Item = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Quote ID", Int64.Type}, {"Item", type text}}),
    QuoteIdsByItem = Table.SelectRows ( 
        #"Changed Type", (x) => List.AnyTrue(List.Transform({"AAA","DDD"}, each Text.Contains((x[Item]), _, Comparer.OrdinalIgnoreCase ))) ),
    Custom2 = Table.SelectRows ( #"Changed Type", each List.Contains ( QuoteIdsByItem[Quote ID], [Quote ID] ) )
in
    Custom2

jennratten_0-1721714511760.png

 

If this post helps to answer your questions, please consider marking it as a solution so others can find it more quickly when faced with a similar challenge.

Proud to be a Microsoft Fabric Super User

View solution in original post

PavelAdam
Regular Visitor

Syndicated - Outbound

@shayla want to get every record (row) of such [Quote ID] value, that contains [Item] value either "AAA" or "DDD".
Solution workflow: load data, clean Item text values (trim, clean), group by [Quote ID], filter to [Quote ID] which passes test (presence of "AAA" or "DDD" value), expand groups to a flat table.

Solution code:

 

 

let
/*https://community.fabric.microsoft.com/t5/Power-Query/Filter-Column-A-based-on-another-Filter-on-Column-B/td-p/4054856*/
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXJ0dFRQitWBcJycnOBsZ2dnONvFxQXMNkJSY4SkxhhiEJwNEzdBtsAESbMpzNBYAA==", BinaryEncoding.Base64), Compression.Deflate)), type table [#"Quote ID" = Int64.Type, Item = Text.Type]),
    ChangedType = Table.TransformColumnTypes(Source,{{"Quote ID", Int64.Type}}),
    CleanTextValues = Table.TransformColumns( ChangedType, {"Item", each Text.Clean(Text.Trim(_)), type text}),
    GroupedRows = Table.Group(CleanTextValues, {"Quote ID"}, {{"AllRows", each _, type table [Quote ID=nullable number, Item=text]}}),
    FilterGroups = Table.SelectRows(GroupedRows, each List.ContainsAny([AllRows][Item], {"AAA", "DDD"})),
    ExpandGroupToTable = Table.ExpandTableColumn(FilterGroups, "AllRows", {"Item"}, {"Item"})
in
    ExpandGroupToTable

 

 

View solution in original post

5 REPLIES 5
PavelAdam
Regular Visitor

Syndicated - Outbound

@shayla want to get every record (row) of such [Quote ID] value, that contains [Item] value either "AAA" or "DDD".
Solution workflow: load data, clean Item text values (trim, clean), group by [Quote ID], filter to [Quote ID] which passes test (presence of "AAA" or "DDD" value), expand groups to a flat table.

Solution code:

 

 

let
/*https://community.fabric.microsoft.com/t5/Power-Query/Filter-Column-A-based-on-another-Filter-on-Column-B/td-p/4054856*/
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXJ0dFRQitWBcJycnOBsZ2dnONvFxQXMNkJSY4SkxhhiEJwNEzdBtsAESbMpzNBYAA==", BinaryEncoding.Base64), Compression.Deflate)), type table [#"Quote ID" = Int64.Type, Item = Text.Type]),
    ChangedType = Table.TransformColumnTypes(Source,{{"Quote ID", Int64.Type}}),
    CleanTextValues = Table.TransformColumns( ChangedType, {"Item", each Text.Clean(Text.Trim(_)), type text}),
    GroupedRows = Table.Group(CleanTextValues, {"Quote ID"}, {{"AllRows", each _, type table [Quote ID=nullable number, Item=text]}}),
    FilterGroups = Table.SelectRows(GroupedRows, each List.ContainsAny([AllRows][Item], {"AAA", "DDD"})),
    ExpandGroupToTable = Table.ExpandTableColumn(FilterGroups, "AllRows", {"Item"}, {"Item"})
in
    ExpandGroupToTable

 

 

jennratten
Super User
Super User

Syndicated - Outbound

@shayla - here you are.  The primary difference between this solution and the one posted by @bhanu_gautam is looking to match cells containing AAA and DDD instead of an exact match.  There must be some white space characters in the cells with the Item values.  

Please try this:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXJ0dFRQitWBcJycnOBsZ2dnONvFxQXMNkJSY4SkxhhiEJwNEzdBtsAESbMpzNBYAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Quote ID" = _t, Item = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Quote ID", Int64.Type}, {"Item", type text}}),
    QuoteIdsByItem = Table.SelectRows ( 
        #"Changed Type", (x) => List.AnyTrue(List.Transform({"AAA","DDD"}, each Text.Contains((x[Item]), _, Comparer.OrdinalIgnoreCase ))) ),
    Custom2 = Table.SelectRows ( #"Changed Type", each List.Contains ( QuoteIdsByItem[Quote ID], [Quote ID] ) )
in
    Custom2

jennratten_0-1721714511760.png

 

If this post helps to answer your questions, please consider marking it as a solution so others can find it more quickly when faced with a similar challenge.

Proud to be a Microsoft Fabric Super User

bhanu_gautam
Super User
Super User

Syndicated - Outbound

@shayla , either you can use m code for this or you can simply filter item rows which are equal to AAA or DDD

 

let
    Source = YourDataSource, // Replace with your actual data source
    FilteredItems = Table.SelectRows(Source, each ([item] = "AAA" or [item] = "DDD")),
    FilteredQuoteIDs = Table.SelectRows(Source, each List.Contains(FilteredItems[Quote ID], [Quote ID]))
in
    FilteredQuoteIDs



Did I answer your question? Mark my post as a solution! And Kudos are appreciated

Proud to be a Super User!




LinkedIn






Syndicated - Outbound

Hi @bhanu_gautam ,

 

It doens't seem working. could you please seperate the steps, as I am quite a newbie to power query.

 

Thanks,

Syndicated - Outbound

@shayla , Can you share your sample data 




Did I answer your question? Mark my post as a solution! And Kudos are appreciated

Proud to be a Super User!




LinkedIn






avatar user

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 FABINSIDER for a $400 discount!

FebPBI_Carousel

Power BI Monthly Update - February 2025

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

March2025 Carousel

Fabric Community Update - March 2025

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

Top Solution Authors (Last Month)
Top Kudoed Authors (Last Month)