Dynamic M Parameters, Snowflake Native SQL And Paginated Reports

There were a couple of new features and enhancements to existing features in the June 2021 Power BI Desktop release that don’t seem to have much to do with each other but which I think can be combined to do cool things. They are:

  1. The new paginated report visual
  2. Native SQL support in the Snowflake connector
  3. Improvements to dynamic M parameters

Let me give you an example of what I mean…

First of all, let’s start with native SQL support in the Snowflake connector. I deal with a lot of customers who use Snowflake and Power BI together and I know just how much people have wanted this. What does it allow you to do? Well, you have always been able to use the Power Query Editor to transform data coming from Snowflake in either Import mode or DirectQuery mode. Now, though, you can write your own native SQL query and use it as the source for a Power Query query (something that has always been possible with some other connectors, such as the SQL Server connector). Incidentally, this also means that the EnableFolding=true option for Value.NativeQuery that I blogged about recently also now works for Snowflake too.

The main reason you’d want to use a native SQL query when connecting to Snowflake, or indeed any database, is to do something that’s possible in SQL but not in Power Query. One example of this is to use regular expressions to filter data. I have the AdventureWorks DW DimCustomer table loaded into Snowflake and I can use Snowflake’s REGEXP function to filter on the LASTNAME column something like this:

SELECT 
DISTINCT FIRSTNAME, LASTNAME, ENGLISHOCCUPATION 
FROM "AWORKS"."PUBLIC"."DIMCUSTOMER" 
WHERE LASTNAME REGEXP 'To.*'

So that’s useful. I can use a query like this as the source of a table in DirectQuery mode in Power BI, but wouldn’t it be useful if end users of my report could change the regular expression used to filter the data? This is where dynamic M parameters come in. Assuming I have a table of pre-defined regular expressions:

And an M parameter:

…I can write an M query like this that uses the M parameter to return the regular expression used in the WHERE clause of the SQL query:

let
  Source = Value.NativeQuery(
    Snowflake.Databases(
      "mysnowflake.com", 
      "DEMO_WH"
    ){[Name = "AWORKS"]}[Data], 
    "SELECT DISTINCT FIRSTNAME, LASTNAME, ENGLISHOCCUPATION 
    FROM ""AWORKS"".""PUBLIC"".""DIMCUSTOMER"" 
    WHERE LASTNAME REGEXP '"
      & pRegEx
      & "'", 
    null, 
    [EnableFolding = true]
  )
in
  Source

…and then turn this into a dynamic M parameter in the Power BI diagram pane:

…and get a report that does this:

One limitation of dynamic M parameters in regular Power BI reports today is that the values you pass into them have to come from a column somewhere inside your dataset, so all of these values have to be pre-defined. Wouldn’t it be useful if the end user could enter any regular expression that they wanted though? That may not be possible in a regular Power BI report but it is possible with a paginated report, because with paginated reports you can write whatever DAX query you want – and therefore pass any value you want to a dynamic M parameter – and also, in a paginated report, you have the option of creating parameters where the user can enter whatever value they want.

I blogged about how to write DAX queries that contain dynamic M parameters here. Here’s an example of a parameterised DAX query (yes, I know, so many types of parameters…) that takes a regular expression and the name of an occupation and returns a table of customers whose last names match the regular expression and whose occupations match the one entered:

DEFINE
    MPARAMETER pRegEx = @DAXRegExParam
EVALUATE
FILTER (
    Customers,
    'Customers'[ENGLISHOCCUPATION] = @DAXOccupationParam
)

This can be used in a paginated report dataset connected to the Power BI dataset created above (yes, I know, so many types of datasets…) like so:

….which can then be used to build a paginated report that does this:

And of course, with the new paginated report visual, this paginated report can be embedded in a regular Power BI report:

All this is very much a proof-of-concept and not something I would recommend for production (I would be worried about SQL injection attacks for a start). There are more enhancements to these features still to come too. However, I do think it’s interesting to see how these features can be put together now and to imagine how they could be used in the future. What do you think?

14 thoughts on “Dynamic M Parameters, Snowflake Native SQL And Paginated Reports

  1. Thanks for this, very helpful! Have you had any success trying to use parameterized queries with Value.NativeQuery and Snowflake?

    Something like parameterizing the LastName filter here?

    let
    Source = Value.NativeQuery(
    Snowflake.Databases(
    “mysnowflake.com”,
    “DEMO_WH”
    ){[Name = “AWORKS”]}[Data],
    “SELECT DISTINCT FIRSTNAME, LASTNAME, ENGLISHOCCUPATION
    FROM “”AWORKS””.””PUBLIC””.””DIMCUSTOMER””
    WHERE LASTNAME = ‘Torres'”,
    null,
    [EnableFolding = true]
    )
    in
    Source

  2. This is very informative, thank you for this. Have you tried passing parameter values directly in native query for Snowflake? I read your other blog about doing so with SQL server, wondering if this is possible for Snowflake.

  3. Hi Chris,
    I added parameter (Snowflake sever instance, data warehouse) and configured in Advanced Editor. When I am trying to add new source i..e, Snowflake in Power BI I am unable to see the option to choose parameter. The same process is working for SQL Server Database and able to choose Parameters. I am eager to hear any suggestions…

  4. Hi, this was really helpfull.
    What about the scenario where the parameters in the sql script are needed in CTE’s or subqueries, not in the actual query output. How can i link a report Builder parameter to these dynamic M parameters in the sql. It looks like it is only possible to link the m parameter with the report builder parameter on columns that are available in the SELECT of the sql. Thanks!

  5. Happy to have found this helpful article, thank you!

    Our company’s migrating a significant portion of its data into Snowflake.

    Until now, no one on our team has taken advantage of Power BI’s native query function aside from connections with Azure.

    1. Import mode should always be your first choice; we have a lot of customers using DirectQuery on Snowflake but you need to be a lot more careful with your design and tuning to get things working well.

  6. Hi, Let’s say I want to pass like multiple parameters to binding .. can you please tell me one example ? want to add where clause like year and quarter

Leave a Reply