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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
DaniRoAzpi
New Member

Delete rows with a condition in PowerQuery

I have a database, in which, I have several columns:

1.-Batch.

2.-Parameter.

3.-Inspection date.

A batch can have several parameters and each parameter can have a different inspection date. I would like to delete all the rows of batches that are missing one or more inspection dates, that is, if a batch is missing the inspection date even if it is for a single parameter, all the corresponding rows for that batch should be deleted, even those that do have an inspection date. 

Thanks in advance.

1 ACCEPTED SOLUTION
Rickmaurinus
Helper V
Helper V

Hey Dani,

 

Thanks for the question. It's a common scenario that can be a little tricky. Here's a way: 
 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("hZFLCsAgDETvknUFzUftWYp36LLHL65My0hcSXg4eeN10TNPoYPueeaFM0vKLbHROP4Ae+AEgESARoB5IAOgbl5gZFEEAN6iGAA+Fh0AGkVYFFGjiBZpdgeIK0pgDwYA3uwgqAevKbAHFGFRxOwBjJd9T5kXoFCuAYA/D6tXAmPd7KnwQwUAdVOFeiUa4wU=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Batch = _t, Parameter = _t, #"Inspection date" = (type date)]),
    ChType = Table.TransformColumnTypes(Source,{{"Batch", type text}, {"Parameter", type text}, {"Inspection date", type date}}),
    RowsWithNull = Table.SelectRows(ChType, each [Inspection date] = null),
    BatchesToRemove = List.Distinct( RowsWithNull[Batch] ),
    BackToSource = Source,
    RemoveRows = Table.SelectRows(BackToSource, each not List.Contains( BatchesToRemove, [Batch] ) )
in
    RemoveRows

 

You could try above steps. You would

1. Filter your data only the items with null

2. Retrieve the batches related to these

3. Filter the original dataset to exclude these

 

Cheers,

Rick

--------------------------------------------------

@ me in replies or I'll lose your thread

 

Master Power Query M? -> https://powerquery.how

Read in-depth articles? -> BI Gorilla

Youtube Channel: BI Gorilla

 

If this post helps, then please consider accepting it as the solution to help other members find it more quickly.

View solution in original post

5 REPLIES 5
Rickmaurinus
Helper V
Helper V

Hey Dani,

 

Thanks for the question. It's a common scenario that can be a little tricky. Here's a way: 
 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("hZFLCsAgDETvknUFzUftWYp36LLHL65My0hcSXg4eeN10TNPoYPueeaFM0vKLbHROP4Ae+AEgESARoB5IAOgbl5gZFEEAN6iGAA+Fh0AGkVYFFGjiBZpdgeIK0pgDwYA3uwgqAevKbAHFGFRxOwBjJd9T5kXoFCuAYA/D6tXAmPd7KnwQwUAdVOFeiUa4wU=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Batch = _t, Parameter = _t, #"Inspection date" = (type date)]),
    ChType = Table.TransformColumnTypes(Source,{{"Batch", type text}, {"Parameter", type text}, {"Inspection date", type date}}),
    RowsWithNull = Table.SelectRows(ChType, each [Inspection date] = null),
    BatchesToRemove = List.Distinct( RowsWithNull[Batch] ),
    BackToSource = Source,
    RemoveRows = Table.SelectRows(BackToSource, each not List.Contains( BatchesToRemove, [Batch] ) )
in
    RemoveRows

 

You could try above steps. You would

1. Filter your data only the items with null

2. Retrieve the batches related to these

3. Filter the original dataset to exclude these

 

Cheers,

Rick

--------------------------------------------------

@ me in replies or I'll lose your thread

 

Master Power Query M? -> https://powerquery.how

Read in-depth articles? -> BI Gorilla

Youtube Channel: BI Gorilla

 

If this post helps, then please consider accepting it as the solution to help other members find it more quickly.

Vijay_A_Verma
Super User
Super User

Can you please post some dummy data and explain with respect to that dummy data?

Yes, no problem.

This could be an example of my data:

BatchParameterInspection date
xxxxx1ppppp125/07/2023
xxxxx1ppppp229/07/2023
xxxxx1ppppp329/07/2023
xxxxx1ppppp429/07/2023
xxxxx1ppppp520/07/2023
xxxxx1ppppp629/07/2023
xxxxx2ppppp113/07/2023
xxxxx2ppppp215/07/2023
xxxxx2ppppp328/07/2023
xxxxx2ppppp413/07/2023
xxxxx2ppppp515/07/2023
xxxxx2ppppp628/07/2023
xxxxx2ppppp729/07/2023
xxxxx2ppppp830/07/2023
xxxxx3ppppp115/07/2023
xxxxx3ppppp228/07/2023
xxxxx3ppppp313/07/2023
xxxxx3ppppp415/07/2023
xxxxx3ppppp528/07/2023
xxxxx3ppppp6 
xxxxx3ppppp702/08/2023
xxxxx4ppppp117/07/2023
xxxxx4ppppp2 
xxxxx4ppppp3 
xxxxx4ppppp428/07/2023
xxxxx4ppppp513/07/2023
xxxxx4ppppp615/07/2023
xxxxx4ppppp7 

 

My goal with this dummy data would be to delete ALL the rows corresponding batch xxxxx3 (it has 1 missing inspection date) and batch xxxxx4, as it has 3 missing inspection dates.

 

Many thanks.

Insert this statement where #"Changed Type" should be replaced with your previous step

= Table.Combine(List.Select(Table.Group(#"Changed Type", {"Batch"}, {{"All", each _}})[All], (x)=> not List.Contains(x[Inspection date], null)))

Complete code in action here

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("hZFLCsAgDETvknUFzUftWYp36LLHL65My0hcSXg4eeN10TNPoYPueeaFM0vKLbHROP4Ae+AEgESARoB5IAOgbl5gZFEEAN6iGAA+Fh0AGkVYFFGjiBZpdgeIK0pgDwYA3uwgqAevKbAHFGFRxOwBjJd9T5kXoFCuAYA/D6tXAmPd7KnwQwUAdVOFeiUa4wU=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Batch = _t, Parameter = _t, #"Inspection date" = (type date)]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Batch", type text}, {"Parameter", type text}, {"Inspection date", type date}}),
    #"Grouped Rows" = Table.Combine(List.Select(Table.Group(#"Changed Type", {"Batch"}, {{"All", each _}})[All], (x)=> not List.Contains(x[Inspection date], null)))
in
    #"Grouped Rows"

 

Is the goal to FILTER records in Power Query based on some criteria (DELETE, in my opinion, is a DATABASE operation, while FILTER is a Power Query operation)?

 

If your goal is to FILTER and remove any Batch that does not have all the Inspections, then i would do this:

DUPLICATE the query. On the Duplicate, do a FILTER to remove Inspection Dates that are NULL or Blank. Then do a GROUP BY on Batch and COUNT of Rows for the Aggregation. Then Filter the Result for Count = 6. Finally, JOIN that (inner join) back to the original query on Batch.




Did I answer your question? If so, mark my post as a solution. Also consider helping someone else in the forums!

Proud to be a Super User!





Helpful resources

Announcements
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.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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