Type.ReplaceFacets

D

T

The M Code Behind the Power Query M function Type.ReplaceFacets

What is Type.ReplaceFacets?

Type.ReplaceFacets is a Power Query M function that is used to modify the data type of a column. It is used when you need to change the data type of a column, but also want to apply additional constraints or facets to the new data type. For example, you may want to change a column from a number to a whole number, or from a text string to a date.

Understanding the Syntax

The syntax for Type.ReplaceFacets is as follows:


Type.ReplaceFacets(type, facets)


The first argument, “type”, specifies the data type you want to change to. This can be any valid data type in Power Query, such as “number”, “text”, or “date”. The second argument, “facets”, is an optional parameter that allows you to specify additional constraints on the new data type. These constraints can include things like minimum or maximum values, precision, or scale.

Examples

To better understand how Type.ReplaceFacets works, let’s explore some examples.

Example 1: Changing a Column to a Whole Number

Suppose you have a column of data that contains decimal values, but you want to convert it to a whole number. You can use Type.ReplaceFacets to accomplish this. Here’s the M code:


= Table.TransformColumnTypes(

#"Previous Step",

{

{"Decimal Column", Type.ReplaceFacets(type nullable number, [IsWholeNumber=true])}

}

)


In this example, we are using Table.TransformColumnTypes to change the data type of a column called “Decimal Column”. We are specifying the new data type as “number”, but we’re also using the “facets” parameter to specify that the new data type should be a whole number.

Example 2: Changing a Column to a Date

Suppose you have a column of data that contains text strings representing dates, but you want to convert it to an actual date data type. You can use Type.ReplaceFacets to accomplish this. Here’s the M code:


= Table.TransformColumnTypes(

#"Previous Step",

{

{"Text Date Column", type text},

{"Date Column", each Date.FromText(_, "MM/dd/yyyy")}

}

)


In this example, we are using Table.TransformColumnTypes to change the data type of a column called “Text Date Column”. We are specifying the new data type as “text”, because we need to convert the text strings to actual dates. We’re then using the “facets” parameter to specify that the new data type should be a date, and we’re using the Date.FromText function to convert the text strings to dates.

Type.ReplaceFacets is a powerful function in Power Query that allows users to modify the data type of a column while also applying additional constraints or facets. By understanding the M code behind this function, users can take full advantage of its capabilities and transform their data more efficiently.

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)