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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Select the latest data based on the date column

Is there a way to filter the data based on the date column. For example -

 

ProductCostDate
131/4/2023
1412/5/2022
1512/20/2022

 

From the above table, I only need 1st row for product 1 which has the latest date and it would be the same for other products.

 

ProductCostDate
131/4/2023
1 ACCEPTED SOLUTION
BA_Pete
Super User
Super User

Hi @Anonymous ,

 

Select your [Product] column and go to the Home tab > Group By.

Call the aggregated column 'data' and use the All Rows operator.

Now add a new custom column with the following code:

 

Table.Max([data], "Date")

 

 

This will give you a nested RECORD column that you can expand to reinstate the columns you need from the row that has the max date per product.

 

Working example code:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Vc1BCkAhCIThu7gOdMaC3lmi+1/jKUHh4t98jLiWQJp4ZF0NSqPLbod78lAwmZdHRCvMoJlraBwU/iJMBSrDcu7v5/4B", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Product = _t, Cost = _t, Date = _t]),
    chgTypes = Table.TransformColumnTypes(Source,{{"Product", Int64.Type}, {"Cost", Int64.Type}, {"Date", type date}}),
    groupProduct = Table.Group(chgTypes, {"Product"}, {{"data", each _, type table [Product=nullable number, Cost=nullable number, Date=nullable date]}}),
    addMaxRecord = Table.AddColumn(groupProduct, "maxRecord", each Table.Max([data], "Date")),
    expandMaxRecord = Table.ExpandRecordColumn(addMaxRecord, "maxRecord", {"Cost", "Date"}, {"Cost", "Date"})
in
    expandMaxRecord

 

Example output:

BA_Pete_0-1672821173080.png

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




View solution in original post

6 REPLIES 6
watkinnc
Super User
Super User

That's what I said! 🙂


I’m usually answering from my phone, which means the results are visualized only in my mind. You’ll need to use my answer to know that it works—but it will work!!
AlexisOlson
Super User
Super User

There are lots of ways to do this. I've written up a few of them here:
https://community.powerbi.com/t5/Community-Blog/Select-Distinct-Rows-Ordered-by-Another-Column-Power...

watkinnc
Super User
Super User

Even easier to group by and add the max aggregation to the date column in the same GUI dialog.

 

--Nate


I’m usually answering from my phone, which means the results are visualized only in my mind. You’ll need to use my answer to know that it works—but it will work!!

 

Except that it doesn't guarantee that you get the correct [cost] value associated with the latest [Date] row.

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




ouaelaam
Resolver I
Resolver I

Hi,

This post explains how to do it for Decimal value, you can follow the same approach for Dates :
https://gorilla.bi/power-query/using-table-max/

 

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

BA_Pete
Super User
Super User

Hi @Anonymous ,

 

Select your [Product] column and go to the Home tab > Group By.

Call the aggregated column 'data' and use the All Rows operator.

Now add a new custom column with the following code:

 

Table.Max([data], "Date")

 

 

This will give you a nested RECORD column that you can expand to reinstate the columns you need from the row that has the max date per product.

 

Working example code:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Vc1BCkAhCIThu7gOdMaC3lmi+1/jKUHh4t98jLiWQJp4ZF0NSqPLbod78lAwmZdHRCvMoJlraBwU/iJMBSrDcu7v5/4B", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Product = _t, Cost = _t, Date = _t]),
    chgTypes = Table.TransformColumnTypes(Source,{{"Product", Int64.Type}, {"Cost", Int64.Type}, {"Date", type date}}),
    groupProduct = Table.Group(chgTypes, {"Product"}, {{"data", each _, type table [Product=nullable number, Cost=nullable number, Date=nullable date]}}),
    addMaxRecord = Table.AddColumn(groupProduct, "maxRecord", each Table.Max([data], "Date")),
    expandMaxRecord = Table.ExpandRecordColumn(addMaxRecord, "maxRecord", {"Cost", "Date"}, {"Cost", "Date"})
in
    expandMaxRecord

 

Example output:

BA_Pete_0-1672821173080.png

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors
Top Kudoed Authors