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

To celebrate FabCon Vienna, we are offering 50% off select exams. Ends October 3rd. Request your discount now.

Reply
fabric-excel
Regular Visitor

Second Max Date based on the current Row Date

Hello,

 

I'm current using M Language from Power Query to achieve this result (I developed already this code with DAX for Power BI and also with Excel formulas, but I need to create that logic with Power Query this time). The idea is, I need to get always the second max date based on the current row date.

 

Here below I show an example from what I'm expecting to get. I already have the date column and the idea is to create an "Previous" column like this.

 

fabricexcel_0-1737727024038.png

 

Thanks in advance for all help.

1 ACCEPTED SOLUTION
dufoq3
Super User
Super User

Hi @fabric-excel, check this:

 

Output

dufoq3_0-1737728398873.png

let
    Source = Table.FromList({#date(2020,3,1), #date(2020,3,7), #date(2020,3,5), #date(2020,10,8), #date(2019,11,30), #date(2020,3,1)}, Splitter.SplitByNothing(), type table[Date=date]),
    Dates = List.Buffer(List.Sort(Source[Date])),
    StepBack = Source,
    Ad_Previous = Table.AddColumn(StepBack, "Previous", each try Dates{List.PositionOf(Dates, [Date])-1} otherwise null, type date)
in
    Ad_Previous

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

3 REPLIES 3
rohit1991
Super User
Super User

Hi @fabric-excel ,

 

This comes up a lot in Power Query when you need the "previous" or "second max" date for each row.

You actually have a couple of good options:

 

1. The quick way: Using List.PositionOf in a sorted date list: If you just need the previous date in a column, this is super concise and efficient:

let
    Source = YourTableName,
    // Make sure your date column is type date!
    Dates = List.Sort(Source[Date]),
    AddPrevious = Table.AddColumn(
        Source,
        "Previous",
        each try Dates{List.PositionOf(Dates, [Date]) - 1} otherwise null,
        type date
    )
in
    AddPrevious

 

2. Flexible way: Filtering and using List.Max If you ever need more filtering (like by group, or other conditions), you can go with this:

let
    Source = YourTableName,
    Sorted = Table.Sort(Source, {{"Date", Order.Ascending}}),
    AddPrevious = Table.AddColumn(
        Sorted,
        "Previous",
        (current) =>
            let
                earlierDates = List.Select(Sorted[Date], (d) => d < current[Date]),
                prevDate = if List.Count(earlierDates) = 0 then null else List.Max(earlierDates)
            in
                prevDate,
        type nullable date
    )
in
    AddPrevious

 

Pro tip: If you want the “nth previous” date, just tweak the index: Dates{List.PositionOf(Dates, [Date]) - n}

 


Did it work? ✔ Give a Kudo • Mark as Solution – help others too!
v-ssriganesh
Community Support
Community Support

Hi @fabric-excel,

In addition to the helpful response provided by @dufoq3, could you please confirm if your query have been resolved? If so, please accept it as a solution and leave a "Kudos" so other members can find it more easily.

Thank you.

 

dufoq3
Super User
Super User

Hi @fabric-excel, check this:

 

Output

dufoq3_0-1737728398873.png

let
    Source = Table.FromList({#date(2020,3,1), #date(2020,3,7), #date(2020,3,5), #date(2020,10,8), #date(2019,11,30), #date(2020,3,1)}, Splitter.SplitByNothing(), type table[Date=date]),
    Dates = List.Buffer(List.Sort(Source[Date])),
    StepBack = Source,
    Ad_Previous = Table.AddColumn(StepBack, "Previous", each try Dates{List.PositionOf(Dates, [Date])-1} otherwise null, type date)
in
    Ad_Previous

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.

August 2025 community update carousel

Fabric Community Update - August 2025

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