Skip to main content

Notifications

Community site session details

Community site session details

Session Id : 6M7L19aQFEJmWTEZClHoiA
Power Apps - Building Power Apps
Unanswered

Tip: Switch(true)

Like (1) ShareShare
ReportReport
Posted on 10 May 2021 09:55:34 by 364

Morning all,

 

So I do some work with Power BI, and a recent development over there is that you can trick SWITCH() into performing as a nested IF statement. 

 

See this article here for more details:

 

https://p3adaptive.com/2015/03/the-diabolical-genius-of-switch-true/ 

 

The beauty of this is that you can create nested IF statements (that equate to True!) in a format that is much easier to read. And it turns out you can use this same functionality in PowerApps with Switch(true, your conditions and results here). As an example, this is me using it to runs a very complicated Filter over a gallery based on buttons pressed:

 

Switch(true,

//Category Filter
Not(IsBlank(areaCat)),
 Filter(
 faqCol,
 Or(
 areaCat in Type,
 areaCat in 'Query On')),

//Filter when Item is selected
Not(IsBlank(suggested)),
 LookUp(
 faqCol,
 suggested.Title in Title),

//Show all Items
Or(IsEmpty('Search Input'),IsBlankOrError('Search Input')), 
 faqCol, 

//Filter based on search input
Filter(
 faqCol,
 Sum(
 ForAll(
 Split('Search Input'.Text, " "),
 If(
 Or(
 Result in Ungroup(ForAll(Split('Query Area',";"),Split(Result," ")),"Value"),
 Result in Ungroup(ForAll(Split('Query On',";"),Split(Result," ")),"Value"),
 Result in Ungroup(ForAll(Split(Title," "),Split(Result," ")),"Value")),
 {Count: 1},
 {Count: 0}
 )
 ),
 Count
 ) > 0
))

 

As a nested IF statement this was previously a nightmare to read. I mean, it still is, but hopefully you get the picture!

 

I don't know if this is a bug or if there are any problems with using SWITCH in this manner, but I hope it helps someone!

  • RandyHayes Profile Picture
    76,287 Super User 2024 Season 1 on 10 May 2021 at 13:40:25
    Re: Tip: Switch(true)

    @EpicTriffid 

    On issue that I see with your formula is that you are returning dissimilar types.  In the first condition (and 3 and 4) you are returning a Table, in condition 2, you are returning a Record.  Although a Gallery in Design mode will take either a table or a record, these are the type of "conversion on play" issues that cause problems at play time.  Your formula should be consistent in its return types.

    As a general rule, it is better not to use conditional statements in an Items property filter.  It is better to simply provide a condition criteria that evaluates to true or false for the record iteration to be included or not in the resultant table.

    So, the following formula is functionally equivalent:

    Filter(
     faqCol,
     (IsBlank(areaCat.someProperty) || (!IsBlank(areaCat.someProperty) && (areaCat.someProperty in Type || areaCat.someProperty in 'Query On')) &&
     (IsBlank(suggested.Title) || (!IsBlank(suggested.Title) && suggested.Title in Title) &&
     (IsBlank('Search Input'.Text) || 
     CountRows(
     Filter(
     ForAll(
     Split('Search Input'.Text, " "),
     {Found:
     Result in 'Query Area' ||
     Result in 'Query On' ||
     Result in Title
     }
     )
     )
     ) > 0
     )
    )

    (except again, the second criteria will provide a table result as its should rather than a single record).

     

    I hope this is helpful for you.

  • timl Profile Picture
    34,978 Super User 2025 Season 1 on 10 May 2021 at 11:58:26
    Re: Tip: Switch(true)

    Many thanks for sharing this @EpicTriffid! As you say, it's definitely more readable.

     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Understanding Microsoft Agents - Introductory Session

Confused about how agents work across the Microsoft ecosystem? Register today!

Warren Belz – Community Spotlight

We are honored to recognize Warren Belz as our May 2025 Community…

Congratulations to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 146,745 Most Valuable Professional

#2
RandyHayes Profile Picture

RandyHayes 76,287 Super User 2024 Season 1

#3
Pstork1 Profile Picture

Pstork1 66,091 Most Valuable Professional

Leaderboard
Loading started