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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

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
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel

Power BI Monthly Update - May 2024

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

LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.