Forum Discussion
Dynamic Merging of Tables - With Ranges and IF statements
- 3 years ago
Hi JamesMcEwan ,
Firstly, +2 for the perfectly executed provision of example data.
Now, I usually advise to do EVERYTHING in Power Query, and to NEVER use DAX calculated columns. However, conditional merges is the one scenario where I break from this. There are a number of ways to do it in Power Query, but not one of them I've found so far comes even close to matching the performance of DAX for this.
Send both your original tables to your data model then, on your Accounts table, create a calculated column like this:
..reportKey = CALCULATE( VAR __compRow = VALUES(Accounts[CompanyID]) VAR __locnRow = VALUES(Accounts[LocationID]) VAR __acctRow = VALUES(Accounts[Account]) RETURN MAXX( FILTER( Reports, Reports[CompanyID] = __compRow && Reports[LocationID] = __locnRow && Reports[AccountFrom] <= __acctRow && Reports[AccountTo] >= __acctRow ), Reports[ReportKey] ) )You'll want to tweak a bit to handle your "*" location escape, but this should give you a solid starting point.
Give it a go and let me know how you get on.
Pete
Hi BA_Pete,
Glad it helps! You get out what you put in! 😁
Thanks for the input - I had thought of doing it via dax but wanted to keep all transformations in powerquery. It's good to hear confirmation that this might be an exceptional circumstance to the ETL best practice.
The code you provided will work for what I need. I will let you know how I get one.
Thanks, again.
No problem.
Note that this is just my personal preference, certainly not an official exception to best practice.
This is still doable in PQ, if you really want to do it, just let me know, but performance will likely be significantly worse.
Pete