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

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
SafeAnalyst
Frequent Visitor

How to Lookup value from latest date for specific value

Hi.

 

I have the following dataset where i would like to locate the 'Item Legder Entry Type' for each Item No based on the last posting date. I have the column 'Last Posting Date' and i would like to return the 'Item Ledger Entry Type' for that date.

It's possible that you have a solution where the Last Posting date isn't needed. 

 

Posting DateItem Ledger Entry TypeItem NoLast Posting Date
28-09-2020             PurchaseEF039728-09-2020 
08-07-2020      Negative AdjustmentEF039728-09-2020 
23-06-2020               ConsumptionEF039728-09-2020 

 

My desired outcome is:

Posting DateItem Ledger Entry TypeItem NoLast Posting Date
28-09-2020                PurchaseEF039728-09-2020 

 

1 ACCEPTED SOLUTION
Smauro
Solution Sage
Solution Sage

Hi @SafeAnalyst ,

 

Using [Last Posting Date] :

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMrLQNbDUNTIwMlBQ0lFSQAIBpUXJGYnFqUBhVzcDY0tzIANZdaxOtJIBkG8O5sM1+6WmJ5ZklqUqOKZklRaX5KbmleAzwchY18AM1QQ4cM7PKy7NLSjJzM/Da4QJjA+URHW1iYkRSANCAVi9EWnqgWwDQyDX0BIoB/deIpr3TMzNgAwktbGxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Posting Date" = _t, #"Item Ledger Entry Type" = _t, #"Item No" = _t, #"Last Posting Date" = _t]),

    #"Keep Last Dates" = Table.SelectRows(Source, each [Posting Date] = [Last Posting Date])
in
    #"Keep Last Dates"


Using just [Posting Date] :

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMrLQNbDUNTIwMlBQ0lFSQAIBpUXJGYnFqUBhVzcDY0tzIANZdaxOtJIBkG8O5sM1+6WmJ5ZklqUqOKZklRaX5KbmleAzwchY18AM1QQ4cM7PKy7NLSjJzM/Da4QJjA+URHW1iYkRSANCAVi9EWnqgWwDQyDX0BIoB/deIpr3TMzNgAwktbGxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Posting Date" = _t, #"Item Ledger Entry Type" = _t, #"Item No" = _t, #"Last Posting Date" = _t]),

    LastDates = Table.Group(Source[[Item No], [Posting Date]], {"Item No"}, {{"Max Date", each List.Max([Posting Date]), type date}}),
    #"Keep Last Dates" = Table.NestedJoin ( Source, {"Item No", "Posting Date"}, LastDates, {"Item No", "Max Date"}, "Remove", JoinKind.Inner),
    #"Remove Extra Column" = Table.RemoveColumns( #"Keep Last Dates", {"Remove"})
in
    #"Remove Extra Column"



Feel free to connect with me:
LinkedIn

View solution in original post

6 REPLIES 6
Smauro
Solution Sage
Solution Sage

Hi @SafeAnalyst ,

 

Using [Last Posting Date] :

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMrLQNbDUNTIwMlBQ0lFSQAIBpUXJGYnFqUBhVzcDY0tzIANZdaxOtJIBkG8O5sM1+6WmJ5ZklqUqOKZklRaX5KbmleAzwchY18AM1QQ4cM7PKy7NLSjJzM/Da4QJjA+URHW1iYkRSANCAVi9EWnqgWwDQyDX0BIoB/deIpr3TMzNgAwktbGxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Posting Date" = _t, #"Item Ledger Entry Type" = _t, #"Item No" = _t, #"Last Posting Date" = _t]),

    #"Keep Last Dates" = Table.SelectRows(Source, each [Posting Date] = [Last Posting Date])
in
    #"Keep Last Dates"


Using just [Posting Date] :

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMrLQNbDUNTIwMlBQ0lFSQAIBpUXJGYnFqUBhVzcDY0tzIANZdaxOtJIBkG8O5sM1+6WmJ5ZklqUqOKZklRaX5KbmleAzwchY18AM1QQ4cM7PKy7NLSjJzM/Da4QJjA+URHW1iYkRSANCAVi9EWnqgWwDQyDX0BIoB/deIpr3TMzNgAwktbGxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Posting Date" = _t, #"Item Ledger Entry Type" = _t, #"Item No" = _t, #"Last Posting Date" = _t]),

    LastDates = Table.Group(Source[[Item No], [Posting Date]], {"Item No"}, {{"Max Date", each List.Max([Posting Date]), type date}}),
    #"Keep Last Dates" = Table.NestedJoin ( Source, {"Item No", "Posting Date"}, LastDates, {"Item No", "Max Date"}, "Remove", JoinKind.Inner),
    #"Remove Extra Column" = Table.RemoveColumns( #"Keep Last Dates", {"Remove"})
