Skip to main content

Advanced data transformation techniques using M language

Power BI's M language, also known as Power Query Formula Language, is a powerful tool for advanced data transformation. It allows you to manipulate and shape your data in ways that go beyond the capabilities of the standard Power BI user interface. Here are some advanced data transformation techniques using M language:


1. Custom Columns and Conditional Logic

You can create custom columns using complex logic. For example, you might want to create a new column based on multiple conditions.

m

Copy code

let

    Source = Excel.Workbook(File.Contents("C:\YourFile.xlsx"), null, true),

    Data = Source{[Name="Sheet1"]}[Data],

    AddCustom = Table.AddColumn(Data, "SalesCategory", each if [SalesAmount] > 500 then "High" else "Low")

in

    AddCustom

2. Pivot and Unpivot Data

M language allows for more complex pivoting and unpivoting of data.

Unpivot Example:

m

Copy code

let

    Source = Excel.Workbook(File.Contents("C:\YourFile.xlsx"), null, true),

    Data = Source{[Name="Sheet1"]}[Data],

    Unpivoted = Table.UnpivotOtherColumns(Data, {"ProductID", "ProductName"}, "Month", "SalesAmount")

in

    Unpivoted

Pivot Example:

m

Copy code

let

    Source = Excel.Workbook(File.Contents("C:\YourFile.xlsx"), null, true),

    Data = Source{[Name="Sheet1"]}[Data],

    Pivoted = Table.Pivot(Data, List.Distinct(Data[Month]), "Month", "SalesAmount", List.Sum)

in

    Pivoted

3. Merging Queries with Custom Join Logic

You can merge queries with custom join logic to combine tables based on more complex relationships.

m

Copy code

let

    Source1 = Excel.Workbook(File.Contents("C:\YourFile1.xlsx"), null, true),

    Table1 = Source1{[Name="Sheet1"]}[Data],

    Source2 = Excel.Workbook(File.Contents("C:\YourFile2.xlsx"), null, true),

    Table2 = Source2{[Name="Sheet2"]}[Data],

    MergedTables = Table.Join(Table1, {"KeyColumn1"}, Table2, {"KeyColumn2"}, JoinKind.FullOuter)

in

    MergedTables

4. Aggregations and Grouping

Perform aggregations and grouping using M language for more control over the operations.

m

Copy code

let

    Source = Excel.Workbook(File.Contents("C:\YourFile.xlsx"), null, true),

    Data = Source{[Name="Sheet1"]}[Data],

    Grouped = Table.Group(Data, {"Category"}, {{"TotalSales", each List.Sum([SalesAmount]), type number}})

in

    Grouped

5. Using Parameters and Custom Functions

Create parameters and custom functions for more dynamic data transformations.

Parameter Example:

m

Copy code

let

    paramYear = 2023,

    Source = Excel.Workbook(File.Contents("C:\YourFile.xlsx"), null, true),

    Data = Source{[Name="Sheet1"]}[Data],

    FilteredData = Table.SelectRows(Data, each [Year] = paramYear)

in

    FilteredData

Custom Function Example:

m

Copy code

let

    myFunction = (x as number) as number => x * x,

    Source = Excel.Workbook(File.Contents("C:\YourFile.xlsx"), null, true),

    Data = Source{[Name="Sheet1"]}[Data],

    AddCustom = Table.AddColumn(Data, "SquaredValue", each myFunction([Value]))

in

    AddCustom

6. Advanced Text Manipulation

M language allows for advanced text manipulations, such as splitting, replacing, and extracting parts of strings.

m

Copy code

let

    Source = Excel.Workbook(File.Contents("C:\YourFile.xlsx"), null, true),

    Data = Source{[Name="Sheet1"]}[Data],

    SplitColumn = Table.SplitColumn(Data, "FullName", Splitter.SplitTextByDelimiter(" "), {"FirstName", "LastName"}),

    ReplaceText = Table.ReplaceValue(SplitColumn, "OldValue", "NewValue", Replacer.ReplaceText, {"ColumnName"})

in

    ReplaceText

7. Error Handling

Handle errors in your data transformation process.

m

Copy code

let

    Source = Excel.Workbook(File.Contents("C:\YourFile.xlsx"), null, true),

    Data = Source{[Name="Sheet1"]}[Data],

    TryOperation = Table.AddColumn(Data, "SafeDivision", each try [Value1] / [Value2] otherwise null)

in

    TryOperation

8. Date and Time Transformations

Manipulate date and time data with M language.

m

Copy code

let

    Source = Excel.Workbook(File.Contents("C:\YourFile.xlsx"), null, true),

    Data = Source{[Name="Sheet1"]}[Data],

    AddDateParts = Table.AddColumn(Data, "Year", each Date.Year([Date])),

    AddMonth = Table.AddColumn(AddDateParts, "Month", each Date.Month([Date])),

    AddDay = Table.AddColumn(AddMonth, "Day", each Date.Day([Date]))

in

    AddDay


Conclusion

Using M language for data transformations in Power BI gives you extensive control over your data preparation processes. These advanced techniques enable you to create dynamic, flexible, and powerful data models that can handle complex scenarios. By mastering M language, you can significantly enhance your data transformation capabilities in Power BI

 


Comments