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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
third_hicana
Helper IV
Helper IV

Get the next latest Date after the Max Date in Power Query

Hi. Asking for you help how can I create a custom column in power query where it will get the next latest date after the max date in a Date column. For example, in the column below the max date is 17/04/2024 then the next latest date is 27/03/2024. I want to get the 27/03/2024 in a created column. 

third_hicana_0-1722508782619.png

 

Thank you very much for your help.

 

-Third

1 ACCEPTED SOLUTION
bhanu_gautam
Super User
Super User

@third_hicana ,Go to the Add Column tab.
Click on Custom Column.
Use the following formula to get the next latest date:

let
    // Load your data
    Source = YourDataSource,
   
    // Sort the date column in descending order
    SortedTable = Table.Sort(Source,{{"Date", Order.Descending}}),
   
    // Add an index column
    IndexedTable = Table.AddIndexColumn(SortedTable, "Index", 1, 1, Int64.Type),
   
    // Get the max date
    MaxDate = List.Max(IndexedTable[Date]),
   
    // Filter out the max date
    FilteredTable = Table.SelectRows(IndexedTable, each [Date] <> MaxDate),
   
    // Get the next latest date
    NextLatestDate = FilteredTable{0}[Date],
   
    // Add a custom column with the next latest date
    AddedCustomColumn = Table.AddColumn(IndexedTable, "NextLatestDate", each if [Date] = MaxDate then NextLatestDate else null)
in
    AddedCustomColumn



Did I answer your question? Mark my post as a solution! And Kudos are appreciated

Proud to be a Super User!




LinkedIn






View solution in original post

2 REPLIES 2
third_hicana
Helper IV
Helper IV

@bhanu_gautam  Than you very much for your help.

bhanu_gautam
Super User
Super User

@third_hicana ,Go to the Add Column tab.
Click on Custom Column.
Use the following formula to get the next latest date:

let
    // Load your data
    Source = YourDataSource,
   
    // Sort the date column in descending order
    SortedTable = Table.Sort(Source,{{"Date", Order.Descending}}),
   
    // Add an index column
    IndexedTable = Table.AddIndexColumn(SortedTable, "Index", 1, 1, Int64.Type),
   
    // Get the max date
    MaxDate = List.Max(IndexedTable[Date]),
   
    // Filter out the max date
    FilteredTable = Table.SelectRows(IndexedTable, each [Date] <> MaxDate),
   
    // Get the next latest date
    NextLatestDate = FilteredTable{0}[Date],
   
    // Add a custom column with the next latest date
    AddedCustomColumn = Table.AddColumn(IndexedTable, "NextLatestDate", each if [Date] = MaxDate then NextLatestDate else null)
in
    AddedCustomColumn



Did I answer your question? Mark my post as a solution! And Kudos are appreciated

Proud to be a Super User!




LinkedIn






Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

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