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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
Harvey85
Helper I
Helper I

Bring Value for Max Date by Key in Power Query

Hello,

I have the following table in Power Query:

 

Inspections:

Key Date Value
AA 03/20/2024 100
AA 02/15/2024 80
AA 02/27/2024 50
BB 02/27/2024 50
BB 02/15/2024 80
CC 03/20/2024 35

 

What I need is to get in Power Query, for each Key, the Value for the max Date:

 

Expected Output:

Key Date Value
AA 03/20/2024 100
BB 02/27/2024 50
CC 03/20/2024 35

 

Is there any simple way to do that using Power Query, preferibly by Drag & Drop or any button in Power Query?

 

I've tried the Group by button but I think it doesn't work in this case:

Harvey85_0-1711558427836.png

Or maybe just group by Key and Max(Date)... and then Join the original table by Key+Date and add the field Value? Or is there any simplier way to do that?

 

Thank you!!!

1 ACCEPTED SOLUTION

Take a look at this gif:
MaxDate.gif
The most complicated part is the merge stage. You have to merge with the table itself, but in the ChangeType step. To choose more than one column to link the two tables, you have to use ctrl + left click in the same order in both tables, as I did in the gif.





Did I answer your question? Mark my post as a solution! Kudos are welcome.

Proud to be a Super User!




View solution in original post

5 REPLIES 5
Harvey85
Helper I
Helper I

Hi @_AAndrade do you have the PBIX or screen capture from steps  "GroupByMaxDate" and "Merge"? Because I have a spanish installation of Power BI and I'm struggling finding the same buttons you used to build the solution. Thank you!!!

Take a look at this gif:
MaxDate.gif
The most complicated part is the merge stage. You have to merge with the table itself, but in the ChangeType step. To choose more than one column to link the two tables, you have to use ctrl + left click in the same order in both tables, as I did in the gif.





Did I answer your question? Mark my post as a solution! Kudos are welcome.

Proud to be a Super User!




Tom_Y
Helper III
Helper III

How about something low code?

Click and sort "Value", decending, and then sort "Key", you can see the small numbers, see that line of code, even it's by clicking.

and then click key, and remove duplicate

Tom_Y_0-1711563266543.png

Tom_Y_1-1711563340534.png

 

Ahmedx
Super User
Super User

pls try this code

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcnRU0lEyMNY3MgAiIxMgx9DAQClWByZjpG9oCpOxQJUwModJmEIknJwISGAY5eyMbruxqVJsLAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Key = _t, Date = _t, Value = _t]),
    ChangeType = Table.TransformColumnTypes(Source,{{"Key", type text}, {"Date", type text}, {"Value", Int64.Type}}),
    #"Grouped Rows" = Table.Group(ChangeType, {"Key"}, {{"Count", (x)=> Table.FromRecords({ Table.Max(x,"Date")})}}),
    #"Removed Other Columns" = Table.SelectColumns(#"Grouped Rows",{"Count"}),
    #"Expanded Count" = Table.ExpandTableColumn(#"Removed Other Columns", "Count", {"Key", "Date", "Value"}, {"Key", "Date", "Value"})
in
    #"Expanded Count"

Screenshot_3.png

_AAndrade
Super User
Super User

Hi,

Please try this code:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcnRU0lEyMNY3MgAiIxMgx9DAQClWByZjpG9oCpOxQJUwModJmEIknJwISGAY5eyMbruxqVJsLAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Key = _t, Date = _t, Value = _t]),
    ChangeType = Table.TransformColumnTypes(Source,{{"Key", type text}, {"Date", type text}, {"Value", Int64.Type}}),
    GroupByMaxDate = Table.Group(ChangeType, {"Key"}, {{"MaxDate", each List.Max([Date]), type nullable text}}),
    Merge = Table.NestedJoin(GroupByMaxDate, {"Key", "MaxDate"}, ChangeType, {"Key", "Date"}, "Merged", JoinKind.Inner),
    ExpandValue = Table.ExpandTableColumn(Merge, "Merged", {"Value"}, {"Value"})
in
    ExpandValue


Change the Source to your own data and the final output should be this:

_AAndrade_0-1711559666052.png

 





Did I answer your question? Mark my post as a solution! Kudos are welcome.

Proud to be a Super User!




Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

April Fabric Community Update

Fabric Community Update - April 2024

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