

Cross Database Query Folding For SQL Server Sources In Power Query In Power BI A...
source link: https://blog.crossjoin.co.uk/2023/08/06/cross-database-query-folding-for-sql-server-sources-in-power-query-in-power-bi-and-excel/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Excel · Power BI · Power Query · SQL Server
Cross Database Query Folding For SQL Server Sources In Power Query In Power BI And Excel
A few months ago a new option was added to the Sql.Database and Sql.Databases functions in Power Query in Power BI and Excel which allows Power Query queries that combine data from different SQL Server databases to fold. Here’s a simple example showing how to use it.
On my local PC I have SQL Server installed and the Adventure Works DW 2017 and Contoso Retail DW sample databases:
Both of these databases have date dimension tables called DimDate. Let’s say you want to create a Power Query query that merges these two tables.
Here’s the M code for a Power Query query called DimDate AW to get just the DateKey and CalendarYear columns from the DimDate table of the Adventure Works DW 2017 database:
let
Source = Sql.Database("localhost", "AdventureWorksDW2017"),
dbo_DimDate = Source{[Schema="dbo",Item="DimDate"]}[Data],
#"Removed Other Columns" = Table.SelectColumns(dbo_DimDate,{"DateKey", "CalendarYear"})
in
#"Removed Other Columns"
Here’s the M code for a Power Query query called DimDate Contoso to get just the Datekey and CalendarYear columns from the DimDate table in the ContosoRetailDW database:
let
Source = Sql.Database("localhost", "ContosoRetailDW"),
dbo_DimDate = Source{[Schema="dbo",Item="DimDate"]}[Data],
#"Removed Other Columns" = Table.SelectColumns(dbo_DimDate,{"Datekey", "CalendarYear"})
in
#"Removed Other Columns"
Both of these Power Query queries fold. However if you create a third query to merge these two queries (ie do the equivalent of a SQL join between them) on the CalendarYear columns like so:
let
Source = Table.NestedJoin(
#"DimDate Contoso",
{"CalendarYear"},
#"DimDate AW",
{"CalendarYear"},
"DimDate AW",
JoinKind.LeftOuter
),
#"Expanded DimDate AW" = Table.ExpandTableColumn(
Source,
"DimDate AW",
{"DateKey", "CalendarYear"},
{"DimDate AW.DateKey", "DimDate AW.CalendarYear"}
)
in
#"Expanded DimDate AW"
…this query does not fold, because it combines data from two different SQL Server databases.
However if you edit the Sql.Database function in the Source step of both of the first two queries above to set the new EnableCrossDatabaseFolding option to true, like so:
Source =
Sql.Database(
"localhost",
"ContosoRetailDW",
[EnableCrossDatabaseFolding=true]
),
…then the query that merges these two queries does fold:
Published by Chris Webb
My name is Chris Webb, and I work on the Fabric CAT team at Microsoft. I blog about Power BI, Power Query, SQL Server Analysis Services, Azure Analysis Services and Excel. View all posts by Chris Webb
Like this:
Recommend
-
7
View Native Query Now Works For Analysis Services Data Sources If you’re familiar with the topic of query folding in Power Query,...
-
18
Optimising The Performance Of Power Query Merges In Power BI, Part 5: Cross Joins In a late addition to the series of posts that I started
-
28
Database Query Server using Quarkus We are updating our application to Quarkus and I have this requirement of supporting datasouces that users of our application can create.This is already supported in
-
8
Solved! Query folding for native SQL in Power BIWriting a native SQL query in Power BI will break query folding, right? Well, not anymoreImage by authorThe only constant when it comes to technology...
-
6
Query folding in Power BI — tricks, lies & ultimate performance testIn the final part of the Query folding series, learn more tricks, find out when the View Native Query lies, and finally spot the difference betw...
-
7
Jerod Johnson Posted on Nov 12...
-
4
What is a Query folding in Power BI and why should I care?“Will that break a query folding?” “Does your query fold?”… Maybe someone asked you those questions, but you were like: “Query…Whaaaat?!” In this series of ar...
-
6
Query folding in Power BI— the devil is in the detailIn the 2nd part of the series on Query folding in Power BI, learn why people say that the devil is in the detail!
-
10
One Exposure, which is the central storage for SAP Cash Management, storing the data (named cash flows) which forming the figures in Cash Position and Liquidity Analysis. It is quite a common and basic requirement to compare the figures fro...
-
10
Anti joins and query folding in Power Query in Excel and Power BI Power Query allows you to merge (“join” in database terms) two tables together in a variety of different ways, including
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK