DEV Community

Cover image for Power Query Data transformations - getting started... quickly
Kinga
Kinga

Posted on

Power Query Data transformations - getting started... quickly

If you have never worked with Power Query before, check additional resources at the bottom of the page.

Power Query is a powerful data connection, cleaning, and shaping technology that can be used in Excel and Power BI alike. The content retrieved from external sources is transformed using Power Query M language, which offers a set of functions for working with tables, lists, dates, etc.

When adding steps using User Interface, Power Query Editor will automatically add and configure steps. You can edit them instead of adding new step correcting the results.

Update generated steps

Selecting columns

When selecting columns from a data source, the generated step will use Table.SelectColumns function. If the column you defined is missing, you will receive an error and the query will fail. Adding UseNull ensures that the query will execute successfully and the values in the missing column will be set to null.

Image description

Expanding columns

If you expand multiple columns, the new column names will be predefined:

predefined column names

Instead of renaming them in the following step, edit the Table.ExpandTableColumn formula.

Updated formula

Taking a sneak-peek

When working with lists or table column types, you may take a sneak-peek by clicking within the cell, next to the value. The data preview will be displayed at the bottom of the window.

Preview cell  contents

Merging steps

When renaming multiple columns, the Power Query editor will automatically merge them into one step. In some cases, however, separate steps are added.

For example, if you want to replace multiple text occurrences using UI

Image description

multiple steps will be generated.

Image description

I like copying formula of the step above into the step below to merge text transformations into one step.

Step Replaced a with A:

Step1

Step Replaced b with B

Step 2

I update the Replaced b with B to replace a reference to Replaced a with A with its formula:

Image description

After deleting the no longer needed step Replaced a with A and renaming step Replaced b with B, my transformations are much more readable.

Image description

Working with functions

In case you are applying the same transformation to multiple columns, you may want to save them as a function, instead of copying the formula over, and updating it in multiple steps.

Functions scoped to a query

To create a new private function, open Advanced Editor, declare a new function, define input parameters, and copy the transformations from the step. I personally like defining functions at the very beginning, above the source.

Image description

Update the function body to use parameters instead of hardcoded parameters

Image description

And replace the formula in the step

Image description

You will now see your function above the "Source" step. The navigation step is now renamed to an ID, but you can rename it.

Reusable functions

Functions defined in the scope of a Query are private for this query. To define global functions that can be reused, create a new blank query and paste the copied code to the formula.

Image description

Rename it to something representative of the actions it executes:

Image description

You can now use it in the query steps, just like the local functions you referenced before.

Image description

For more information about Power Query M functions, see Understanding Power Query M functions

Parameters

When referencing the step in the Power Query M functions, the data provided by previous steps is of type table.

For more details on accessing values of list or table items, see Item Access. If the column name is provided as parameter, you may use Record.Field (Record functions)

Passing current cell as parameter:

Image description

Passing current row as parameter using _:

Image description

Image description

Make sure that for any cells that may have null value, you define the corresponding parameter as nullable:

Image description

For more information about Power Query M types, see Types.

Best kept secret

To access all functions directly from your Power Query editor, create new blank query and in Advanced Editor type = #shared

Image description

The result:

Image description

After clicking next to the Function, you will see its documentation, along with usage example.

Image description

And clicking on the Function will allow you to test it:

Image description

Source: Data Preparation in Power BI By Reza Rad

Additional resources

⭐ You will find the Power Query M functions used in my Power Platform cost optimization dashboard in the Power BI functions gist

Top comments (0)