Canvas Apps Expressions

Number vs Text in If function

This is another one of those weird behaviors that I ran into when I was working on a Power Apps project. It might be a known behavior to some, but it definitely wasn’t to me. So I thought I will share this in case someone else runs into the same issue. The use case? Display a numeric value in a label if an If function was true display a text string when false.

At first, I used the following expression:

If(condition, 007, "James Bond")

To test this out, I added a toggle control. I then used its value as the condition in the above If expression:

If(Toggle1.Value, 007, "James Bond")

As I turned the toggle control on and off, the value in the label was 7 when (the leading zeroes are removed) and nothing when false.

I then changed the expression to the following:

If(!Toggle1.Value, "James Bond", 007)

In this condition, the text in the label was James Bond when Toggle1 was true, and when it was false, 7. Both outcomes were exactly as expected.

What did this mean? I had to be careful in writing my If expressions. If they ever involved text and numbers, I had to choose the text first. And I had to invert the condition if the text was supposed to be the output in the true condition. For example, !Toggle1.Value in the above expression.

My final attempt was this:

If(Toggle1.Value, Text(007), "James Bond")

With this, outcomes in both true and false conditions were as expected.

Take a look at a demonstration of the 3 behaviors listed above:

Behavior when true and else values in a If function are text and numbers
Text vs Number (click to enlarge)

Based on this, my conclusion was that the true value of an If expression “defines” the data type of the outcome. If the else value happens to be of the same data type (as the true value), then the else value will work. One way to get around this is to switch the order of true and else values (and modify the condition accordingly). This will help to have the “broader” data type as the true value. However, if this isn’t possible for any reason, you can explicitly convert the true value data type to the else value data type.

This is simply a conclusion of the behavior I saw while testing this out. Please feel free to correct me with either a better explanation/conclusion or a confirmation that this is simply the expected behavior. Would love to hear from you all! Until then..

Have fun!

1 thought on “Number vs Text in If function”

Leave a Reply