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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
TTS
Frequent Visitor

Latest value by key column

Tried to go through community cases and other help platforms but group by with fill down did not solve the issue.

The data I am receiving from source system:

TTS_0-1716964233271.png

- ID's can get values or nulls for certain columns by time

What is needed for reporting:

TTS_1-1716964326068.png

- Latest non-null value by ID and column - of course null if there is no value at any point in time.





 

2 ACCEPTED SOLUTIONS
AlienSx
Super User
Super User

let
    Source = your_table,
    names = List.Buffer(List.Skip(Table.ColumnNames(Source), 2)),
    f = (tbl) => 
        [sorted = List.Skip(Table.ToColumns(Table.Sort(tbl, "Date")), 2),
        rec = Record.FromList(
            List.Transform(sorted, (x) => try List.Last(List.RemoveNulls(x)) otherwise null),
            names
        )][rec],
    group = Table.Group(Source, "ID", {"x", f}),
    xp = Table.ExpandRecordColumn(group, "x", names)
in
    xp

View solution in original post

dufoq3
Super User
Super User

Hi @TTS, different approach here:

 

Enter column names you want to calculated MAX here:

dufoq3_1-1717169312990.png

 

Result

dufoq3_0-1717169231167.png

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTIyMDLRNTAEIiAnLDGnNBWDjtWJVjLCpRSEQAqM0RVAEMIEZMuMDGBSRkhGGKGrQFYEZaBbhVNdLAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Date = _t, Column1 = _t, Column2 = _t, Column3 = _t]),
    // You can probably delete this step.
    ReplaceBlankToNull = Table.TransformColumns(Source, {}, each if _ = "" then null else _),
    ChangedType = Table.TransformColumnTypes(ReplaceBlankToNull,{{"ID", Int64.Type}, {"Date", type date}}),
    __EnterColumnsHere__ = {"Column1", "Column2", "Column3"},
    GroupedRows = Table.Group(ChangedType, {"ID"}, {{"MaxValue", each 
        [ a = List.Transform(__EnterColumnsHere__, (x)=> List.Max(Table.Column(_, x))),
          b = Table.FromRows({ {[ID]{0}} & a }, {"ID"} & __EnterColumnsHere__)
        ][b], type table}}),
    CombinedMaxValue = Table.Combine(GroupedRows[MaxValue])
in
    CombinedMaxValue

 


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

View solution in original post

7 REPLIES 7
dufoq3
Super User
Super User

Hi @TTS, different approach here:

 

Enter column names you want to calculated MAX here:

dufoq3_1-1717169312990.png

 

Result

dufoq3_0-1717169231167.png

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTIyMDLRNTAEIiAnLDGnNBWDjtWJVjLCpRSEQAqM0RVAEMIEZMuMDGBSRkhGGKGrQFYEZaBbhVNdLAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Date = _t, Column1 = _t, Column2 = _t, Column3 = _t]),
    // You can probably delete this step.
    ReplaceBlankToNull = Table.TransformColumns(Source, {}, each if _ = "" then null else _),
    ChangedType = Table.TransformColumnTypes(ReplaceBlankToNull,{{"ID", Int64.Type}, {"Date", type date}}),
    __EnterColumnsHere__ = {"Column1", "Column2", "Column3"},
    GroupedRows = Table.Group(ChangedType, {"ID"}, {{"MaxValue", each 
        [ a = List.Transform(__EnterColumnsHere__, (x)=> List.Max(Table.Column(_, x))),
          b = Table.FromRows({ {[ID]{0}} & a }, {"ID"} & __EnterColumnsHere__)
        ][b], type table}}),
    CombinedMaxValue = Table.Combine(GroupedRows[MaxValue])
in
    CombinedMaxValue

 


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

AlienSx
Super User
Super User

let
    Source = your_table,
    names = List.Buffer(List.Skip(Table.ColumnNames(Source), 2)),
    f = (tbl) => 
        [sorted = List.Skip(Table.ToColumns(Table.Sort(tbl, "Date")), 2),
        rec = Record.FromList(
            List.Transform(sorted, (x) => try List.Last(List.RemoveNulls(x)) otherwise null),
            names
        )][rec],
    group = Table.Group(Source, "ID", {"x", f}),
    xp = Table.ExpandRecordColumn(group, "x", names)
in
    xp
TTS
Frequent Visitor

Thanks for this input!
Actually I got it working now!!! Great thanks!

TTS
Frequent Visitor

Managed to do almost the same like this:

TTS_1-1716969482553.png

But same "issue" still to be solved; I just get the lates records; and in the case that the newest has null for certain column and there is a value in the history I would like to get that to same record.
By the way - I don't need the date for reporting purposes at all...so that's not needed for the end result.

 

Ok, I see. Sorry, didn't read the requirements properly first time.

Can you provide a copyable example of your data in its 'before' state please?

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




TTS
Frequent Visitor

Thanks! But still some issues... 🙂
I did as You mentioned; and everything seems to go without errors.

TTS_0-1716969043206.png

But how to get from here a single record that has values:
Column1 = New Contract. Valid from 1.5.2024
Column2 = 30 days net from invoice date
Column3 = Only EXW in use for 2024

BA_Pete
Super User
Super User

Hi @TTS ,

 

If you can provide a copyable example of your data I can provide you the exact code for your scenario but, in the absence of that, the process you want to follow will be as follows:

-1- Group By [ID]. Use the 'All Rows' operator for the aggregate column. I'll assume you've called the aggregate column [data].

-2- Add a new custom column and use the following code for the calculation:

Table.Max([data], "Date")

-3- Expand the resulting nested record column reinstating whichever columns you need back to the table.

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

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