How to return cell header with latest date

Sian1

Board Regular
Joined
Nov 9, 2009
Messages
90
Hi, i have 4 columns with various date on it, how could i write a formula to return the header named based on the latest date

ABCDReturn Value
1/20/20201/20/20201/23/2020D
7/11/20195/28/20198/6/20208/10/2020D
7/5/20216/23/20216/23/2021B
7/5/20211/14/20211/14/20211/15/2021A
8/10/20201/3/20201/6/2020B
10/10/20196/23/202110/27/2020C
 
No you would have to modify it. Something like

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    ChangeType = Table.TransformColumnTypes(Source,{{"A", type date}, {"B", type date}, {"C", type date}, {"D", type date}}),
    tbl = Table.SelectColumns(ChangeType, List.RemoveItems(Table.ColumnNames(ChangeType), {"ID"})),
    AddCol = Table.AddColumn(tbl, "Latest Date", each Table.FromColumns({Table.ColumnNames(tbl),Record.ToList(_)})),
    TransformCol = Table.TransformColumns(AddCol, {"Latest Date", each Text.Combine(Table.SelectRows(_, (x)=> x[Column2] = List.Max(_[Column2]))[Column1],", ")}),
    Result = Table.FromColumns({ChangeType[ID]} & Table.ToColumns(TransformCol), {"ID"} & Table.ColumnNames(TransformCol))
in
    Result
 
Upvote 0

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
thanks it works, however if all items blank, it still comeback with details. Do you know how to solve it ?
 
Upvote 0
maybe

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    ChangeType = Table.TransformColumnTypes(Source,{{"A", type date}, {"B", type date}, {"C", type date}, {"D", type date}}),
    tbl = Table.SelectColumns(ChangeType, List.RemoveItems(Table.ColumnNames(ChangeType), {"ID"})),
    AddCol = Table.AddColumn(tbl, "Latest Date", each Table.FromColumns({Table.ColumnNames(tbl),Record.ToList(_)})),
    TransformCol = Table.TransformColumns(AddCol, {"Latest Date", each 
        if List.Count(List.RemoveNulls(_[Column2]))>0 then 
            Text.Combine(Table.SelectRows(_, (x)=> x[Column2] = List.Max(_[Column2]))[Column1],", ") 
        else 
            null})
in
    TransformCol
 
Upvote 0
i added the "else null" statement it didn't come out correctly. the reasons probably because i have a lot more fields before these 4 particulars field. I created a table in within the table here is my query. how can I modify item 11?
 
Upvote 0
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    ChangeType = Table.TransformColumnTypes(Source,{{"A", type date}, {"B", type date}, {"C", type date}, {"D", type date}}),
    AddCol = Table.AddColumn(ChangeType, "Latest Date", each Table.FromColumns({Table.ColumnNames(ChangeType),Record.ToList(_)})),
    TransformCol = Table.TransformColumns(AddCol, {"Latest Date", each Text.Combine(Table.SelectRows(_, (x)=> x[Column2] = List.Max(_[Column2]))[Column1],", ")})
in
    TransformCol

Book1
ABCDEFGHIJK
1ABCDABCDLatest Date
21/20/20201/20/20201/23/20201/20/20201/20/20201/23/2020D
37/11/20195/28/20198/6/20208/10/20207/11/20195/28/20198/6/20208/10/2020D
47/5/20216/23/20216/23/20217/5/20216/23/20216/23/2021B
57/5/20211/14/20211/14/20211/15/20217/5/20211/14/20211/14/20211/15/2021A
68/10/20201/3/20201/6/20208/10/20201/3/20201/6/2020B
76/23/202110/10/20196/23/202110/27/20206/23/202110/10/20196/23/202110/27/2020A, C
8
Sheet1
Very nice. I especially like that there are no SPACES in your step names. Keeps the code so much cleaner!
 
Upvote 0
Alternative (not necessarily better, maybe easier to understand without any "(x)=>" function construction).
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="data"]}[Content],
    Cols = List.Buffer(Table.ColumnNames(Source)),
    Set_as_dates = Table.TransformColumnTypes(Source,List.Transform(Cols, each {_, type date})),
    Find_header_for_max_date = Table.AddColumn(Set_as_dates, "Max Header", each 
    let 
        RecordList = Record.ToList(_),
        Max = List.Max(RecordList),
        PosOfMax = List.PositionOf(RecordList, Max),
        Header = if Max = null then null else List.Range(Cols, PosOfMax,1){0}
    in 
        Header)
in
    Find_header_for_max_date
 
Upvote 0

Forum statistics

Threads
1,223,401
Messages
6,171,896
Members
452,431
Latest member
TiffanyMcllwain

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top