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!
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.
Many thanks for sharing this @EpicTriffid! As you say, it's definitely more readable.
WarrenBelz
146,745
Most Valuable Professional
RandyHayes
76,287
Super User 2024 Season 1
Pstork1
66,091
Most Valuable Professional