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:
pricepaid | 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:
let CountyList = if //check to see if the parameter is a list Type.Is( Value.Type(SelectedCounty), List.Type ) then //if it is a list let //add single quotes around each value in the list AddSingleQuotes = List.Transform( SelectedCounty, each "'" & _ & "'" ), //then turn it into a comma-delimited list DelimitedList = Text.Combine( AddSingleQuotes, "," ) in DelimitedList else //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( "https://mycluster.northeurope.kusto.windows.net", "pricepaid", "pricepaid#(lf) | where county in (" & CountyList & ")#(lf) | summarize avg(price) by county", [ MaxRows = null, MaxSize = null, NoTruncate = null, AdditionalSetStatements = null ] ) in Source
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]