The CONVERT function converts an expression from one type to another.
CONVERT(
expression,
datatype
)
- expression: Any valid expression in DAX.
- datatype: Type to apply to the previous expression.
The CONVERT function returns the indicated expression with the new type.
The types available are:
- INTEGER: Integer number.
- DOUBLE: Real number.
- STRING: Text string.
- BOOLEAN: Boolean (True/False).
- CURRENCY: Real number with four decimal places.
- DATETIME: Date and time.
The function returns an error message when it cannot convert the expression to the indicated type.
We can convert a date to its equivalent as a whole number with the following expression:
Date to Int = CONVERT(DATE(2019, 10, 28), INTEGER)
If we take the result to a "card" type display, we obtain the following:
We can convert the same date into a text string with the following expression:
Date to Str = CONVERT(DATE(2019, 10, 28), STRING)
The result is as follows:
If we convert a real number to an integer, it is rounded if necessary:
Float to Int = CONVERT(4.5, INTEGER)
When converting texts to numbers, if the text can be converted correctly, the function returns the expression with the specified type:
Text to Int = CONVERT("7", INTEGER)
If it is not possible -because the string contains characters not compatible with the number format-, the function returns an error message:
Text to Int = CONVERT("7a", INTEGER)