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

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply
Anonymous
Not applicable

Power Query - Add column if max date by id

Hello,

 

I have a set of records into a table and want to add the "ismax" column in powerquery.

 

DateCP_IDP_NAMEismax
01.01.20221Test1n
05.01.20221Test1y
06.01.20222Test2y

 

How can I achieve this ? 

 

I ahve a working solution in DAX but not with Power Query.

 

Thank you.

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @Anonymous ,

 

Please try this custom column:

if [DateC] = List.Max(Table.SelectRows(#"Previous step name",(x)=>x[P_ID]=[P_ID] and x[P_NAME] = [P_NAME])[DateC]) then "y" else "n"

vcgaomsft_0-1675923923146.png

all steps:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtQ31DcyMDJS0lEyBOKQ1OISQ6VYnWglU1wSZggJI6iEkVJsLAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [DateC = _t, P_ID = _t, P_NAME = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"DateC", type date}, {"P_ID", Int64.Type}, {"P_NAME", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "ismax
", each if [DateC] = List.Max(Table.SelectRows(#"Changed Type",(x)=>x[P_ID]=[P_ID] and x[P_NAME] = [P_NAME])[DateC]) then "y" else "n")
in
    #"Added Custom"

If I misunderstand your needs, please tell me what your expected output is and how it is calculated.

Best Regards,
Gao

Community Support Team

 

If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly. If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

How to get your questions answered quickly --  How to provide sample data in the Power BI Forum

View solution in original post

2 REPLIES 2
Anonymous
Not applicable

Hi @Anonymous ,

 

Please try this custom column:

if [DateC] = List.Max(Table.SelectRows(#"Previous step name",(x)=>x[P_ID]=[P_ID] and x[P_NAME] = [P_NAME])[DateC]) then "y" else "n"

vcgaomsft_0-1675923923146.png

all steps:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtQ31DcyMDJS0lEyBOKQ1OISQ6VYnWglU1wSZggJI6iEkVJsLAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [DateC = _t, P_ID = _t, P_NAME = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"DateC", type date}, {"P_ID", Int64.Type}, {"P_NAME", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "ismax
", each if [DateC] = List.Max(Table.SelectRows(#"Changed Type",(x)=>x[P_ID]=[P_ID] and x[P_NAME] = [P_NAME])[DateC]) then "y" else "n")
in
    #"Added Custom"

If I misunderstand your needs, please tell me what your expected output is and how it is calculated.

Best Regards,
Gao

Community Support Team

 

If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly. If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

How to get your questions answered quickly --  How to provide sample data in the Power BI Forum

Anonymous
Not applicable

Hello,

let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"DateC", type date}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"P_ID"}, {{"Count", each _, type table [DateC=nullable date, P_ID=number, P_NAME=text]}}),
custom_function = (check_max_id)=> Table.AddColumn(check_max_id, "ismax", each if [DateC] = List.Max(check_max_id[DateC]) then "y" else "n"),
call_custom_function = Table.AddColumn(#"Grouped Rows", "Custom", each custom_function([Count])),
#"Removed Other Columns" = Table.SelectColumns(call_custom_function,{"Custom"}),
#"Expanded Custom" = Table.ExpandTableColumn(#"Removed Other Columns", "Custom", {"DateC", "P_ID", "P_NAME", "ismax"}, {"DateC", "P_ID", "P_NAME", "ismax"})
in
#"Expanded Custom"

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

60 days of Data Days Carousel

Data Days 2026

Join Fabric Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.