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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
Pbiuserr
Post Prodigy
Post Prodigy

Create new column to flag most recent date for each id/category

Hello, 

Assuming I have a table like down below with ID and LastUpdated columns, I'd like to get a flag to where this particular ID has most recent (up to date) date. Ideally I'd like to have that in PQ. Is it something to achieve?

IDLastUpdatedFlag
ID110.06.2022 
ID111.06.2022 
ID114.06.20221
1 ACCEPTED SOLUTION
v-yiruan-msft
Community Support
Community Support

Hi  @Pbiuserr ,

I created a sample pbix file(see attachment) for you, please check whether that is what you want. You can achieve it in Power Query Editor:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8nQxVNJRMtM3NNA3MjAyUorVQRIzxCJmgixmBBYzxxQyNIOKxQIA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, LastUpdated = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", type text}, {"LastUpdated", type date}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"ID"}, {{"Maxdate", each List.Max([LastUpdated]), type nullable date}, {"Details", each _, type table [ID=nullable text, LastUpdated=nullable date]}}),
    #"Expanded Details" = Table.ExpandTableColumn(#"Grouped Rows", "Details", {"LastUpdated"}, {"LastUpdated"}),
    #"Added Custom" = Table.AddColumn(#"Expanded Details", "Flag", each if [LastUpdated]=[Maxdate] then 1 else null),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Maxdate"})
in
    #"Removed Columns"

yingyinr_0-1655460215353.png

Best Regards

Community Support Team _ Rena
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

2 REPLIES 2
v-yiruan-msft
Community Support
Community Support

Hi  @Pbiuserr ,

I created a sample pbix file(see attachment) for you, please check whether that is what you want. You can achieve it in Power Query Editor:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8nQxVNJRMtM3NNA3MjAyUorVQRIzxCJmgixmBBYzxxQyNIOKxQIA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, LastUpdated = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", type text}, {"LastUpdated", type date}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"ID"}, {{"Maxdate", each List.Max([LastUpdated]), type nullable date}, {"Details", each _, type table [ID=nullable text, LastUpdated=nullable date]}}),
    #"Expanded Details" = Table.ExpandTableColumn(#"Grouped Rows", "Details", {"LastUpdated"}, {"LastUpdated"}),
    #"Added Custom" = Table.AddColumn(#"Expanded Details", "Flag", each if [LastUpdated]=[Maxdate] then 1 else null),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Maxdate"})
in
    #"Removed Columns"

yingyinr_0-1655460215353.png

Best Regards

Community Support Team _ Rena
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
AlexanderPrime
Solution Supplier
Solution Supplier

Assuming your Table is called Table, make a new calculated column for Flag and use the following: 

Flag = 

IF((FORMAT(CALCULATE(MAX('Table'[LastUpdated]), ALLEXCEPT('Table','Table'[ID])),"dd-mm-yyyy")=(FORMAT('Table'[LastUpdated],"dd-mm-yyyy"))),"1","")


If you get a "Expressions that yield variant data-type cannot be used to define calculated columns." error. Change the flag column type to Text. 

If you ever want the column to show the highest date for each ID rather than just a flag, use:
CALCULATE(MAX('Table'[LastUpdated]), ALLEXCEPT('Table','Table'[ID]))

Please note - I am not affiliated with Microsoft, I'm just an end user like yourself.
Just a regular guy doin' Data Science.

If my post has helped you, please don't forget to thumbs up or click "Accept as solution" if it solved your problem!

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.