cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
EpicTriffid
Resolver I
Resolver I

Tip: Switch(true)

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!

2 REPLIES 2
timl
Super User
Super User

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

 

RandyHayes
Super User
Super User

@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.

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

Helpful resources

Announcements
PA_User Group Leader_768x460.jpg

Manage your user group events

Check out the News & Announcements to learn more.

secondImage

Power Apps Community Call

Please join us on Wednesday, October 20th, at 8a PDT. Come and learn from our amazing speakers!

Community Connections 768x460.jpg

Community & How To Videos

Check out the new Power Platform Community Connections gallery!

Welcome Super Users.jpg

Super User Season 2

Congratulations, the new Super User Season 2 for 2021 has started!

Carousel 2021 Release Wave 2 Plan 768x460.jpg

2021 Release Wave 2 Plan

Power Platform release plan for the 2021 release wave 2 describes all new features releasing from October 2021 through March 2022.

Top Solution Authors
Top Kudoed Authors
Users online (2,071)