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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch 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
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.