Basics
M Type Conversion
Converting Data Types
M type conversion uses functions like Number.FromText.
Introduction to Type Conversion in M
Type conversion in M, a language used in Microsoft Power Query, involves transforming data from one type to another. This is crucial when manipulating data imported from various sources, ensuring consistency and correct functionality in data analysis operations. M provides several built-in functions to facilitate this process, with Number.FromText being one of the most commonly used.
Converting Text to Numbers
When working with data, you might encounter numbers stored as text. The Number.FromText
function is used to convert these text-based numbers into numeric data types, allowing for mathematical operations.
In the example above, the text "123.45"
is converted to the numeric value 123.45
. This allows you to perform calculations and other numeric operations on the converted value.
Handling Errors in Conversion
Type conversion can sometimes fail if the text format is not compatible with the target data type. M provides error handling mechanisms to manage such scenarios, allowing you to ensure your script continues to function smoothly.
In this example, try
is used to attempt the conversion. If Number.FromText
fails (as it would with "abc"
), the otherwise
clause provides a default value of 0
.
Converting Other Data Types
Besides converting text to numbers, M also offers functions for other types of conversion such as converting text to dates or logical values. Functions like Date.FromText
and Logical.FromText
are used similarly:
Here, Date.FromText
converts a date string into a date data type, while Logical.FromText
converts a text representation of a boolean into a logical value.
Basics
- Previous
- Data Types
- Next
- Operators