What’s The Difference Between These Power Apps Functions?

What’s The Difference Between These  Power Apps Functions?

“There’s more than one way to skin a cat.”

As a cat-lover, I dislike this graphic-reference to taxidermy. But it does have a point: there are often multiple ways to achieve a goal.

In this article I will compare 6 pairs of Power Apps functions, why they are the similar and how you can use their differences to your advantage.


RGBA vs COLORVALUE function

The RGBA and COLORVALUE functions can both output the same colors but they require different inputs.

Here’s an example:

ColorRGBA(Red,Green,Blue,Alpha)ColorValue(CSSColor)
color blueviolet RGBA(138, 43, 226, 1)ColorValue(“#8a2be2”)


RGBA gives Power Apps makers the ability to adjust the transparency of a color by setting the Alpha property. This is useful for creating overlays.

COLORVALUE takes text-based hex value as an input which means you can easily store it in a SharePoint list and use it across several apps to ensure consistent branding.


PowerAppsColors (SharePoint List)


Put this code in the OnStart property of your app to store the colors in a global variable. Then you can refer to a color by typing varColors.Primary1

Set(varColors,
    Primary1: 
    ColorValue(LookUp(PowerAppsColors, Title="Primary1", HexValue)),
    Primary2: 
    ColorValue(LookUp(PowerAppsColors, Title="Primary2", HexValue)),
    Secondary1: 
    ColorValue(LookUp(PowerAppsColors, Title="Secondary1", HexValue)),
    Secondary2: 
    ColorValue(LookUp(PowerAppsColors, Title="Secondary2", HexValue)),
)



ISBLANK vs ISEMPTY function

ISBLANK and ISEMPTY both check for the absence of any value. Suppose you have a SharePoint list with some Cities:


Locations (SharePoint List)

City
New York
London
Paris


You might want to check if the city “Tokyo” exists in City column. The code below will return true because “Tokyo” cannot be found using the LOOKUP function.

IsBlank(LookUp(Locations, City="Tokyo"))


Another approach would be to use a FILTER function and check whether any results were found.

// The output of this formula is false
IsBlank(Filter(Location, City="Tokyo"))

// The output of this formula is true
IsEmpty(Filter(Location, City="Tokyo"))


Why does ISBLANK return false? The empty table counts as value even though it has no records. Therefore, you must apply ISEMPTY to find the expected result of true.



CONCATENATE vs CONCAT

CONCATENATE and CONCAT each join multiple text strings together into a single text string.

In this example CONCATENATE merges several individual email addresses into a format used when sending a message to multiple people.

// load emails into variables
Set(varEmail1, "HarryAnderson@yourcompany.com);
Set(varEmail2, "SarahAllan@yourcompany.com);
Set(varEmail3, "JohnAdams@yourcompany.com);

// join together emails with a separator
Concatenate(varEmail1, "; ", varEmail2, "; ", varEmail3")

>> Result: HarryAnderson@yourcompany.com; SarahAllan@yourcompany.com; JohnAdams@yourcompany.com;


The “&” symbol is a short-hand for CONCATENATE and also produces the same result

// alternate method to join emails with a separator
varEmail1 & "; " & varEmail2 & "; " & varEmail3


What if the emails were stored in a table like this instead?


Employees (SharePoint List)

CompanyEmail
HarryAnderson@yourcompany.com
SarahAllan@yourcompany.com
JohnAdams@yourcompany.com


CONCAT can extract the Email Address on each row of CompanyEmail and convert them into a single text string.

// join together emails with a separator
Concat(Employees, CompanyEmail & "; ")

>> Result: HarryAnderson@yourcompany.com; SarahAllan@yourcompany.com; JohnAdams@yourcompany.com;



SET vs UPDATECONTEXT

SET and UPDATECONTEXT both temporarily store data such as a line of text, a number, a date, a true/false value, etc.

SET creates a global variable which can be referenced on any screen. For example, you could create a variable called varFontSize and place it in the FontSize property of all the labels in your app. Then if you decide the text in your app needs to appear larger you could make the change by updating a single value.

Set(varFontSize, 16)


UPDATECONTEXT creates a local variable which can only be referenced from a single screen where it is initialized. What is the advantage of limiting a variable to a single-screen? Imagine that your app grows to have over 50 variables. It could become difficult keep track where all of those global variables are being used and also to troubleshoot any errors over multiple screens. Local variables are an excellent way to organize our code on a single screen keeping your app simple.

UpdateContext({locIsFormSubmitted: true});


At first it is difficult to understand when to use a global variable vs. a local variable. The best way to figure it out is through experimentation.



IF vs SWITCH function

IF and SWITCH are both logical functions. They determine whether a series of conditions are true or false. Then based on the outcome a specific action is taken.

IF is the more well-known function in this pair. A benefit of the IF function is you can test multiple criteria at once as shown in the code below

If(
    varTestScore >= "80" And varTestScore <=100,
    Set(varGrade: "A"),
    varTestScore > "70" And varTestScore < 80 ,
    Set(varGrade: "B"),
    varTestScore > "60" And varTestScore < 70 ,
    Set(varGrade: "C"),
    Set(varGrade: "F")
)


SWITCH is much simpler by comparison. It can only test if a variable is equal to a defined value.

Switch(
    varNextScreen;
    "Approved", Navigate('Approved Screen');
    "Rejected", Navigate('Rejected Screen');
    "Hold", Navigate('Hold Screen');
)


Why should you use SWITCH when considering the IF function does everything SWITCH can and more? Arguably, SWITCH is cleaner and more easily readable but in my opinion using it does not have any advantage in PowerApps. It is just another way of doing the same thing.

In other programming languages SWITCH can offer performance benefits. Pretend the code below was not written in PowerApps. The SWITCH function is more efficient because it only looks for the User’s name on the server only once. However, in Power Apps there is some behind-the-scenes magic that prevents multiple calls to the server in the same statement.

// Retrieves the username 3 times from the server, once for each comparison
If(
    User().FullName="Jill", Set(varRole: "General Manager"),
    User().FullName="Mark", Set(varRole: "Supervisor")
    User().FullName="Gerald", Set(varRole: "Employee")
)

// Only obtains the username once
Switch(
    User().FullName,
    "Jill", Set(varRole: "General Manager"),
    "Mark", Set(varRole: "Supervisor"),
    "Gerald", Set(varRole: "Employee")
)


TODAY vs NOW

TODAY and NOW each output information about the current date and time

TODAY only returns the date. You might not require any information about the time if you are checking whether it is a friend’s birthday.

Today()

>> Result: 7/3/2020


NOW shows both the date and the time. The time is important for scenarios like recording when a package was delivered

Now()

>> Result: 7/3/2020 9:36PM





Questions?

If you have any questions or feedback about What’s The Difference Between These Power Apps Functions please leave a message in the comments section below. You can post using your email address and are not required to create an account to join the discussion.

Matthew Devaney

Subscribe
Notify of
guest
4 Comments
Oldest
Newest
Inline Feedbacks
View all comments
Ryan Bond
Ryan Bond
2 years ago

Thanks Matthew! I’ve recently picked up your trick of setting my RGBA colours in an onstart variable. Previously I referenced them from a control on an admin screen but your method allows me to have see their colour in the code preview. Very useful thanks!

Art K
Art K
2 years ago

Hey Matt,

Here’s a few hot tips to add to your color info:

1. Colors in HEX can include alpha info for transparency. For example, take the color #742774. (Matt’s favorite purple).

E.g. #74277466 The 66 makes it 40% transparent. ColorValue(“#74277466”) Converts it to RGBA(116, 39, 116 ,40)

2. HEX is for humans and RGBA is for computers. ? I find it is just easier to always work in HEX (one value copy/paste/done) and then have my canvas app do the conversion to RGBA one time on app start. To speed that up, I use the Concurrent() function. Like this…

Concurrent(
Set(_mattFavColorHEX, “#74277466”); Set(_mattFavColorRGBA, ColorValue(_mattFavColorHEX)),
Set(_mattOtherFavColorHEX, “#66B6E3B3”); Set(_mattOtherFavColorRGBA, ColorValue(_mattOtherFavColorHEX))
);

Start up goes faster because the Concurrent function evaluates multiple formulas at the same time.
However, the RGBA color var requires that the HEX var has already been created. I put them on the same line so I know they are related. The secret is to put a semicolon between the two color Set() functions with a comma at the end. This forces the canvas app to process them together, one after the other. So if you have a number of colors that you want to use in the app, you can process them all in pairs.

Sadly, canvas apps do not have a built-in function to convert from RGBA back to HEX. Maybe next week?

Dean
Dean
2 years ago

Good article. The With statement is another useful alternative to variables, almost acts like a ‘very local’ variable.