Forum Discussion
Adding/merging a Custom Column based upon a Date Condition
I'm new(ish) to this. I did this in PQ in Excel, so set up the two tables Transactions and Rates (blue tables below) and put them in Power Query.
In the Transactions query, I merged the Rates table according to Location and Rate Class and ended up with a column with rates tables with just the relevant Location and Rate Class rows from Rates table.
Then I put together a function which for each row which for each row in the Transaction table:
takes the table in the newly added column,
keeps all the rows before or equal to the Transaction date,
then keeps only therows with the latest (max) of the remaining dates and
returns only the Rate Amount column.
At the moment this function returns a list (of one in this case) which needs to be expanded. I should really have converted this list within the function to plain value by adding {0} to the line:
FilteredRows1 = Table.SelectRows(FilteredRows, let latest = List.Max(FilteredRows[Rate Effective Date]) in each [Rate Effective Date] = latest)[Rate Amount]
making it:
FilteredRows1 = Table.SelectRows(FilteredRows, let latest = List.Max(FilteredRows[Rate Effective Date]) in each [Rate Effective Date] = latest)[Rate Amount]{0}
which would mean you don't need the ExpandedRateAtTheTime step.
As I left it, if there are multiple rates for the same dates they're all returned and you'd get as many rows for that transaction as there are entries in the Rates table for that date/location/rate class; this would alert you to the existence of such errant rows in the Rates table!
The M-code I ended up with:
let
Source = Excel.CurrentWorkbook(){[Name="Transactions"]}[Content],
fnRateAtTheTime = (tbl,dte)=>
let
FilteredRows = Table.SelectRows(tbl, each [Rate Effective Date] <= dte),
FilteredRows1 = Table.SelectRows(FilteredRows, let latest = List.Max(FilteredRows[Rate Effective Date]) in each [Rate Effective Date] = latest)[Rate Amount]
in
FilteredRows1,
ChangedType = Table.TransformColumnTypes(Source,{{"Transaction Date", type date}}),
MergedQueries = Table.NestedJoin(ChangedType, {"Location", "Rate Class"}, Rates, {"Location", "Rate Class"}, "Rates", JoinKind.LeftOuter),
InvokedCustomFunction = Table.AddColumn(MergedQueries, "RateAtTheTime", each fnRateAtTheTime([Rates], [Transaction Date])),
ExpandedRateAtTheTime = Table.ExpandListColumn(InvokedCustomFunction, "RateAtTheTime"),
RemovedColumns = Table.RemoveColumns(ExpandedRateAtTheTime,{"Rates"})
in
RemovedColumns
I'm sure there's a slicker way but I hope I answered your question 'where do I begin…'
My results in the green table:
Thanks p45cal for your response! 🙂
When I tried to implement your M-Code verbatim in Excel, however, I got the following Warning message:
Unfortunately, I don't have the required advanced skills to "troubleshoot and/or edit" your solution relative to the warning message.
Respectfully, CincyKJ
- p45cal2 years agoSolution Supplier
Odd. A pain.
See if anything at https://community.fabric.microsoft.com/t5/Power-Query/Query-references-other-queries-or-steps-so-it-may-not-directly/td-p/2836287
helps.