神刀安全网

The M Code Behind Power BI Parameters

For me the most exciting new feature in Power BI in a long while is the appearance of Query Parameters for data loading. We have been promised an official blog post explaining how they work (although they are very easy to use) and in fact Soheil Bakhshi has already two very good, detailed posts on them here and here . What I want to do in this post, however, is look at the M code that is generated for them and see how it works.

Consider the following parameter built in Power BI Desktop that has, as its possible values, the names of all of the days of the week:

The M Code Behind Power BI Parameters

The first thing to notice is that parameters are shown as a special type of query in the Queries Pane, but they are still a query:

The M Code Behind Power BI Parameters

This means that you can open up the Advanced Editor and look at the M code for the query. Here’s the code for the query shown above:

"Monday"  meta  [ IsParameterQuery=true,  List={"Monday", "Tuesday", "Wednesday",  "Thursday", "Friday", "Saturday", "Sunday"},  DefaultValue="Monday",  Type="Text",  IsParameterQueryRequired=true ]

From this you can see that the value returned by the parameter query is just a single piece of text – it’s the value “Monday” that is set as the Current Value, that’s to say the value returned by the parameter itself. The interesting stuff is all in the metadata record associated with the value. I blogged about metadatahere, so you may want to read that post before going any further; it’s pretty clear that the fields in the metadata record correspond to the values set in the UI. All of the fields in the metadata record can be edited in the Advanced Editor if you want.

When the parameter is used in another query it is referenced like any other query value. For example, if you load the DimDate table from the Adventure Works DW sample database and use the parameter above to filter the EnglishDayNameOfWeek column then the code generated in the UI looks like this:

let     Source =            Sql.Databases("localhost"),     #"Adventure Works DW" =            Source{[Name="Adventure Works DW"]}[Data],     dbo_DimDate =            #"Adventure Works DW"{[Schema="dbo",Item="DimDate"]}[Data],     #"Filtered Rows" =           Table.SelectRows(dbo_DimDate,             each [EnglishDayNameOfWeek] = Day) in     #"Filtered Rows"

The filtering takes place in the #”Filtered Rows” step and you can see where the name of the parameter – Day – is used in the Table.SelectRows() function to filter the EnglishDayNameOfWeek column. This is nothing new in terms of the language itself because you have always been able to return values of any data type from a query, not just tables, and you have always been able to reference queries in other queries like this – in fact you can see me write the same kind of code manually in this video . What is new is that there is now a UI to do this and there’s no need to write any code.

Personally, I think the Power BI team have done a great job here in terms of usability and clearly a lot of thought has gone into this feature. It doesn’t do everything I would want yet though: the ability to bind the list of available values to the output of another query and the ability to select multiple parameter values at the same time are obvious missing features (and ones that would be needed to match the parameter functionality in SSRS). However I would not be surprised to see them appear in a future version of Power BI.

After seeing the code, I wondered whether I could edit the code in the parameter query to make it do more interesting things. For example, even if the UI doesn’t support data-driven lists of available values for a parameter, it looks as though it should be possible to replace the hard-coded list with a list of values returned by another query. Unfortunately this does not work: any changes I tried to the parameter query code were either ignored or removed completely. A bit of a disappointment but again, hopefully this will be possible in a future version.

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » The M Code Behind Power BI Parameters

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址