Value.ReplaceMetadata

D

T

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

What is Power Query M?

Power Query M is a functional programming language used in Power Query, a data transformation and data preparation tool in Microsoft Excel and Power BI. It is used to connect to various data sources, transform and shape data, and load it into an output destination. Power Query M is a powerful tool in data analytics, as it allows users to perform complex data transformations with ease.

Understanding Value.ReplaceMetadata

In Power Query M, metadata is data that describes other data. It provides information about the structure and content of data, such as column headers and data types. The function Value.ReplaceMetadata is used to replace the metadata of a value in M.

The syntax for Value.ReplaceMetadata is as follows:


Value.ReplaceMetadata(value as any, metadata as any) as any


The first parameter, `value`, is the value whose metadata needs to be replaced. The second parameter, `metadata`, is the new metadata that needs to be applied to the value. The function returns a new value with the updated metadata.

The M Code Behind Value.ReplaceMetadata

The M code behind Value.ReplaceMetadata is relatively simple. The function takes two parameters, `value` and `metadata`, and returns a new value with the updated metadata. Here is the M code for Value.ReplaceMetadata:


(Value as any, Metadata as any) =>

let

OriginalType = Value.Type,

OriginalNullable = Value.IsNullable,

OriginalKind = Value.Kind,

OriginalMetadata = Value.Metadata,

ReplaceType = if (Type.Is(Value.Type, type function)) then Value.Type else Metadata.Type,

ReplaceNullable = if (Type.Is(Value.Type, type function)) then Value.IsNullable else Metadata.IsNullable,

ReplaceKind = if (Type.Is(Value.Type, type function)) then Value.Kind else Metadata.Kind,

ReplaceMetadata = if (Type.Is(Value.Type, type function)) then Value.Metadata else Metadata.Metadata

in

Type.ReplaceMetadata(

Type.ReplaceKind(

Type.ReplaceNullable(

Type.ReplaceType(

Value.Type,

ReplaceType

),

ReplaceNullable

),

ReplaceKind

),

ReplaceMetadata

) meta OriginalMetadata


The M code starts by defining the function parameters, `Value` and `Metadata`. It then defines four variables to store the original type, nullable value, kind, and metadata of the value.

It then replaces the original type, nullable value, kind, and metadata with the new values provided in the `Metadata` parameter, unless the value is a function. If the value is a function, the original type, nullable value, kind, and metadata are retained.

The M code then uses the `Type.ReplaceMetadata`, `Type.ReplaceKind`, `Type.ReplaceNullable`, and `Type.ReplaceType` functions to replace the original type, nullable value, kind, and metadata with the new values.

Finally, the M code returns the updated value with the original metadata in the `meta` field.

Using Value.ReplaceMetadata Effectively

Now that we have explored the M code behind Value.ReplaceMetadata, let’s explore how it can be used effectively in Power Query M.

One common use case for Value.ReplaceMetadata is when merging tables with different metadata. For example, if you have two tables with the same column names but different data types, you can use Value.ReplaceMetadata to change the data types of one of the tables so that they match.

To do this, you would first merge the tables using the Merge Queries function. You would then use Value.ReplaceMetadata to change the data types of the columns in one of the tables so that they match the data types in the other table. This would ensure that the merged table has consistent metadata.

Another use case for Value.ReplaceMetadata is when working with JSON data. JSON data often contains nested structures, which can make it difficult to work with in Power Query M. By using Value.ReplaceMetadata to flatten the nested structures, you can make the data easier to work with and analyze.

In conclusion, Value.ReplaceMetadata is a powerful function in Power Query M that can be used to replace metadata of a value. By understanding the M code behind this function and how it can be used effectively, you can take advantage of this powerful tool in your data analytics projects.

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)