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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
jerryr125
Helper III
Helper III

Remove records by date

Hi Everyone 

I have I would like to do the following:

* Based upon todays date, keep any records after todays date

* Based upon todays date, keep only the single most last record Max Date

 

Example:

Table input:

 

AIDDate
12341/1/2024
12345/1/2024
123411/14/2024
12341/1/2025
12343/1/2025
56787/1/2024
56789/1/2024
56783/3/2025




Table output:

 

 

AIDDate
123411/14/2024
12341/1/2025
12343/1/2025
56789/1/2024
56783/1/2025



Any thoughts ? Thanks - Jerry

2 ACCEPTED SOLUTIONS
serpiva64
Solution Sage
Solution Sage

Hi,

you can try something like this

let
// Load source data and decompress it
Source = Table.FromRows(
Json.Document(
Binary.Decompress(
Binary.FromText("i45WMjQyNlHSUTLUN9Q3MjAyUYrVgYuZYhEzBCo0wRSFqDRFFjNGFjM1M7cAipkjmwgVs8QiZqxvDNUbCwA=", BinaryEncoding.Base64),
Compression.Deflate
)
),
let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [AID = _t, Date = _t]
),

// Convert Date column to date type
ChangedType = Table.TransformColumnTypes(Source, {{"Date", type date}}, "en-US"),

// Add a column to check if the Date is in the future
AddCustomColumn = Table.AddColumn(ChangedType, "IsFuture", each if [Date] > Date.From(DateTime.LocalNow()) then "Yes" else "No"),

// Group the table by AID and IsFuture, compute MaxDate and retain all rows
GroupedRows = Table.Group(AddCustomColumn, {"AID", "IsFuture"}, {{"MaxDate", each List.Max([Date]), type nullable date}, {"AllRows", each _, type table [AID=nullable text, Date=nullable date, IsFuture=text]}}),

// Expand the AllRows table and merge it back
ExpandedRows = Table.ExpandTableColumn(GroupedRows, "AllRows", {"Date"}),

// Add a new column with conditional logic based on IsFuture
AddCustomDate = Table.AddColumn(ExpandedRows, "FinalDate", each if [IsFuture] = "Yes" then [Date] else [MaxDate]),

// Remove duplicate rows based on FinalDate
RemovedDuplicates = Table.Distinct(AddCustomDate, {"FinalDate"}),
#"Removed Other Columns" = Table.SelectColumns(RemovedDuplicates,{"AID", "FinalDate"}),
#"Changed Type" = Table.TransformColumnTypes(#"Removed Other Columns",{{"FinalDate", type date}})
in
#"Changed Type"

 

If this post is useful to help you to solve your issue consider giving the post a thumbs up 

 and accepting it as a solution !

View solution in original post

Omid_Motamedise
Super User
Super User

Hi @jerryr125 

This is solution for your question, just copy it and past it into the advance editor

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQyNlHSUTLUN9Q3MjAyUYrVgYuZYhEzNNQ3wRSEKDRFFjNGFjM1M7cAipkjGwgVs8QiZqxvDNUbCwA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [AID = _t, Date = _t]),
    CHType = Table.TransformColumnTypes(Source,{{"Date", type date}}),
    Table1_AfterToday = Table.SelectRows(CHType, each [Date] > Date.From(DateTime.LocalNow())),
    Table1_LastPreviousdate = Table.Distinct( Table.Buffer(Table.Sort(Table.SelectRows(CHType, each [Date] < Date.From(DateTime.LocalNow())),{"Date",Order.Descending})),"AID"),
    Custom1 = Table1_AfterToday & Table1_LastPreviousdate
in
    Custom1

 

 

If this answer helped resolve your issue, please consider marking it as the accepted answer. And if you found my response helpful, I'd appreciate it if you could give me kudos. 

Thank you!

If my answer helped solve your issue, please consider marking it as the accepted solution. It helps others in the community find answers faster—and keeps the community growing stronger!
You can also check out my YouTube channel for tutorials, tips, and real-world solutions in Power Query with the following link
https://youtube.com/@omidbi?si=96Bo-ZsSwOx0Z36h

View solution in original post

5 REPLIES 5
Anonymous
Not applicable

Hi @jerryr125 ,

Thanks for all the replies! Good answer!
And @jerryr125 , Have you solved your problem? If solved please mark the reply in this post which you think is helpful as a solution to help more others facing the same problem to find a solution quickly, thank you very much!

