Forum Discussion
Understanding DAX operator overloading
Hi pmscorca
DAX doesn't support operator overloading in the traditional object-oriented sense, but it does define different behaviors for the same operator based on the operand types.
For example, + / - works for numberical data types and also for adding days to a date.
However, DAX doesn't use + for strings – it uses & for string concatenation.
If this post helps, then please consider Accepting it as the solution to help the other members find it more quickly
Ok, but does the operator result depend on the operator data type (or better the expected data type of the operands) or the real data type of the operands?
The + operator expects numeric operands; if numeric strings are used, this operator continues to return a numeric result.
The & operator expects alphanumeric operands; if numeric strings are used, this operator raises an error.
It seems that it drives the expected operand data type without overriding the operator behaviour, isn't it?
- Ritaf19831 year agoSuper User
Hi pmscorca
It also depends on what’s on the other side of the operator.
For example, if you try to use the+operator between a number and a text string, you'll get an error.
But if the number is actually stored as text, and you try to concatenate it with another string, the engine treats both operands as text and performs concatenation.
So the operator behavior depends not only on the expected data type but also on the actual data types involved – and sometimes the result is determined by how the engine coerces types based on context.If this post helps, then please consider Accepting it as the solution to help the other members find it more quickly.
- pmscorca1 year agoKudo Kingpin
The addition (+ operator) between a number and a numeric string returns a numeric value according to the MS documentation: DAX tries to convert the numeric string in a number, because the expected operands of the additions have to be numeric; a such conversion is performed successfully because the actual numeric string can be converted in a number. For a text string as an addition operand, this implicit conversion fails.
So, a DAX operator respect to the expected operand data types affects the operator result and the operator behaviour doesn't change respect to the actual operand data types.
I think that in DAX it has no sense to talk about operator overloading: the behaviour of + operator doesn't change respect to actual text operands because it expects numeric operands.
Thanks
- lbendlin1 year agoSuper User
Here's a fun one. Calculated column, but works with measures too
Column =var y = 2025return "FY" & y