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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
SadStatue
Helper II
Helper II

retrieve Max value based on other columns

Hi Everybody,

 

I have a table "MainTable" as follow:

IDcompleteNamefromto
8827/06/2019Elly5/09/201912/10/2019
8827/06/2019Ann4/01/201910/10/2019
8827/06/2019Lary1/07/201710/10/2019
2227/06/2019Joe5/09/201912/10/2019
2227/06/2019Henry4/01/201910/06/2019
2227/06/2019Adam1/07/201710/10/2019
531/07/2019Mary5/01/201912/10/2019
531/07/2019Ferry4/01/201910/10/2019
531/07/2019Lucy1/07/201710/10/2019

Each ID has a single Complete Date, but has different name with different From & To dates.

What I want to extract from this table is: Getting the maximum From date for each ID where the Complete date is between From & To date. So the result table should be something like that:

IDCompleteNamefromto
8827/06/2019Ann4/01/201910/10/2019
2227/06/2019Adam1/07/201710/10/2019
531/07/2019Mary5/01/201912/10/2019

I would be glad if anyone can help me deal with the situation whether using DAX or Power querry.

 

Cheers,

1 ACCEPTED SOLUTION
Zubair_Muhammad
Community Champion
Community Champion

Hi @SadStatue 

 

Try this M code.

Please see attached file as well

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WsrBQ0lEKySgtKk5JrNRR8CrNS1UwMtdRMDIwtATKuObkVKIoCE4tKEnNTUotUjCFKwpOLCktAsv6J5fkg+QMjaCSsToErXDMywOSbkWZENnEvNLEokoFE7g8QifcdAPiTfcBGobiRK/SnEoFQ7C8OWHTjYzwmu6Vn0pp6BCwwSM1D+wB3OHjm5+H0EmK4x1TEnMpCBpTYxTL4VrBbsIIdaiziQ4WfKa7pRYRCBMKnO5Tmkx2gokFAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [ID = _t, complete = _t, Name = _t, from = _t, to = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"complete", type date}, {"Name", type text}, {"from", type date}, {"to", type date}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"ID", "complete"}, {{"AllRows", each _, type table [ID=number, complete=date, Name=text, from=date, to=date]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.Max(
    Table.SelectRows([AllRows],(x)=>[complete]>=x[from] and [complete]<=x[to]),
    "from")),
    #"Expanded Custom" = Table.ExpandRecordColumn(#"Added Custom", "Custom", {"Name", "from", "to"}, {"Name", "from", "to"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded Custom",{"AllRows"})
in
    #"Removed Columns"

 

 

View solution in original post

2 REPLIES 2
Zubair_Muhammad
Community Champion
Community Champion

Hi @SadStatue 

 

Try this M code.

Please see attached file as well

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WsrBQ0lEKySgtKk5JrNRR8CrNS1UwMtdRMDIwtATKuObkVKIoCE4tKEnNTUotUjCFKwpOLCktAsv6J5fkg+QMjaCSsToErXDMywOSbkWZENnEvNLEokoFE7g8QifcdAPiTfcBGobiRK/SnEoFQ7C8OWHTjYzwmu6Vn0pp6BCwwSM1D+wB3OHjm5+H0EmK4x1TEnMpCBpTYxTL4VrBbsIIdaiziQ4WfKa7pRYRCBMKnO5Tmkx2gokFAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [ID = _t, complete = _t, Name = _t, from = _t, to = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"complete", type date}, {"Name", type text}, {"from", type date}, {"to", type date}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"ID", "complete"}, {{"AllRows", each _, type table [ID=number, complete=date, Name=text, from=date, to=date]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.Max(
    Table.SelectRows([AllRows],(x)=>[complete]>=x[from] and [complete]<=x[to]),
    "from")),
    #"Expanded Custom" = Table.ExpandRecordColumn(#"Added Custom", "Custom", {"Name", "from", "to"}, {"Name", "from", "to"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded Custom",{"AllRows"})
in
    #"Removed Columns"

 

 

Hi Zubair,

 

Thank you so much for your response. It is working as I wanted.

 

 

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

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.