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

The Power BI Data Visualization World Championships is back! It's time to submit your entry. Live now!

Reply
Daniel_Jesus
Frequent Visitor

How to remove specific rows based on a condition?

Hello everyone,

 

I have a table in a power bi report organized like you can see below:

Daniel_Jesus_1-1657211140107.png

 

And this is what I'm trying to do using power query:

 

Daniel_Jesus_2-1657211370574.png

 

Explaining: I have a column named "REV", and for each item in REV (I may have: REV0, REV1, REV2, REV3 and REV4) I need to get the lastest date for REV, removing all other lines. Note that for some REV (in this case REV1) I have a unique date and should keep it. I tried some approaches, but it didn't work. 

If you have any ideas I would appreciate. For more details just ask me.

 

Data in a table format:

MODELAVERAGEREVMONTHDATEITEM_N
PMED BASE65,93REV1JUL07/04/20221
ECMW65,85REV1JUL07/04/20222
GEFS64,22REV1JUL07/04/20223
ETA65,22REV1JUL07/04/20224
PMED BASE64,49REV2JUL07/05/20221
ECMW64,84REV2JUL07/05/20222
GEFS65,78REV2JUL07/05/20223
ETA64,87REV2JUL07/05/20224
PMED BASE61,04REV2JUL07/06/20221
ECMW62,40REV2JUL07/06/20222
GEFS65,76REV2JUL07/06/20223
ETA64,71REV2JUL07/06/20224
PMED BASE55,70REV0JUL07/01/20221
ECMW55,70REV0JUL07/01/20222
GEFS55,70REV0JUL07/01/20223
ETA55,70REV0JUL07/01/20224
PMED BASE55,70REV0JUL07/02/20221
ECMW55,70REV0JUL07/02/20222
GEFS55,70REV0JUL07/02/20223
ETA55,70REV0JUL07/02/20224

 

 

Thank you in advance,

Hope you're good.

 

 

 

2 ACCEPTED SOLUTIONS
ImkeF
Community Champion
Community Champion

Hello @Daniel_Jesus ,
Please paste the following code into the advanced editor and follow the steps:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("nc/dCsIgFMDxVxleD6Zn50zXnZUF0SBaHxey93+NzHUxS/AsEA8IP45/78VlcPtqa0cnatFR34ZxdQ8Vxul+DrdusAEJUCnYSBlOZYfwrMRUe+F2wzM6Q0wH0R3dYXw7BGC6dt53s3Edm2FkSSNiP2NYYCo3okGmSxtJG6ZLGtFoJss0Kpn5a1duBJRM993YMV3aqBWT/TYS6c9f5QKrYuMKlzSucMvGFYzbCH82Zh2jMevKjVkWGqcX", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [MODEL = _t, AVERAGE = _t, REV = _t, MONTH = _t, DATE = _t, ITEM_N = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"MODEL", type text}, {"AVERAGE", Int64.Type}, {"REV", type text}, {"MONTH", type text}, {"DATE", type datetime}, {"ITEM_N", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"REV", "MONTH"}, {{"All", each Table.Distinct(Table.Buffer(Table.Sort(_, {{"DATE", Order.Descending}})), {"MODEL"})}}),
    #"Expanded All" = Table.ExpandTableColumn(#"Grouped Rows", "All", {"MODEL", "AVERAGE", "DATE", "ITEM_N"}, {"MODEL", "AVERAGE", "DATE", "ITEM_N"})
in
    #"Expanded All"



Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

View solution in original post

Daniel_Jesus
Frequent Visitor

Hey, Imke

Thanks for sharing your answer with me. It seems to work as I asked, but I need to make some changes. Would you mind to help me again?

 

Here're the changes:

1 - The table data source is an excel file, how can I read an excel file as a source in your code? Need that because everyday the data updates with new dates per REV.

 

2 - I committed a mistake while creating the post, in the column "AVERAGE" I have float values. But I left the commas (portuguese version) instead use ".". I'm saying that because after applying the code the values became Integer, example: 65,93 → 6593 and I need 65.93. Tried to get the right float values using the power query editor, but couldn't.

 

3 - Is there a way to change the date format: 04/07/2022 to 07/04/2022 (used in Brazil)? I tried in the Power Query Editor but nothing happens.

 

 

Feel free to ask for more information. Thank you again.

View solution in original post

4 REPLIES 4
AlexisOlson
Super User
Super User

There are lots of ways to do this. I've written about a few methods in this community blog post:

Select Distinct Rows Ordered by Another Column

Daniel_Jesus
Frequent Visitor

Hey, Imke

Thanks for sharing your answer with me. It seems to work as I asked, but I need to make some changes. Would you mind to help me again?

 

Here're the changes:

1 - The table data source is an excel file, how can I read an excel file as a source in your code? Need that because everyday the data updates with new dates per REV.

 

2 - I committed a mistake while creating the post, in the column "AVERAGE" I have float values. But I left the commas (portuguese version) instead use ".". I'm saying that because after applying the code the values became Integer, example: 65,93 → 6593 and I need 65.93. Tried to get the right float values using the power query editor, but couldn't.

 

3 - Is there a way to change the date format: 04/07/2022 to 07/04/2022 (used in Brazil)? I tried in the Power Query Editor but nothing happens.

 

 

Feel free to ask for more information. Thank you again.

I did some changes, after study your code better, and I accomplished what I expected. Thank you so much, Imke. Please, feel free to answer my last comment.

ImkeF
Community Champion
Community Champion

Hello @Daniel_Jesus ,
Please paste the following code into the advanced editor and follow the steps:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("nc/dCsIgFMDxVxleD6Zn50zXnZUF0SBaHxey93+NzHUxS/AsEA8IP45/78VlcPtqa0cnatFR34ZxdQ8Vxul+DrdusAEJUCnYSBlOZYfwrMRUe+F2wzM6Q0wH0R3dYXw7BGC6dt53s3Edm2FkSSNiP2NYYCo3okGmSxtJG6ZLGtFoJss0Kpn5a1duBJRM993YMV3aqBWT/TYS6c9f5QKrYuMKlzSucMvGFYzbCH82Zh2jMevKjVkWGqcX", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [MODEL = _t, AVERAGE = _t, REV = _t, MONTH = _t, DATE = _t, ITEM_N = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"MODEL", type text}, {"AVERAGE", Int64.Type}, {"REV", type text}, {"MONTH", type text}, {"DATE", type datetime}, {"ITEM_N", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"REV", "MONTH"}, {{"All", each Table.Distinct(Table.Buffer(Table.Sort(_, {{"DATE", Order.Descending}})), {"MODEL"})}}),
    #"Expanded All" = Table.ExpandTableColumn(#"Grouped Rows", "All", {"MODEL", "AVERAGE", "DATE", "ITEM_N"}, {"MODEL", "AVERAGE", "DATE", "ITEM_N"})
in
    #"Expanded All"



Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! It's time to submit your entry.

January Power BI Update Carousel

Power BI Monthly Update - January 2026

Check out the January 2026 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.