Last week, one of the issues I ran into as I re-engineered an app, I suddenly ended up with my Power Apps Studio complaining about 100s of incompatible types.

Variables with Incompatible Type in Power Apps
Variables with Incompatible Type in Power Apps 1

Data Types

There are quite a few different data types that exist in Power Apps. In the mentioned article quite a few types are mentioned in the ‘complete list’ However there are a few more. You can for example set a variable to a control on a screen or a screen as well.

If we first have a look at some basic types. The below code will show you how to set a varibale to a Boolean or a Number.

Set(varMyBoolean, true);
Set(varMyNumber, 1234);

But also the following will work.

Set(varCurrentScreen, App.ActiveScreen);

Incompatible Type

Now when you use a single variable to first store one type of data in and later another type of data, then Power Apps will give you an error and your app may fail in unexpected ways. The following error might appear in the Power Apps Studio.

Incompatible type. We can’t evaluate your formula because the context variable types are incompatible with the types of values in other places in your app.

An example of code that will generate the above error is shown below. Notice that the first occurrence of the issue may be flagged with a red line.

Incompatible type error in Power Apps
Incompatible type error in Power Apps

However if we retyped the true in the first line of code. Then the second line shows us the warning. So this means that the code that already existed gives us the error, while the code that we just updated does not give us an error. This will be very confusing as hunting the error reporting lines give you to wrong errors to chase.

Incompatible type error in Power Apps
Incompatible type error in Power Apps
Set(varMyVariable, true);
Set(varMyVariable, 1234);

This is why you should always keep an eye on the app checker. As soon as that red dot appears, try and fix the reported issues.

App checker reporting issues
App checker reporting issues

Last week, I got into a tricky situation as my app had already quite a few errors before I even started the work. then suddenly I had over 1000 errors reported to me. This is where the app checker becomes useless as it is difficult to identify which usage of a specific variable caused the tsunami of errors to appear.

Initialize variables

The earlier example was easy as a Boolean and a Number are different. But how about the example below:

Set(varMyVariable, Blank());
Set(varMyVariable, 1234);

The above code will not give any errors and is fine if you want to initialize a variable to an empty value before setting it later in your app.

If however we want to set a record in a List or Table to an empty record. Then we should consider the following code. The Defaults function is often used in the Patch function, however you can also use it to simply initialise a variable, which is used later.

Using the Defaults function
Using the Defaults function

Some more examples

The below is ok and will not give any errors as both records are records and the two properties are different and therefore there are not conflicts.

Set(varMyVariable, { Property1: "test"});
Set(varMyVariable, { Property2: "test"});

The following gives an error however as a single property within the records is used twice with different types.

Set(varMyVariable, { Property1: "test"});
Set(varMyVariable, { Property1: 1234});

I’ll take that last example a bit further. The following example is a bit unexpectedly throwing two errors. Where earlier we only got one error reported. But obviously a Label and a Button are very different.

Variables with Incompatible Type in Power Apps
Variables with Incompatible Type in Power Apps 2

Naming your variables

In this post I prefixed all variables with var. You could however prefix each variable with the type of that that it is used for. Whichever standard you like is fine, but including the type can avoid a lot of troubles.

  • gbl_txt_Title (global variable of the text type to store a Title)
  • ctx_bln_IsValid (A Boolean Context variable to hold if a record is valid or not)


Discover more from SharePains

Subscribe to get the latest posts sent to your email.

Related Posts

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.