There are two popular ways in Power Fx to analyse a condition that can have multiple outcomes. Most might lean on the If function, but have you thought about using a Switch function instead?
I have a table of data. I want to change the background fill of each entry, depending on the text.
Using the If function
The If function will do this absolutely fine for us, but it’s not hugely efficient and gets more repetitive with every additional outcome that’s needed:
A better use case for using an If function will be when you have an absolute true or false outcome to the condition.
To evaluate a single condition against multiple possible matches, use the Switch function instead.
Using the Switch function
We can use the Switch function to achieve exactly the same outcome, but with cleaner Power Fx:
If you have a similar use case in future, try to remember this more efficient method. It’s easier to read and manage, both for yourself and any other colleagues that are co-developing or supporting.
A good reminder for this is to keep validating your canvas app builds with the Code Review Tool. Using nested If‘s instead of Switch will be flagged as a failure of the ‘Code Readability’ pattern:
Fingers crossed the Code Review tool will have a ‘How to spell statement correctly’ pattern in due course…