Handling Multi-select In Power BI Dynamic M Parameters

Even though the documentation for dynamic M parameters does mention how to handle multi-select in the M code for your Power Query queries, I thought it would be useful to provide a detailed example of how to do this and explain what happens behind the scenes when you use multi-select.

Once again I’m going to use Azure Data Explorer as my DirectQuery data source in this post. Consider the following simple KQL query, which returns a list of counties in the UK and the average price paid for real estate in them:

| where county in ('DEVON', 'CORNWALL', 'KENT')
| summarize avg(price) by county

The third line of this query uses a combination of the KQL where and in operators to filter the list of counties in a way that’s very similar to other query languages such as SQL. The challenge is to write an M expression that will generate the comma-delimited list of county names in parantheses at the end of this line.

Taking the query above and generating a non-dynamic DirectQuery table in Power BI is straightforward, and let’s assume that you have already created an M parameter called Selected County:

… a table of county names:

…and bound the County column of this table to the SelectedCounty M parameter and turned on the Multi-select option in Model View in the main Power BI Desktop window:

Now the confusing thing for me, when I first started to look at this problem, was that the M parameter called SelectedCounty I created was of type Text but the example code in the documentation was written for an M parameter of type list – when in fact it isn’t possible to create an M parameter of type list in the Power Query Editor (at least not at the time of writing). It turns out that when the Multi-select option is turned on Power BI is able to send a value of type list to the parameter regardless of what type you have defined for it.

With that knowledge here’s an example of an M query to generate the query using the parameter:

  CountyList = 
    //check to see if the parameter is a list
      ) then 
        //if it is a list
          //add single quotes around each value in the list
          AddSingleQuotes = List.Transform(
              each "'" & _ & "'"
          //then turn it into a comma-delimited list
          DelimitedList = Text.Combine(
      //if the parameter isn't a list
      //just add single quotes around the parameter value
      "'" & SelectedCounty & "'",
  //generate and run the KQL query
  Source = AzureDataExplorer.Contents(
       | where county in 
        & CountyList &
       | summarize avg(price) by county", 
        MaxRows                 = null, 
        MaxSize                 = null, 
        NoTruncate              = null, 
        AdditionalSetStatements = null

Note that the CountyList step has to check the data type of the parameter using an if statement, because in the Query Editor it will always be Text whereas in the report it will be a list if multi-select is turned on and Text if not.

Here’s the final report showing a multiselect slicer passing values into this query:

[Thanks to Ravi Kiran Vemulapalli and Sujata Narayana for their help with this post]

9 responses

  1. One thing worth mentioning – the whole point of having dynamic M parameters in the ADX connector, was to support generating efficient KQL queries. In your sample, this means performing the `where` statement BEFORE the `summarize` statement. While in this simple sample, ADX is smart enough to propagate the filtering before the aggregation when executing the query, on more complex queries this might not always be the case…

  2. Pingback: Dynamic M Parameters and Multi-Select – Curated SQL

  3. Pingback: Handling Multi-select In Power BI Dynamic M Parameters | Pardaan.com

  4. Hi, is there a way to use the Select All-Option?
    The Multi-Select is working perfectly fine, but the Select All is greyed out.
    If I select nothing, the label in the dropdown is “All”, but only seems to use the Value that is set in the parameter in Power Query.

    We use the parameters combined with Kusto Queries. The values for the button are queried from our ADX.

  5. Pingback: Chris Webb's BI Blog: Power Query And Power BI Connectivity Announcements At The Microsoft Business Applications Summit Chris Webb's BI Blog

  6. Pingback: Power BI: Selección múltiple en parámetros M dinámicos - power BI

  7. Pingback: Chris Webb's BI Blog: Handling “Select All” For Slicers Bound To Dynamic M Parameters In Power BI Chris Webb's BI Blog

Leave a Reply

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

%d bloggers like this: