Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.
I'm looking for a M-code that minic this SQL code:
--
Note: While writing this, I figured out a way to solve this. But it's kind of not how I expected this to be solved, and a bit more complex then I expected. But I add it here for reference and if someone have a better and easier solution, please show how.
Even though the solution was based on a M-code that was not that obvious, I'm finally impressed how the Power Query Engine managed to optimize the final SQL code
--
userId | Name |
1 | Donald Trump |
2 | Joe Biden |
FactTable:
Fact | customerId |
3.14 | 1 |
0.707 | 1 |
Running the SQL query above will select only thouse rows in the DimensionTable that has an existing customerId in the FactTable like below:
userId | Name |
1 | Donald Trump |
I can't use Native SQL queries since that will stop query folding.
I also can't create a view in the SQL database that executes the SQL query for me, else this would have been a solution
So this needs to be solved using pure M.
What I first tried was to creat two none loading queries like below:
// DimensionTable
let
Source = Sql.Database(Server1, Database1),
DimensionTable = Source{[Schema=Schema1,Item="DimensionTable"]}[Data]
in
DimensionTable
// FactTable
let
Source = Sql.Database(Server1, Database1),
FactTable = Source{[Schema=Schema1,Item="FactTable"]}[Data]
in
FactTable
I can then referens these two queries in my first attempt
// CustomerTable1
let
Source = Table.AddJoinColumn(DimensionTable, "userId", FactTable, "customerId", "facts"),
// Or using this: Source = Table.NestedJoin(DimensionTable, "userId", FactTable, "customerId", "facts"),
CustomersOnly = Table.SelectRows(Source, each Table.IsEmpty([facts])<>true)
in
CustomersOnly
This kind of works (delivers the expecte result), although this doesn't support query folding and will be to slow. But maybe there's some other solution that will support Query Folding
The other approach would be to mimic the other SQL pattern doing the exact same thing:
I expected this to work and I also expected it to do Query Folding. But I didn't expect it to generate this SQL code, and I never expected it to generate the same query plans as the other SQL codes does.
But as I said in the beginning. If anyone knows a better M-code, please show how
Hi, @pade
Based on your description, I created data to reproduce your scenario, The pbix file is attached in the end,
Dimension:
Fact:
Power Query:
As is suggested by @wdx223_Daniel , you may create a new query with the following codes in 'Advanced Editor'.
let
Source = Table.SelectRows(Dimension,each List.Contains(Fact[CustomerId],[UserId]))
in
Source
Result:
DAX:
You may create a calculated table as below.
Table =
FILTER(
'Dimension',
[UserId] in DISTINCT('Fact'[CustomerId])
)
Result:
Best Regards
Allan
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Thanks @v-alq-msft .
Although, I can't use this solution either, and I realize it's my fault. I didn't mention that I'm using Direct Query mode, hense I can't create calculated tables.
I will update my initial post so this is more clear
Thanks @wdx223_Daniel .
Although, I can't see that this supports Query Folding, so it will take a very long time to execute on large tables
Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City
Check out the April 2024 Power BI update to learn about new features.