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

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

Reply
Anonymous
Not applicable

How to group by ID and max of column and get another column value

I have a table with multiple columns. One column has ID which has duplicate values for different dates and days.

IDDateDayTitle
12310/11/2022Tuesday

XYZ

11112/11/2022TuesdayXYZ
12309/11/2022MondayXYZ
12311/11/2022FridayZXK

 

I want to get a table where I have 1 row for each unique ID, Max of date for that ID and corresponding Day and Title. For now, I am able to group by ID and select Max of Date, but when I select Day, it's giving duplicate rows. I have got the expected outputs below. Please help.

IDDateDayTitle
12311/11/2022FridayZXK
11112/11/2022TuesdayXYZ

 

2 REPLIES 2
WanderingBI
Resolver III
Resolver III

Hi, 

 

the first step is to find the maximum date by grouping ID by Max[Date]:

WanderingBI_1-1719250564123.png

 

You can then use this table to filter down your source table.

Two solutions:

Solution 1

Easier solution where you just use the GUI.

1. Create a new query called FilterTable where you group the original table by ID using Max of Date. Rename the column with the date to "MaxDate".

2. Then merge the filterTable Query with the original Query on "ID" "Date"/"MaxDate" and select "Inner" (only keep matching rows).

 

This will yield the two rows that you are looking for.

WanderingBI_1-1719259296099.png

You can open the sample Power BI file here:

https://1drv.ms/u/s!AjX6-gq4HfZQg_9-Hi25c1e4QqO-Nw?e=UBCwvM 

 

Solution 2

This method is to  first create a filter table that has the highest date for each ID by grouping by max date.

Then insert custom columns in the original table to do some filtering. In the first column I insert the filterTable into each row.

 

You can copy my example code into the advanced query editor and follow the transformation steps:

 

 

 

 

let
// Input table
  Source = Table.FromRows(
    Json.Document(
      Binary.Decompress(
        Binary.FromText("i45WMjQyVtJRMjTQNzTUNzIwMgJyQkpTi1MSK4GsiMgopVgdoCJDQ5AiI/yKwCYZWCIp8s3Pw6YGqAChxq0oE6ImKsKbODWxAA==", BinaryEncoding.Base64), 
        Compression.Deflate
      )
    ), 
    let
      _t = ((type nullable text) meta [Serialized.Text = true])
    in
      type table [ID = _t, Date = _t, Day = _t, Title = _t]
  ), 

  // Real code starts here
  removeDuplicates = Table.Distinct(Source), 
  tableToFilter = Table.TransformColumnTypes(removeDuplicates, {{"ID", Int64.Type}, {"Date", type date}, {"Day", type text}, {"Title", type text}}), 
  
  // Branch off with a filter Table
  filterTable = Table.Group(tableToFilter, {"ID"}, {{"MaxDate", each List.Max([Date]), type nullable date}}), 
  
  filterColumn = Table.AddColumn(tableToFilter, "Filter Column", (outerRow) => Table.SelectRows(filterTable, (innerRow) => innerRow[ID] = outerRow[ID])), 
  matchIDandHighestDate = Table.AddColumn(
    filterColumn, 
    "isHighest", 
    each if List.Contains(filterTable[ID], [ID]) and List.Contains(filterTable[MaxDate], [Date]) then true else false
  ), 
  onlyKeepHighest = Table.SelectRows(matchIDandHighestDate, each ([isHighest] = true))
in
  onlyKeepHighest

 

 

 

WanderingBI_0-1719258733150.png

 

 

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!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 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.

Top Kudoed Authors