Advanced editor and Power Query: keep your code clean!

Advanced editor and Power Query: keep your code clean!

Hello!

Hope you are doing well!

In today's article I would like to talk about documenting your work within Power BI and quality of life improvements. From my point of view common situation in IT projects is like:

"Documentation, everyone needs it, nobody likes to write it."

Subjects I'll write about:

  • Folders for your queries
  • Parameters
  • Commenting Power Query steps
  • Naming convention for Power Query steps

Folders for your queries

As we all know, Power BI works better with star or snowflake schema. So we need facts and dimensions. Let's take a look at screen below with a couple of tables loaded in advanced editor:

queries

Luckily for me, there is a prefix saying if it's fact or a dimension table.

What in case we would have 20-30 tables? Plus subtables for append/merge purpose. Then we would have quite a long list of queries!

Answer to this might be creating folders.

Simply, right click on query -> move to group -> new group:

new group for queries

Additionaly, within group itself you can drop a few words of description:

new group creation panel

After few clicks we can have such view:

grouped queries

One downsize of this is that "other queries" group is always automatically created and you cannot remove it.

Of course, same as folders on desktop, you might have hierarchy of groups. So it is easier to manage. I can remove "fact"/"dim" prefix in case I don't want to keep it. With folders it's still easy to read and understand.

hierarchy of groups

If you upload changes, within Power BI desktop "data" panel you will see just tables. So the grouping is good for load and transform work in back-end of the report.

data panel - tables ready to consume

Parameters

Parameters are specific objects within Power BI that can help you on Power Query level to automate work, for example:

  • setup incremental refresh (I was writing about it few articles ago),
  • prepare queries to change data source - for instance from dev environment to test or production,
  • create lists that can be used in other power query steps or functions,
  • parameter itself can be separate query used in custom scenarios.

To create parameter, simply right click on query pane (or the group where you want to store it). Based on info within folders for your queries, I already created separate group for parameters.

new parameter

Once you click new parameter, then you have a lot of options to choose from:

  • Name - I like to put prefix of "p" as parameter and also keep "_" within name. We will be using it in query itself. M Query does not like spaces in object names.
  • Description - free text box, this can be a place where you can put for instance owner of parameter or business logic/reason behind it,
  • Required - to be honest, I never had a scenario where I had to change this option, let me know in the comments if you have some business scenarios for this,
  • Type - you can choose how paramter value will be stored, whether it's text, date, number or other common formats
  • Suggested Values - here you can toggle between "list of values", "query" or "any value". Depending on the option you will choose, additional boxes may appear at the bottom,
  • Current Value - default value. In our scenario it will be a path to folder with excel file I used for this example.

parameter panel

Let's now look on our queries. Every query is a connection to excel table within the same folder, the same excel. So whole path is common.

folder and excel path

As you can see, in excel connector (and many others) path is a text value. So our parameter "p_folder_path" also needs to be a text type. Here's the parameter I'll be using to automate queries:

path parameter

Type is changed to "text" and current value is the excatly same path I was showing in above screenshot.

For each query, I can swap source text value with the parameter. So in case I would have 20 queries and suddenly source path is changing, I only need to update parameter. You can also change parameter value from Power BI Service API.

p_folder_path instead of hardcoded text

Common case is that usually you import data from multiple excel files. So it's just a matter of editing text within parameters and you can have something like:

combined path

In few words: you are joining different strings of text into one. Just remember to update "current value" within parameters accordingly. Make sure that parameters combined, create valid path to folder and excel file.

Commenting Power Query steps

I find ability to comment in any programming language very powerful. This is the place where we, simple developers can put business context. Why we did certain things in the code. Or why we used this specific workaround? Maybe there're some specific cases we need to take under consideration?

In M Query, if you put /* and then */ everything between these 2 marks is ommited when code is compiling. Alternatively you can use // for quick comments within code.

Example of query inroduction plus few comments - definitely would help in future developing or troubleshooting:

general comments

Another approach I like to take is to split query steps between data load and data transformations. So instead of this within query:

default applied steps

We can have something like that:

split by load and transform

I believe it provides cleaner way to look at applied steps.

M Query itself looks like that:

enhanced query

You just need to remember about M Query rules. Usually each step refers to step above. But this is topic for the whole separate article.

Important: If you need query folding, then instead of doing #"===SET SOURCE===" = 1 you could rename the 1st step itself. So we can have clear information in applied steps and query folding at work:

set source instead of source step


Additional note: when I write M Query manually, I usually put comma at the start of line. This specific code was generated automatically, so comma is always at the end of line. The more M Query you will write on your own, the more differences you will start to notice.

Naming convention for Power Query steps

Last item I would like to share is to keep your applied steps clean. Based on this I cannot really tell what's going on. Unless I check details behind the steps.

applied steps with default names

For instance "filtered rows"? What is filtered? Where and why?

Instead we could write something like:

renamed steps

This way we have some understanding of what's going on. If we need more input, for instance we want to know excatly how the date column was filtered, we can add extra comment ("i" icon on the right). So with hover over we can check the note:

comment

And this is how it looks in M Query:

steps and comments

As I mentioned, M Query doesn't like spaces. So if we are referring to above step, we need #"step name" syntax. Another solution might be to use underline sign:

using underlines

This way, you can refer to certain step faster, if you are writing M Query on your own. Usually I also use prefix of "s_" so M Query intellisense gives me only list of steps to choose from, instead of all methods, functions and other fancy items.

Overall, this is how M Query code should look for our example of Sales table. Of course on top we have (lines 1-7) we have overall query documentation, but the whole window didn't fit in screen capture.

example of clean code

That's all for today!

Let me know if you have any questions or comments!

Hope you have a great week ahead!

























Robert Johnson

Programmer / Report Writer / Advancement data professional at Stockton University

1 年

Great post. I have done some of the things you talk about in the power query I create. Thanks to you I have a few more good tips. ?? Thanks for sharing.??

要查看或添加评论,请登录

Micha? Zalewski的更多文章

社区洞察

其他会员也浏览了