Forum Discussion

ElinG's avatar
ElinG
Frequent Visitor
1 year ago
Solved

The last date

Hi   I have a column Org_parner_nr that is not unik, and then I have like snap_month that I will take out the last one Is there anyone that know how to?
  • Cookistador's avatar
    1 year ago

    Hello ElinG 

     

    You can solve this issue with the following step

     

    1)Create a copy of your table and name it for FilterDate (or the name you want to give)

    2) Delete all columns excpept snap_month 

    3) Select the column then Transform --> Date --> Latest

    you should have something like this 

     

    4) In your intitial table, Applied a filter on the date (select one date in your list)
    You will get something like:

    = Table.SelectRows(#"Changed Type", each ([snap_month ] = #date(2025, 2, 15)))
    5) Replace date(2025, 2, 15) by FilterDate (or the name you gave to your table)

     

    Tadam, you only get the last value, each time you will refresh, your filteredtable will be updated

     

    If it is not what you are trying to achieve, let me know 🙂

  • rohit1991's avatar
    1 year ago

    Hi ElinG ,

    If you have a column like Org_partner_nr (not unique) and a snap_month column (representing a time snapshot), and you want to keep only the latest snap_month for each Org_partner_nr, here’s how you can do it in Power Query:

    1. Sort the table by snap_month in descending order.

    2. Then go to the "Home" tab and use "Group By".

    3. In the Group By window: Group by: Org_partner_nr >> Operation: Select "All Rows" (this will nest the grouped data).

    4. Add a custom column inside the nested tables to extract the top row (the latest one).

    5. Expand the nested tables to bring the full row back.

    Or alternatively, if you're comfortable with code, you can use something like this in M:

    let
        SortedTable = Table.Sort(YourTable, {{"Org_partner_nr", Order.Ascending}, {"snap_month", Order.Descending}}),
        GroupedTable = Table.Group(SortedTable, {"Org_partner_nr"}, {{"Latest", each Table.FirstN(_, 1)}}),
        ExpandedTable = Table.ExpandTableColumn(GroupedTable, "Latest", Table.ColumnNames(SortedTable))
    in
        ExpandedTable