in
    #"Remove Extra Column"



Feel free to connect with me:
LinkedIn

Thankt you @Smauro!

 

I have a question regarding the Table.NestedJoin part of the code. How do you merge the same table with an earlier version of it self? Is it only possible when writing the code or can you do that through the UI by pressing merge queries?

SafeAnalyst
Frequent Visitor

Actually @AlB i have some issues with your code. It me be because i haven't expressed myself clearly enough.

 

I have added som more data to my example because your code only filters to the item numbers with the posting date of 29-09-2020.

 

Posting DateItem Ledger Entry TypeItem NoLast Posting Date
28-09-2020             PurchaseEF039728-09-2020 
08-07-2020      Negative AdjustmentEF039728-09-2020 
23-06-2020               ConsumptionEF039728-09-2020 
24-09-2020PurchaseEF044224-09-2020
22-09-2020PurchaseEF044224-09-2020
09-01-2019Negative adjustmentEF047609-01-2019
Anonymous
Not applicable

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMrLQNbDUNTIwMlBQ0lFSQAIBpUXJGYnFqUBhVzcDY0tzpVidaCUDoHpzsHq4cr/U9MSSzLJUBceUrNLiktzUvBJUPUbGugZmqHrgwDk/r7g0t6AkMz8PTZMJzGFAcVS3mJgYQZQYEVQClDcwBCoxtASKwx2aiOZQE3MzpdhYAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Posting Date" = _t, #"Item Ledger Entry Type" = _t, #"Item No" = _t]),
    #"Duplicated Column" = Table.DuplicateColumn(Source, "Posting Date", "Last Posting Date"),
    #"Grouped Rows" = Table.Group(#"Duplicated Column", {"Item No"}, {{"max Posting Date", each Table.Max(_, {"Posting Date"})}}),
    #"Expanded max Posting Date" = Table.ExpandRecordColumn(#"Grouped Rows", "max Posting Date", {"Posting Date", "Item Ledger Entry Type", "Last Posting Date"}, {"Posting Date", "Item Ledger Entry Type", "Last Posting Date"})
in
    #"Expanded max Posting Date"

 

 

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMrLQNbDUNTIwMlBQ0lFSQAIBpUXJGYnFqUBhVzcDY0tzpVidaCUDoHpzsHq4cr/U9MSSzLJUBceUrNLiktzUvBJUPUbGugZmqHrgwDk/r7g0t6AkMz8PTZMJzGFAcVS3mJgYQZQYEVQClDcwBCoxtASKwx2aiOZQE3MzpdhYAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Posting Date" = _t, #"Item Ledger Entry Type" = _t, #"Item No" = _t]),
    maxList=Table.ToRecords(Table.Group(Source, "Item No",{"Posting Date", each List.Max([Posting Date])})),
    tsr=Table.SelectRows(Source, each List.Contains(maxList, [[Item No],[Posting Date]]))
in
    tsr

 

AlB
Super User
Super User

Hi @SafeAnalyst 

Paste this code in a blank query to see the steps. #"Filtered Rows" is the relevant one

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMrLQNbDUNTIwMlBQ0lEKKC1KzkgsTgUyXd0MjC3NlWJ1opUMgGrMwWqA4n6p6YklmWWpCo4pWaXFJbmpeSWoqo2MdQ3MYKqd8/OKS3MLSjLz85BUxQIA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Posting Date" = _t, #"Item Ledger Entry Type" = _t, #"Item No" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Posting Date", type date}, {"Item Ledger Entry Type", type text}, {"Item No", type text}}),

    #"Filtered Rows" = Table.SelectRows(#"Changed Type", each List.Max(#"Changed Type"[Posting Date])=[Posting Date])
in
    #"Filtered Rows"

Please mark the question solved when done and consider giving kudos if posts are helpful.

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

Cheers 

SU18_powerbi_badge

 

Thanks @AlB - It worked like a charm. I figured it would be quite simple 🙂

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

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

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

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! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

Check out the November 2024 Power BI update to learn about new features.