'Coerce Logical to Numeric in PowerBI

I am creating a new column in PowerBI and would like to return a column with 1s or 0s depending on whether a logical condition is true or false. I would have expected the following to work (as it is similar to type coercion in Excel and other languages)

= 1*([Value] < [Threshold])

However, it returns an error:

Expression.Error: We cannot apply operator * to types Number and Logical.
Details:
    Operator=*
    Left=1
    Right=TRUE

I have got the result I want by returning a logical value and then manually converting the type, however this seems inefficient. How would the above be accomplished with just a formula?



Solution 1:[1]

If you're adding the column using DAX, then you can use the INT function:

= INT ( [Value] < [Threshold] )

If you're using M (Power Query) then you can use the Number.From function:

= Table.AddColumn(#"Previous Step", "My Column", each Number.From([Value] < [Threshold]), Int64.Type)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1