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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
OneWithQuestion
Post Prodigy
Post Prodigy

Power Query M - inable to filter out rows using AND condition (only OR)

I have a table and I want to exclude rows where one column has a specific value and another column is blank.

 

All other rows I want to keep.

 

I want to exclude all rows where Category=Sports AND Sub Category ="" (blank)  (So take that subset and remove it)

 

My table:

 

ExampleTable.PNG

Product Category Sub Category

NerfToys 
HeManToysAction
GI JoeToysAction
500 Piece TigerToysPuzzle
Star Wars ReplicaToys 
BasketBallSports 
BaseballSports 
BikeSportsOutdoor
LipstickMakeupFace
Beauty CreamMakeup 

 

 

What happens if I try to filter to say  Everything BUT Sports AND Everything BUT blank

 

 

= Table.SelectRows(#"Changed Type", each ([Category] <> "Sports") and ([Sub Category] <> ""))

Product Category Sub Category

HeManToysAction
GI JoeToysAction
500 Piece TigerToysPuzzle
LipstickMakeupFace

 

excluded.PNG

 I would expect to still see all the rows that have a blank "Sub Category" but were NOT a Category of Sports.

 

I would also expect to see rows that are Sports but did NOT have a blank for Sub Category

 

ProductCategorySub Category
NerfToys 
HeManToysAction
GI JoeToysAction
500 Piece TigerToysPuzzle
Star Wars ReplicaToys 
BikeSportsOutdoor
LipstickMakeupFace
Beauty CreamMakeup 

 

 

1 ACCEPTED SOLUTION
v-shex-msft
Community Support
Community Support

Hi @OneWithQuestion,

 

I'd like to suggest you to use if statement to filter rows:

 

Sample:

= Table.SelectRows(#"Changed Type", each if [Category]="Sports" and [Sub Category] ="" then false else true)

8.PNG

 

Full query:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bY69CsIwEIBfJWR26OIDWME/rBZbcCgdznjKkdiE5DK0T2+oQ4t0uuP7PrhrGnlB/5IrWds+pCHbVSMPWEA3sY1ist1o9kdxsrio1lkmSkKFoqY3+qkp4zAYHJuKwYs7+CBu6Awp+DucQ9DIORiTSOWs57nCx6IgjXN4jfy01o/uTC4wKZ1wARqjS8sO1O+ZHCFyL7Ye4TMPZNt+AQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Product = _t, Category = _t, #"Sub Category" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Product", type text}, {"Category", type text}, {"Sub Category", type text}}),
    FitlerRows = Table.SelectRows(#"Changed Type", each if [Category]="Sports" and [Sub Category] ="" then false else true)
in
    FitlerRows

 

BTW, I think you need to use 'OR' link your conditions, your requirement is remove rows which not matched with all conditions.

 

Modified formula:

 = Table.SelectRows(#"Changed Type", each [Category] <> "Sports" or [Sub Category] <> "")

 

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

View solution in original post

1 REPLY 1
v-shex-msft
Community Support
Community Support

Hi @OneWithQuestion,

 

I'd like to suggest you to use if statement to filter rows:

 

Sample:

= Table.SelectRows(#"Changed Type", each if [Category]="Sports" and [Sub Category] ="" then false else true)

8.PNG

 

Full query:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bY69CsIwEIBfJWR26OIDWME/rBZbcCgdznjKkdiE5DK0T2+oQ4t0uuP7PrhrGnlB/5IrWds+pCHbVSMPWEA3sY1ist1o9kdxsrio1lkmSkKFoqY3+qkp4zAYHJuKwYs7+CBu6Awp+DucQ9DIORiTSOWs57nCx6IgjXN4jfy01o/uTC4wKZ1wARqjS8sO1O+ZHCFyL7Ye4TMPZNt+AQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Product = _t, Category = _t, #"Sub Category" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Product", type text}, {"Category", type text}, {"Sub Category", type text}}),
    FitlerRows = Table.SelectRows(#"Changed Type", each if [Category]="Sports" and [Sub Category] ="" then false else true)
in
    FitlerRows

 

BTW, I think you need to use 'OR' link your conditions, your requirement is remove rows which not matched with all conditions.

 

Modified formula:

 = Table.SelectRows(#"Changed Type", each [Category] <> "Sports" or [Sub Category] <> "")

 

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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