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
DomDom78
Regular Visitor

Max Date across columns but return the column name not the Date

Hello, 

 

As the title suggests, in Power Query, is there a way to identify the max date across multiple coloums but return the column name the max date is within, instead of the date? e.g 

 

DomDom78_0-1675877968166.png

 

I'm happy finding the max date using List.Max, but i don't know how to return the colmun name.

 

If there are mutiple columns with the same date could column names be returned seperated by a comma?

 

Thanks in advance

 

DomDom

1 ACCEPTED SOLUTION
AlexisOlson
Super User
Super User

Find the positions of the maximal value and then take those positions from the list of column names.

 

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTICYmMgNgFiUyA2UIrVwZQxBvNBMqZIIjA9JkqxsQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t, Column4 = _t, Column5 = _t, Column6 = _t]), 
  #"Added Custom" = Table.AddColumn(
    Source, 
    "Max", 
    each 
      let
        row_values    = Record.ToList(_), 
        column_names  = Record.FieldNames(_), 
        max_value     = List.Max(row_values), 
        max_positions = List.PositionOf(row_values, max_value, 6), 
        max_names     = List.Transform(max_positions, each column_names{_}), 
        combine_names = Text.Combine(max_names, ", ")
      in
        combine_names, 
    type text
  )
in
  #"Added Custom"

 

If you don't like all the intermediate variables, you can put this in the custom column box:

Text.Combine(
    List.Transform(
        List.PositionOf(
            Record.ToList(_),
            List.Max(Record.ToList(_)),
            Record.FieldCount(_)
        ), 
        (pos) => Record.FieldNames(_){pos}
    ),
    ", "
)

AlexisOlson_0-1675881845682.png

 

View solution in original post

5 REPLIES 5
DomDom78
Regular Visitor

Thankyou AlexisOlson, it works perfectly. 

AlexisOlson
Super User
Super User

Find the positions of the maximal value and then take those positions from the list of column names.

 

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTICYmMgNgFiUyA2UIrVwZQxBvNBMqZIIjA9JkqxsQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t, Column4 = _t, Column5 = _t, Column6 = _t]), 
  #"Added Custom" = Table.AddColumn(
    Source, 
    "Max", 
    each 
      let
        row_values    = Record.ToList(_), 
        column_names  = Record.FieldNames(_), 
        max_value     = List.Max(row_values), 
        max_positions = List.PositionOf(row_values, max_value, 6), 
        max_names     = List.Transform(max_positions, each column_names{_}), 
        combine_names = Text.Combine(max_names, ", ")
      in
        combine_names, 
    type text
  )
in
  #"Added Custom"

 

If you don't like all the intermediate variables, you can put this in the custom column box:

Text.Combine(
    List.Transform(
        List.PositionOf(
            Record.ToList(_),
            List.Max(Record.ToList(_)),
            Record.FieldCount(_)
        ), 
        (pos) => Record.FieldNames(_){pos}
    ),
    ", "
)

AlexisOlson_0-1675881845682.png

 

Thank you for your solution! May I ask how to limit the selection to about 5 (out of 20) columns in my table only please? And if you know of a similar solution but with DAX? 

Could you please explain how this works? I mean where are the column names specified in this code?

Each row is a record and Record.FieldNames is the list of column names for that row. The code finds the positions of the maximal values in the list of values and then applies those positions to the list of column names.

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.

Top Solution Authors