0
0
Power-biHow-ToBeginner ยท 4 min read

How to Change Data Type in Power Query in Power BI

In Power BI's Power Query, you change a column's data type by selecting the column and using the Data Type dropdown in the ribbon or by applying the Table.TransformColumnTypes function in M code. This changes how Power BI reads and processes the data for that column.
๐Ÿ“

Syntax

The main M code syntax to change a column's data type is:

Table.TransformColumnTypes(table as table, typeTransformations as list, optional culture as nullable text) as table

Where:

  • table is your data table.
  • typeTransformations is a list of pairs: each pair has a column name and the new data type.
  • culture is optional and controls locale-specific conversions.
m
Table.TransformColumnTypes(Source, {{"ColumnName", type text}})
๐Ÿ’ป

Example

This example changes the data type of the column Age to Int64.Type and DateOfBirth to Date.Type in a sample table.

m
let
    Source = Table.FromRecords({
        [Name="Alice", Age="30", DateOfBirth="1992-05-01"],
        [Name="Bob", Age="25", DateOfBirth="1997-08-15"]
    }),
    ChangedTypes = Table.TransformColumnTypes(Source, {{"Age", Int64.Type}, {"DateOfBirth", Date.Type}})
in
    ChangedTypes
Output
[ {"Name": "Alice", "Age": 30, "DateOfBirth": #date(1992, 5, 1)}, {"Name": "Bob", "Age": 25, "DateOfBirth": #date(1997, 8, 15)} ]
โš ๏ธ

Common Pitfalls

Common mistakes when changing data types in Power Query include:

  • Trying to convert text with invalid format to numbers or dates, which causes errors.
  • Not applying the change step properly, so the data type does not update.
  • Forgetting to select the correct column name or misspelling it in the M code.

Always check your data format before changing types and preview the results.

m
/* Wrong: Trying to convert text 'abc' to number causes error */
let
    Source = Table.FromRecords({[Value="abc"]}),
    ChangedType = Table.TransformColumnTypes(Source, {{"Value", Int64.Type}})
in
    ChangedType

/* Right: Ensure text is numeric before conversion */
let
    Source = Table.FromRecords({[Value="123"]}),
    ChangedType = Table.TransformColumnTypes(Source, {{"Value", Int64.Type}})
in
    ChangedType
๐Ÿ“Š

Quick Reference

ActionDescriptionExample
Change data type via UISelect column, then choose type from Data Type dropdownSelect 'Age' column โ†’ choose 'Whole Number'
Change data type via M codeUse Table.TransformColumnTypes with column and type pairsTable.TransformColumnTypes(Source, {{"Age", Int64.Type}})
Common data typesUse built-in types like Int64.Type, Text.Type, Date.TypeInt64.Type for numbers, Text.Type for text
Handle errorsCheck data format before conversion to avoid errorsClean or filter data before type change
โœ…

Key Takeaways

Use the Data Type dropdown in Power Query UI or Table.TransformColumnTypes in M code to change data types.
Always verify your data format matches the target type to avoid conversion errors.
Specify the correct column name and data type when using M code.
Changing data types helps Power BI understand and process your data correctly.
Preview changes in Power Query to confirm data types updated as expected.