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

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes! Register now.

Reply
juan-quinones
New Member

Find the max date that is less than a specific date, considering a grouping factor (Power Query)

Hi,

 

I have the following table in Power Query:

 

juanquinones_0-1711118137059.png

 

I need to add a column that follows the following logic: if Date1 = Date3 then Date1, otherwise find the maximum date in Date3 that is less than the Date1 of each row, considering the group ID. Is it possible to do this in Power Query?

 

Thanks

1 ACCEPTED SOLUTION
dufoq3
Super User
Super User

Hi @juan-quinones,

 

Result

dufoq3_0-1711312359570.png

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("TcvBDQAhCETRXjibiCCarcXQfxvL6GUS5vJfOEd09DpTm9JkYNo1EEyylRu51Shcd3LHf/lG8OeTHKNwPcgD/+UfwpLMHw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date1 = _t, ID = _t, Date3 = _t]),
    ChangedType = Table.TransformColumnTypes(Source,{{"Date1", type date}, {"Date3", type date}}),
    Ad_Custom = Table.AddColumn(ChangedType, "Custom", each if [Date1] = [Date3] then [Date1] else List.Max(Table.SelectRows(ChangedType, (x)=> x[Date3] < [Date1])[Date3]), type date)
in
    Ad_Custom

 


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

View solution in original post

1 REPLY 1
dufoq3
Super User
Super User

Hi @juan-quinones,

 

Result

dufoq3_0-1711312359570.png

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("TcvBDQAhCETRXjibiCCarcXQfxvL6GUS5vJfOEd09DpTm9JkYNo1EEyylRu51Shcd3LHf/lG8OeTHKNwPcgD/+UfwpLMHw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date1 = _t, ID = _t, Date3 = _t]),
    ChangedType = Table.TransformColumnTypes(Source,{{"Date1", type date}, {"Date3", type date}}),
    Ad_Custom = Table.AddColumn(ChangedType, "Custom", each if [Date1] = [Date3] then [Date1] else List.Max(Table.SelectRows(ChangedType, (x)=> x[Date3] < [Date1])[Date3]), type date)
in
    Ad_Custom

 


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

Helpful resources

Announcements
September Power BI Update Carousel

Power BI Monthly Update - September 2025

Check out the September 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