cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
RusselThomas
Microsoft
Microsoft

Why with, why?

This is likely to be an advanced question, but I am curious to see if any of the community or MS members can answer this one.  it has me stumped to the point I suspect it may even be a bug - but I'm always willing to be corrected and learn something new, so...

 

If you consider the If statement construct as follows;

 

If(boolean condition, instruction when condition = true, instruction when condition = false)

 

You can, if you like, have two completely different instructions for when the condition is true, and when it is false, each with completely different outputs.  By this I mean you can have something like this;

 

If(a=b, 
    Collect(myCollection, {Value: "something"}), 
    Launch("www.bing.com"))
)

 

Pretty much any instruction you give PowerApps has an output of some sort, whether you can see it or not.

  • When you Collect(), it outputs a table.
  • When you Launch(), it outputs a boolean value (for those who are curious, this returns True when the Launch function runs properly).
  • and so on...

The above example works perfectly fine if you place it on a button or other behaviour property.

However, if you place that exact same If() function inside a With() function, there is a constraint placed on the If instruction set that requires their outputs to be the same.  In other words, if the first instruction (instruction when condition = true) results in a table, then the second instruction (instruction when condition = false) must also result in a table, otherwise you get a type mismatch error which, for the example above would be "expecting a table".  To illustrate with a slightly different example;

 

With({something: "Something"},
If(a=b, 
    Today(), 
    Launch("www.google.com"))
)

 

This will present an error under the Launch() function saying it expects a date output - because the first instruction results in a date.

Go ahead and check it out yourself - If anyone understands why this is the case, or has a different experience, I'd love to hear from you!

Kind regards,

RT

4 REPLIES 4
TheRobRush
Super User
Super User

Works just fine for me using this context

With({a: "hello",
    b: "hdllo"},
If(a=b, 
    Set(DateNow,Today()), 
    Launch("www.google.com"))
)

 

typing it like above, launched google. changing both hellos to match, set a variable for my date

 

But when I cheanged first portion from a set, to a Collect() then it behaved as you said. Tried a little further by switchting the collect to a loookup and it gave expecting a record. Looks like it may be very specifically limited when tables are used / including records. Maybe a bug.

 

@WarrenBelz @RandyHayes Do either of you know if this behavior is intended or perhaps behaving more liek a bug?

_____________________________________________________________________________________
Like my answer? - Hit that Thumbs Up. Resolved the Issue? - Hit Accept as Solution.
This helps others find solutions to future issues!
JesseV
Frequent Visitor

I attempted the following in 'OnSelect'  of a label 

 

With({something: "Something"};
If( 1 = 2 ;  
    Text(Today()); 
    Launch("www.google.com"))
)

 

And it worked fine. I'm still very new in PowerApps but what usually happens to me when bugfixing is that I did silly things in completely other areas of the script.

 

It's not something with the 

a=b

variables ? 

timl
Super User
Super User

Hi @RusselThomas 

Thanks for raising this issue.

I would like for this behaviour to be acknowledged and treated as a bug by Microsoft. It doesn't make sense why a call to If should behave differently when inside With, and the documention makes no reference to the If function not supporting different return values in the true and false branches.

https://docs.microsoft.com/en-gb/power-platform/power-fx/reference/function-if

 

This behaviour causes much confusion and I've answered a few posts on this topic, such as the one below. Here, you can see how this behaviour also applies to Concurrent, and also the hack I use to return a boolean on both branches of the call to If.

https://powerusers.microsoft.com/t5/Building-Power-Apps/Strange-behaviour-of-Concurrent-function-and...

 

 

RusselThomas
Microsoft
Microsoft

Thanks @timl , @TheRobRush and @JesseV  for testing and responding.

Interesting to see similar behaviour in Concurrent().  Nesting If()'s doesn't have the same effect so I guess it's something to do with the if statement itself and how it behaves within other functions - perhaps some scoping type constraint weirdness? 

I tried Switch and it worked fine....

 

By the way, it's not just tables that have this issue - the following also does not work;

 

With({something: "Something"},
If(1=2, 
    Today(), //Date output
    Launch("www.bing.com") //boolean output
    )
)

 

 

And if you tested and didn't see the error, you were most likely wrapping a function with one type of output inside another function with another type of output - which just means the outer function type is evaluated instead of the inner, so yes, you can work around this problem by matching the outputs of outer functions to previous true statements, like the following - which works fine;

With({something: "Something"},
If(
    1=2, Collect(myCollection, {Value: "something"}), //Collect() has a table output
    2=3, FirstN(ComboBoxSample,2),                    //FirstN() has a table output
    3=4, Filter(ComboBoxSample, Value1="something"),  //Filter has a table output
    
    //with the boolean output wrapped inside a table output
    Collect(collectSomething, Launch("www.bing.com")) 
    )
)

This will not throw any errors - likewise you can do the inverse, i.e. matching the true statement output to the false statement output by wrapping it;

With({something: "Something"},
If(1=2, 
    Set(myVar, Collect(myCollection, {Value: "something"})), //Set() has a boolean output
    Launch("www.bing.com")                                   //Launch() has boolean output
    )
)

but working around it isn't really the idea I'm looking for... I'm just trying to understand why the following doesn't work, when it seems logical that it should...

With({something: "Something"},
If(
    1=2, Collect(myCollection, {Value: "something"}), //table output
    2=3, Launch("www.bing.com")                       //boolean output
    3=4, Today()                                      //date output
  )
)

I guess at this point I'm leaning towards 'unwanted behaviour', so I'm still curious if anyone else picks up on this from the product team. 

Thanks again for your feedback!

Kind regards,

RT 

Helpful resources

Announcements
Microsoft 365 Conference – December 6-8, 2022

Microsoft 365 Conference – December 6-8, 2022

Join us in Las Vegas to experience community, incredible learning opportunities, and connections that will help grow skills, know-how, and more.

Difinity Conference 2022

Difinity Conference 2022

Register today for two amazing days of learning, featuring intensive learning sessions across multiple tracks, led by engaging and dynamic experts.

European SharePoint Conference

European SharePoint Conference

The European SharePoint Conference returns live and in-person November 28-December 1 with 4 Microsoft Keynotes, 9 Tutorials, and 120 Sessions.

Power Apps Ideas

Changes to Ideas Coming

We are excited to announce a new way to share your ideas for Power Apps!

Top Solution Authors
Top Kudoed Authors
Users online (3,788)