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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
bonjourposte
Helper V
Helper V

Filtering out old rows based on status in most recent row

I want to use "Group By" in Power Query, but I don't want to aggregate anything.  A loan number can have several rows of old data in a table before it gets to the current row with the most recent status, and if the most recent row shows "PAIDINFULL", I want ALL the rows associated with that loan number filtered out.  How can I group the loan numbers together by the most recent status?  

 

(I'm loosely following this approach: https://community.fabric.microsoft.com/t5/Desktop/Filter-and-remove-data-in-Power-Query/m-p/2429554#...)

 

Sample Data: 

LoanStatusAccounting Date
1REPAYMENT4/30/2019
1REPAYMENT7/31/2020
1PAIDINFULL12/31/2024
2REPAYMENT6/30/2019
2REPAYMENT7/31/2019
2REPAYMENT1/31/2022
2PAIDINFULL7/31/2024
3REPAYMENT3/31/2020
3REPAYMENT8/31/2020
4REPAYMENT11/30/2021
4REPAYMENT6/30/2023
4REPAYMENT5/31/2024

 

 

Edit: for some reason my table looks like sh#t.  here's a screenshot: 

bonjourposte_0-1724864316338.png

 

Thanks.

1 ACCEPTED SOLUTION
DataInsights
Super User
Super User

@bonjourposte,

 

Try this Power Query solution. The original query is named Raw.

 

Create query MaxDate:

 

 

let
  Source = Raw,
  GroupRows = Table.Group(
    Source,
    {"Loan"},
    {
      {"MaxDate", each List.Max([Accounting Date]), type nullable date},
      {
        "All",
        each _,
        type table [Loan = nullable number, Status = nullable text, Accounting Date = nullable date]
      }
    }
  ),
  ExpandTable = Table.ExpandTableColumn(GroupRows, "All", {"Status"}, {"Status"}),
  FilterPaidInFull = Table.SelectRows(ExpandTable, each ([Status] = "PAIDINFULL"))
in
  FilterPaidInFull

 

 

DataInsights_0-1724866866671.png

 

Create query below. This query uses a left anti-join to exclude any loans that are in MaxDate (these are the PAIDINFULL loans). You can disable the load of queries Raw and MaxDate so only the final query will appear in your model.

 

 

let
  Source        = Table.NestedJoin(Raw, {"Loan"}, MaxDate, {"Loan"}, "MaxDate", JoinKind.LeftAnti),
  RemoveMaxDate = Table.RemoveColumns(Source, {"MaxDate"})
in
  RemoveMaxDate

 

 

DataInsights_1-1724867088748.png

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




View solution in original post

3 REPLIES 3
DataInsights
Super User
Super User

@bonjourposte,

 

Try this Power Query solution. The original query is named Raw.

 

Create query MaxDate:

 

 

let
  Source = Raw,
  GroupRows = Table.Group(
    Source,
    {"Loan"},
    {
      {"MaxDate", each List.Max([Accounting Date]), type nullable date},
      {
        "All",
        each _,
        type table [Loan = nullable number, Status = nullable text, Accounting Date = nullable date]
      }
    }
  ),
  ExpandTable = Table.ExpandTableColumn(GroupRows, "All", {"Status"}, {"Status"}),
  FilterPaidInFull = Table.SelectRows(ExpandTable, each ([Status] = "PAIDINFULL"))
in
  FilterPaidInFull

 

 

DataInsights_0-1724866866671.png

 

Create query below. This query uses a left anti-join to exclude any loans that are in MaxDate (these are the PAIDINFULL loans). You can disable the load of queries Raw and MaxDate so only the final query will appear in your model.

 

 

let
  Source        = Table.NestedJoin(Raw, {"Loan"}, MaxDate, {"Loan"}, "MaxDate", JoinKind.LeftAnti),
  RemoveMaxDate = Table.RemoveColumns(Source, {"MaxDate"})
in
  RemoveMaxDate

 

 

DataInsights_1-1724867088748.png

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Thank-you! I got it to work! (although I had to change Loan to nullable text).  Sweet!

 

EDIT: I just checked, and my table isn't dynamic, isn't hasn't updated with the 5 payouts done this morning, so instead of Duplicating LOANHIST to create MaxDate, I should probably Reference it, right?

 

bonjourposte_0-1724948011449.png

 

 

 

@bonjourposte,

 

Correct, use "Reference".





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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

Top Solution Authors