domingo, 8 de enero de 2017

Step Descriptions And Comments In M Code In Power BI Desktop

The June release of Power BI Desktop has what seems to be a fairly unremarkable new feature in that it allows you to add descriptions to each step in a query in the Query Editor window. However the implementation turns out to be a lot more interesting than you might expect: the step descriptions become comments in the M code, and even better if you write M code in the Advanced Editor window your comments appear as descriptions in the Applied Steps pane.
Take the following M query, entered in the Advanced Editor, as an example:
1
2
3
4
5
6
let
    Step1 = 5,
    Step2 = 10,
    Step3 = Step1 * Step2
in
    Step3
There are three variables declared in the let expression which appear as three steps in the Applied Steps pane. The first two steps declare integers and the third multiplies these two integers together, returning 50.
If you right-click on the first step and select Properties, then you can enter a description for the step in the Properties pane that appears:
image
image
After you click OK, the description is visible as a tooltip when you mouse-over the step:
image
If you then open the Advanced Editor window you’ll see the M code for the query has now been changed to include a comment (NB comments in M code start with //):
1
2
3
4
5
6
7
let
    // Declare the first number
    Step1 = 5,
    Step2 = 10,
    Step3 = Step1 * Step2
in
    Step3
image
If you then edit the M code in the Advanced Editor window to add a comment in the line before a step, like so:
1
2
3
4
5
6
7
8
let
    // Declare the first number
    Step1 = 5,
    Step2 = 10,
    // Multiple the two numbers together
    Step3 = Step1 * Step2
in
    Step3
…then this will also show up as a description when you mouse-over the step in the Applied Steps pane:
image

As a result, for anyone like me who writes a lot of M code manually in the Advanced Editor window, this turns out to be a really handy feature.