Value.RemoveMetadata

D

T

The M Code Behind the Power Query M function Value.RemoveMetadata

In this article, we will explore the M code behind the Value.RemoveMetadata function, how it works, and how it can be used to improve data processing in Power Query.

Understanding Metadata in Power Query

Before we dive into the M code behind the Value.RemoveMetadata function, it’s important to understand what metadata is and how it affects data processing in Power Query.

Metadata refers to information that describes the structure, content, or context of data. In Power Query, metadata is often generated automatically when importing data from different sources, such as Excel spreadsheets, CSV files, or databases. This metadata can include column names, data types, formatting, and other attributes that are used to define the structure of the data table.

While metadata can be useful for understanding and manipulating data, it can also add unnecessary overhead and complexity to data processing in Power Query. For example, metadata can slow down queries, increase file size, and make it harder to perform certain operations on the data.

To address these issues, Power Query provides the Value.RemoveMetadata function, which allows you to selectively remove metadata from data tables, while preserving the underlying data values.

The Syntax of Value.RemoveMetadata

The Value.RemoveMetadata function is a simple yet powerful M function that takes a data table as its input, and returns a new data table with the same data values, but without any metadata.

The syntax of Value.RemoveMetadata is as follows:


Value.RemoveMetadata(data as any) as table


Here, “data” is the input data table, which can be of any type, such as table, list, or record. The function returns a new table with the same data values, but without any metadata.

For example, let’s say we have a data table with the following metadata:


let

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

Sheet1_Sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data],

#"Promoted Headers" = Table.PromoteHeaders(Sheet1_Sheet, [PromoteAllScalars=true]),

#"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Order Date", type date}, {"Region", type text}, {"Category", type text}, {"Sales", Currency.Type}})

in

#"Changed Type"


This code imports an Excel file, promotes headers, changes data types, and generates metadata for the resulting table. To remove the metadata, we can simply apply the Value.RemoveMetadata function as follows:


let

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

Sheet1_Sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data],

#"Promoted Headers" = Table.PromoteHeaders(Sheet1_Sheet, [PromoteAllScalars=true]),

#"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Order Date", type date}, {"Region", type text}, {"Category", type text}, {"Sales", Currency.Type}}),

#"Removed Metadata" = Value.RemoveMetadata(#"Changed Type")

in

#"Removed Metadata"


This code applies the Value.RemoveMetadata function to the output of the Table.TransformColumnTypes step, resulting in a new table with the same data values, but without any metadata.

When to Use Value.RemoveMetadata

The Value.RemoveMetadata function can be useful in a variety of scenarios, such as:

– Performance optimization: Removing metadata can improve the performance of queries and reduce memory usage, especially for large data sets.

– Simplifying data analysis: Removing metadata can make it easier to filter, manipulate, and visualize data, by reducing clutter and focusing on the essential data values.

– Data privacy: Removing metadata can help protect sensitive information, such as column names or formatting, from being exposed to unauthorized users.

However, it’s important to use Value.RemoveMetadata selectively and with caution, as removing essential metadata can lead to unexpected results or errors in data processing.

For example, if a query depends on a specific data type or column name, removing that metadata can cause the query to fail or produce incorrect results. Similarly, if a query requires detailed formatting information, removing that metadata can make it harder to interpret or present the data.

Therefore, it’s recommended to use Value.RemoveMetadata only when necessary, and to carefully test and validate the output of the function before applying it to production data.

The Value.RemoveMetadata function is a powerful tool for manipulating and transforming data in Power Query. By removing extraneous metadata from data tables, it can improve performance, simplify analysis, and protect sensitive information. However, it’s important to use Value.RemoveMetadata selectively and with caution, and to validate the output of the function before applying it to production data.

Power Query and M Training Courses by G Com Solutions (0800 998 9248)

Upcoming Courses

Contact Us

    Subject

    Your Name (required)

    Company/Organisation

    Email (required)

    Telephone

    Training Course(s)

    Your Message

    Upload Example Document(s) (Zip multiple files)