Incompatible Type errors are like apples and pears. Things just aren’t all the same and you can expect to get some troubles.
Data Types in Power Apps
Power Apps will display the type of the variables that you have with the overview of your variable.
The type of the variable is set when you set the variable to a value. You will even find that when you set a variable that the type may change. This is exactly the problem that I’m looking at in this post. You can change the type of the variable and when this happens by accident you might break some code in other places.
Just to be clear it is not a good idea to use the same variable for different types of data!
Error Type Variables
Ok, there isn’t really a type of variable called Error. But in the same place as where Power Apps would tell you that you have a text variable it will also tell you that your variable has an error.
Incompatible Type error
When you look in your app you might find a perfectly valid expression showing you the red lines.
And even when you look at other places where this variable is used you will find the similar red line.
Especially in the beginning when you just started with Power Apps development and you don’t follow any standard of naming your variables it is quite common to see these kind of errors. Solving these kind of problems can be tricky.
You might even look at the app checker and find yet the same error back. But none of the errors actually look wrong. This is one of those moments where you might think that it was working yesterday and it stopped today.
Fixing Errors with Variables
So how do we go about fixing these errors? I will go back to the Error message of var1.
It may not be immediately obvious but you can click on the variable. This will get you straight through to the definitions of the var1.
What is a definition?
A Definition is every piece of code that sets (and potentially defines your variable).
Now looking at the above code the problem is easily solved. In the first definition I’m setting my variable to “1” and then in the second definition I’m setting the variable to 0. So we’re getting confused between the number data type and the text data type.
So what caused the problem in my case?
I created a button that set my var1 to 0, where everywhere else in my app I was storing my data in var1 as a text. Once I clicked on my button and Power Apps reported a lot of errors the problems were unclear as the place where I made the mistake didn’t report an error. So be careful with your data types.
In this post we have seen that Power Apps can be very helpful in accepting any kind of data and identifying the type of data, however this can cause major issues in your code. When you use variables in Power Apps it can be a good idea to prefix every variable with the type of data that it holds.
So for example txtVariable or numVariable is better than Variable1 and Variable2. Of course you also want to name your variables a bit better so that they describe their contents or purpose. All of these steps will avoid you running into the Incompatible Type errors when you develop your apps.