Forum Discussion
Help with Identifying the Most Recent Contract
- 1 year ago
re: "Ideally, this should be foldable, since the source is a SQL Azure DB."
In the attached is a query which is very unlikely to be foldable.
I'm still learning m code so this effort is probably (a) naïve and (b) will take an age!
It currently gives the results you expect, however.
- 1 year ago
Here's another M-Code solution. Although I believe an upstream SQL solution would be the most efficient:
Original Data:
let Source = Table, //Your original data table #"Group Account" = Table.Group(Source, "AccountID",{ {"all", (t)=> if List.Contains(t[Status],"Active") then Table.SelectRows(t, each [Status] = "Active") else if List.Contains(t[Status],"Future") then [a=List.Select(t[StartDate], each _ > Date.From(DateTime.FixedLocalNow())), b=List.Min(a), c=Table.SelectRows(t, each [StartDate]=b)][c] else Table.SelectRows(t, each [StartDate] = List.Max(t[StartDate])), type table[ContractID=Int64.Type, Status=text, StartDate=date, EndDate=date]} }), #"Expanded all" = Table.ExpandTableColumn(#"Group Account", "all", {"ContractID", "Status", "StartDate", "EndDate"}) in #"Expanded all" - 1 year ago
Hi DerhakRon,
Thank you for reaching out to the Microsoft fabric community forum. Also thanks SundarRaj, ronrsnfld, p45cal, AlexisOlson, for those valuable insights for this thread.
After thoroughly reviewing the details you provided, I reproduced the scenario again, and it worked on my end. I used it as sample data and successfully implemented it.
outcome:
I am also including .pbix file for your better understanding, please have a look into it:
If this post helps, then please give us ‘Kudos’ and consider Accept it as a solution to help the other members find it more quickly.
Thank you for using Microsoft Community Forum.
Hi DerhakRon
To efficiently filter your 24-million-row Contract table in Power Query (especially with a foldable query against Azure SQL), you can use a grouping and conditional filtering approach that mimics your DAX logic. The key is to group by AccountID and then apply conditional logic within each group to retain only the contract that fits your priority criteria: (1) if there's an Active contract, keep the most recent Active one; (2) if only Past contracts, keep the latest by StartDate; (3) if only Future contracts, keep the one with the soonest upcoming StartDate; (4) if both Future and Past exist but no Active, still pick the soonest Future one. In Power Query (M), this typically involves using Table.Group to nest contract records per account, followed by a custom column using Table.SelectRows and Table.Sort logic to apply your filtering rules within each subgroup. While Power Query itself may not support native SQL folding with this entire logic chain, you can try using Table.Group with as few transformations as possible inside the sub-tables, or even consider building the logic as a SQL view in Azure and using Power BI to connect to that filtered view instead. This offloads the filtering to the server and ensures scalability. If performance is still critical, you might consider creating a SQL stored procedure or computed view that encapsulates this logic using ROW_NUMBER() partitioned by AccountID with CASE statements prioritizing status. That would yield a much leaner dataset to import or query directly into Power BI.