Forum Discussion

nok's avatar
nok
Icon for Advocate II rankAdvocate II
11 months ago
Solved

Fetch value from another table where date is more recent

Hi!

I have two tables that follow this structure:

Orders

ID      Amount
AA12
AA88
BB34
CC90
CC56
DD23

 

History

IDStatusEditDate
AA    Ok23/09/2025    
AADelayed14/03/2025
BBIn progress   15/04/2025
BBOk21/01/2025
CCIn progress29/06/2025
CCFailed31/12/2025
CCOk01/12/2023
DDDelayed03/03/2024
DDContact11/12/2020

I want to create a column in the "Orders" table that retrieves the value of the "Status" column in the "History" table (through the ID relationship), but only the status where the EditDate column is the most recent date for that specifi ID. The end result would be something like this:

IDAmountStatus
AA    12Ok
AA88Ok
BB34In progress   
CC90In progress
CC56Failed
DD23Delayed


How can I do this?

  • Hi nok ,

    Thank you for reaching out to the Microsoft Community Forum.

     

    Please refer below Calculated column.

     

    Status =
    VAR latestDate =
        CALCULATE(
            MAX(History[EditDate]),
            FILTER(History, History[ID] = Orders[ID])
        )
    RETURN
        CALCULATE(
            MAX(History[Status]),
            FILTER(
                History,
                History[ID] = Orders[ID] &&
                History[EditDate] = latestDate
            )
        )
     
    Please refer output snap and attched PBIX file.
     

     

     

    I hope this information helps. Please do let us know if you have any further queries.

     

    Regards,

    Dinesh

     

     

7 Replies

  • Hi nok 

     

    Load both tables into Power Query.
    In the History table:

    Sort by EditDate descending.
    Use "Remove Duplicates" on the ID column — this keeps the row with the most recent date.


    Now, go to the Orders table:

    Use "Merge Queries" to join with the filtered History table on ID.
    Expand the Status column from the merged table.

     

    This will give you the desired result.

     

    --------------------------------

    I hope this helps, please give kudos and mark as solved if it does!

     

    Connect with me on LinkedIn.

    Subscribe to my YouTube channel for Fabric/Power Platform related content!

  • kpost's avatar
    kpost
    Icon for Solution Sage rankSolution Sage

    1) Open Power Query, pull up your History table. Make sure that "ID" is formatted as text and the "Date" column is formatted as date.

     

    2) Duplicate the History Table.  Call it something descriptive like "History_Maxdate".  Immediately un-select "Enable Load", as you will not be integrating this table into your semantic model.

     

    3) Remove the "Status column" from your new History_Maxdate table.

     

    4) Do a "Group by" on your History_Maxdate table and group by the ID, using MAX(Date) as the grouping.

     

    5) You should now have a table with Unique IDs in the first column and the "latest" of the dates for each one in the second column.

     

     

    6) Now you want to do a "Merge" between this "History Maxdate" table and your "History" table, like so, selecting both the ID and the Date columns.  


     

    7) Now you want to "Expand" that new column, selecting only the "status" field.

     

    8 ) ou should now have all of the correct Status values for the latest date.

     

     

    9) Next you want to do another 'Merge', this time from your Order table to this History Maxdate table, only on the ID column.

     

     

     

    10) Expand the new column by bringing in only the 'Status', and voila you are done.

    '

     

    See the attached .pbix file with your solution implemented.  And if this solution solves your problem for you, please mark 'Accept as Solution!'  Thank you.

     

    (BTW, I think in your desired result table you had a typo?  It should say "Failed" for both CC orders instead of "In Progress" for one and "Failed" for the other. 

     

    ///Mediocre Power BI Advice, but it's free!///

  • Shahid12523's avatar
    Shahid12523
    Icon for Community Champion rankCommunity Champion

    LatestStatus =
    VAR CurrentID = Orders[ID]
    VAR LatestDate =
    CALCULATE(
    MAX(History[EditDate]),
    History[ID] = CurrentID
    )
    RETURN
    CALCULATE(
    MAX(History[Status]),
    History[ID] = CurrentID,
    History[EditDate] = LatestDate
    )

  • Hi nok 

    You can do this easily using DAX by creating a calculated column in your Orders table that looks up the most recent status from the History table for each ID.

    Here’s how:

     

    Status =
    VAR currentID = Orders[ID]
    VAR LatestDate =
    CALCULATE (
    MAX ( History[EditDate] ),
    FILTER ( History, History[ID] = currentID )
    )
    RETURN
    CALCULATE (
    SELECTEDVALUE ( History[Status] ),
    FILTER (
    History,
    History[ID] = currentID &&
    History[EditDate] = LatestDate
    )
    )

      
    Thanks🌹

     

     

     

     

     

     

     

  • Hi,

    This calculated column formula works

    =LOOKUPVALUE(History[Status],History[EditDate],CALCULATE(MAX(History[EditDate]),FILTER(History,History[ID]=EARLIER(Orders[ID]))),History[ID],Orders[ID])

    Hope this helps.

     

  • Hi nok 

    You can use TOPN (top 1) on history table filtered by order ID and sorted by Edit Date in descending order and then return the status

    Latest Status = 
    VAR _ID = Orders[ID]
    VAR _History =
        TOPN ( 1, FILTER ( History, History[ID] = _ID ), History[EditDate], DESC )
    RETURN
        MAXX ( _History, [Status] )
    

     

  • v-dineshya's avatar
    v-dineshya
    Icon for Community Support rankCommunity Support

    Hi nok ,

    Thank you for reaching out to the Microsoft Community Forum.

     

    Please refer below Calculated column.

     

    Status =
    VAR latestDate =
        CALCULATE(
            MAX(History[EditDate]),
            FILTER(History, History[ID] = Orders[ID])
        )
    RETURN
        CALCULATE(
            MAX(History[Status]),
            FILTER(
                History,
                History[ID] = Orders[ID] &&
                History[EditDate] = latestDate
            )
        )
     
    Please refer output snap and attched PBIX file.
     

     

     

    I hope this information helps. Please do let us know if you have any further queries.

     

    Regards,

    Dinesh