Best Regards,
Dino Tao

Omid_Motamedise
Super User
Super User

Hi @jerryr125 

This is solution for your question, just copy it and past it into the advance editor

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQyNlHSUTLUN9Q3MjAyUYrVgYuZYhEzNNQ3wRSEKDRFFjNGFjM1M7cAipkjGwgVs8QiZqxvDNUbCwA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [AID = _t, Date = _t]),
    CHType = Table.TransformColumnTypes(Source,{{"Date", type date}}),
    Table1_AfterToday = Table.SelectRows(CHType, each [Date] > Date.From(DateTime.LocalNow())),
    Table1_LastPreviousdate = Table.Distinct( Table.Buffer(Table.Sort(Table.SelectRows(CHType, each [Date] < Date.From(DateTime.LocalNow())),{"Date",Order.Descending})),"AID"),
    Custom1 = Table1_AfterToday & Table1_LastPreviousdate
in
    Custom1

 

 

If this answer helped resolve your issue, please consider marking it as the accepted answer. And if you found my response helpful, I'd appreciate it if you could give me kudos. 

Thank you!

If my answer helped solve your issue, please consider marking it as the accepted solution. It helps others in the community find answers faster—and keeps the community growing stronger!
You can also check out my YouTube channel for tutorials, tips, and real-world solutions in Power Query with the following link
https://youtube.com/@omidbi?si=96Bo-ZsSwOx0Z36h

it will result in 

Omid_Motamedise_0-1731967652989.png

 

If my answer helped solve your issue, please consider marking it as the accepted solution. It helps others in the community find answers faster—and keeps the community growing stronger!
You can also check out my YouTube channel for tutorials, tips, and real-world solutions in Power Query with the following link
https://youtube.com/@omidbi?si=96Bo-ZsSwOx0Z36h
serpiva64
Solution Sage
Solution Sage

Hi,

you can try something like this

let
// Load source data and decompress it
Source = Table.FromRows(
Json.Document(
Binary.Decompress(
Binary.FromText("i45WMjQyNlHSUTLUN9Q3MjAyUYrVgYuZYhEzBCo0wRSFqDRFFjNGFjM1M7cAipkjmwgVs8QiZqxvDNUbCwA=", BinaryEncoding.Base64),
Compression.Deflate
)
),
let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [AID = _t, Date = _t]
),

// Convert Date column to date type
ChangedType = Table.TransformColumnTypes(Source, {{"Date", type date}}, "en-US"),

// Add a column to check if the Date is in the future
AddCustomColumn = Table.AddColumn(ChangedType, "IsFuture", each if [Date] > Date.From(DateTime.LocalNow()) then "Yes" else "No"),

// Group the table by AID and IsFuture, compute MaxDate and retain all rows
GroupedRows = Table.Group(AddCustomColumn, {"AID", "IsFuture"}, {{"MaxDate", each List.Max([Date]), type nullable date}, {"AllRows", each _, type table [AID=nullable text, Date=nullable date, IsFuture=text]}}),

// Expand the AllRows table and merge it back
ExpandedRows = Table.ExpandTableColumn(GroupedRows, "AllRows", {"Date"}),

// Add a new column with conditional logic based on IsFuture
AddCustomDate = Table.AddColumn(ExpandedRows, "FinalDate", each if [IsFuture] = "Yes" then [Date] else [MaxDate]),

// Remove duplicate rows based on FinalDate
RemovedDuplicates = Table.Distinct(AddCustomDate, {"FinalDate"}),
#"Removed Other Columns" = Table.SelectColumns(RemovedDuplicates,{"AID", "FinalDate"}),
#"Changed Type" = Table.TransformColumnTypes(#"Removed Other Columns",{{"FinalDate", type date}})
in
#"Changed Type"

 

If this post is useful to help you to solve your issue consider giving the post a thumbs up 

 and accepting it as a solution !

jgeddes
Super User
Super User

I do not understand what you mean by "keep only the single most last record Max Date".

But the following code will keep records that are greater than today and equal to the max date for all dates less than today.

 Table.SelectRows(PREVIOUSTABLESTEP, each let today = Date.From(DateTime.FixedLocalNow()), lastDate = List.Max(Table.SelectRows(PREVIOUSTABLESTEP, each [Date] < today)[Date]) in [Date] = lastDate or [Date] > today)

Hope this gets you pointed in the right direction.





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!





Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

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.

Top Kudoed Authors