How to Find the Most Current Date From a Column in Power Query - MAX()

Mike picture Mike · May 22, 2018 · Viewed 12.1k times · Source

This is for a Power Query:

I am working on a report that compiles information from different dates and I need a column that generates the most recent date in the list and the previous date to the most current one in separate columns:

Most Current Date must be the same for the whole column (same for Previous Date Column)

Table Name : Skipped_Issue

Worker  |Case   |Report_Date    |MOST_CURRENT_DATE  |PREVIOUS_DATE
Tran    |3000   |1/2018     
Dhni    |52451  |4/2018     
Dhtuni  |39656  |2/2018 

enter image description here

Answer

Alexis Olson picture Alexis Olson · May 22, 2018

For the most recent date, you can create a custom column with this formula:

= Date.From(List.Max(NameOfPreviousStep[Report_Date]))

Where NameOfPreviousStep references the prior step in your query (e.g. #"Changed Type" or Source).

To get the second to last date, you can create a custom column that evaluates the max after removing the MOST_CURRENT_DATE

= Date.From(
      List.Max(
          List.RemoveItems(#"Added Custom"[Report_Date],
                           #"Added Custom"[MOST_CURRENT_DATE])))

Here's the whole query for the sample data:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCilKzFPSUTI2MDAAUob6hvpGBoYWSrE60UouGXmZQDFTIxNTQyBtgipXUgqWNbY0MzUD0kZw2VgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Worker = _t, Case = _t, Report_Date = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Worker", type text}, {"Case", Int64.Type}, {"Report_Date", type date}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "MOST_CURRENT_DATE", each Date.From(List.Max(Source[Report_Date])), type date),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "PREVIOUS_DATE", each Date.From(List.Max(List.RemoveItems(#"Added Custom"[Report_Date], #"Added Custom"[MOST_CURRENT_DATE]))), type date)
in
    #"Added Custom1"