M · Power BI · Power Query

Another Way To Check Query Folding In Power BI/Power Query M Code

Following on from my last post on checking whether query folding is taking place in a Power BI/Power Query query, I’ve just stumbled on another way of doing this – by looking at metadata. I blogged about M metadata and Power Query some time ago (you might want to read the post to get some background), but it seems like metadata is used more widely than I thought in M…

Take the following query, as used in my previous blog post:

[sourcecode language=”text”]
let
Source = Sql.Database("localhost", "adventure works dw"),
dbo_DimDate = Source{[Schema="dbo",Item="DimDate"]}[Data],
#"Removed Other Columns" =
Table.SelectColumns(
dbo_DimDate,
{"DateKey", "FullDateAlternateKey",
"DayNumberOfWeek"}),
#"Filtered Rows" =
Table.SelectRows(
#"Removed Other Columns",
each ([DayNumberOfWeek] = 1))
in
#"Filtered Rows"
[/sourcecode]

As shown in that post this query connects to the Adventure Works DW database in SQL Server and gets a few columns plus some filtered rows from the DimDate table, and most importantly query folding takes place for all of these transformations.

It turns out that each of the variables used as steps in the query can have metadata automatically assigned to them, some of which is relevant to query folding, and we can use the Value.Metadata() function to get this metadata. So, taking the previous query, we can add two extra steps:

[sourcecode language=”text” highlight=”13,14″ padlinenumbers=”true”]
let
Source = Sql.Database("localhost", "adventure works dw"),
dbo_DimDate = Source{[Schema="dbo",Item="DimDate"]}[Data],
#"Removed Other Columns" =
Table.SelectColumns(
dbo_DimDate,
{"DateKey", "FullDateAlternateKey",
"DayNumberOfWeek"}),
#"Filtered Rows" =
Table.SelectRows(
#"Removed Other Columns",
each ([DayNumberOfWeek] = 1)),
GetMetadata = Value.Metadata(#"Filtered Rows"),
QueryFolding = GetMetadata[QueryFolding]
in
QueryFolding
[/sourcecode]

The GetMetadata step gets the metadata associated with the #”Filtered Rows” step, and that metadata comes back in the form of a record with a single field called QueryFolding. The next step gets the data from that field, which is again in the form of a record:

image

So we can see quite clearly that this step is being folded and that SQL is being generated in the background. It doesn’t seem to be possible to get the SQL query generated here though.

One last thing to note: while all of this works for SQL Server, when I tested it on an SSAS data source where query folding is also definitely taking place the metadata told me – incorrectly – that there was no query folding happening (I couldn’t see the MDX query generated using the View Native Query menu option either). Maybe this is all still a work in progress?

15 thoughts on “Another Way To Check Query Folding In Power BI/Power Query M Code

  1. Thanks Chris,

    I’m getting an error when I try to incorporate this into my code:
    Expression.Error: The field ‘QueryFolding’ of the record wasn’t found.
    Details:

    Do you know what I might be doing wrong?
    Thanks

    (using Excel 2016)

    1. 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.
      Chris Webb says:

      Are you using the Office 365 click-to-run version of Excel? If not, your version of Power Query probably won’t include this feature.

  2. Pingback: SutoCom Solutions
  3. I tried it with Excel 2016 (on prem) as well and getting the same result as Richard. However in Query Editor I get a properly looking table as in Chris’s screenshot above. Strange thing is: doesn’t matter how simple the query I use is, it always says “IsFolded: False”. Tried it with Microsoft Dynamics CRM 2016 On Premises and with MS Dynamics 365 Online.

  4. Hi Chris

    Quick question: I note that Table.Profile() does query fold in most cases (using SQL Profiler) however it does not show up in either: (a) right click step -> View Native Query; and (b) GetMetadata. Do you know what’s happening here?

    Cheers,
    Simon

    1. 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.
      Chris Webb says:

      There seem to be a few scenarios where View Native Query doesn’t show a query, even when query folding is taking place (eg when you are importing data from SSAS), so I guess it’s another one of those. I think the Power Query dev team are working on UI improvements that will show us what is folding and what isn’t.

  5. ricardodiazjimenez – World – I'm a person of big ambitions, but takes care of small details. As an entrepreneur I have learned to value people that surrounds me and invest in their wealth. Living in a big chaotic city has been an inspiration for ideas to make working from home a viable option. From some years, until now, I have discovered that sharing my knowledge is a way to feel good and at the same time, receive more. I have invested in a headhunting company specialized in information technology, an elearning content production company and an outsourcing administrative services company.
    ricardodiazjimenez says:

    Hi Chris. Have you succeed using query folding against Exchange Online? Every step in the query I have tried doesn’t allow query folding.

    1. 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.
      Chris Webb says:

      For some data sources (and Exchange might be the same) there is no way of knowing whether query folding is taking place in the user interface.

      1. ricardodiazjimenez – World – I'm a person of big ambitions, but takes care of small details. As an entrepreneur I have learned to value people that surrounds me and invest in their wealth. Living in a big chaotic city has been an inspiration for ideas to make working from home a viable option. From some years, until now, I have discovered that sharing my knowledge is a way to feel good and at the same time, receive more. I have invested in a headhunting company specialized in information technology, an elearning content production company and an outsourcing administrative services company.
        ricardodiazjimenez says:

        Thank you Chris

  6. Hi Chris,

    I was trying to check if “query folding” is happening using “value.metadata” function as suggested. But I did not get any result (Expression.Error: The field ‘QueryFolding’ of the record wasn’t found) when I tried this in original file. But when I tried to recreate the same transformations in a new file, I could see the query folding details ISFOLDED = TRUE, HASNATIVEQUERY = FALSE as shown in the screenshots. Do you know why “value.metadata” does not return any value in some scenarios?

    Source – DataBricks

    Thanks in advance,
    Sneha

  7. Hi Chris,

    I was trying to check if “query folding” is happening using “value.metadata” function. But I did not get any result (Expression.Error: The field ‘QueryFolding’ of the record wasn’t found) when I tried this in original pbix file. But when I tried to recreate the same transformations in a new file, I could see the query folding details as shown (ISFOLDED = TRUE, HASNATIVEQUERY = FALSE). Do you know why “value.metadata” does not return any value in some scenarios?

    Source: Databricks

    Thanks in advance,
    Sneha

Leave a ReplyCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.