1 & 1 equals “11”, what are you talking about?! Continuing our Power Fx text functions saga (pt 1. Text Extraction), let’s have a look at the different ways of concatenating text within Power Apps.
In this blog post we’ll explore 5 ways to easily concatenate text. From the simplicity of the ‘&’ operator to the extensibility of string interpolation, we’ll be looking at their advantages and preferable use cases.
Table of Contents
Individual string concatenation
Concatenate
The concatenate function allows you to (1) concatenate string values by adding them as individual function parameters and (2) concatenate a single-column table of string values (see ‘Concatenate (single-column table)‘). Let’s first focus on individual string concatenation.
The function expects at least 1 parameter, and optional additional parameters:
Concatenate(, [StringValue2])
Example of using the concatenate function to create a user greeting label:
Concatenate("Hello ", User().FullName)
The '&' operator
The string concatenation operator ‘&’ allows you to, as you may have guessed, concatenate string values by placing the operator between the to-be concatenated values. This is my go-to method for short and simple concatenation scenarios.
Using our previous user greeting label as an example, the ‘&’ operator method looks as follows:
"Hello " & User().FullName
String interpolation
When working with multiple to-be concatenated dynamic values, splicing each part into a separate parameter can be quite tiresome and result in code that is more difficult to read. String interpolation addresses these issues by removing the need to splice the parameters and providing a more readable format.
String interpolation can be achieved by prefixing the opening double quote with a dollar sign and enclosing dynamic values (read: not literal text) between curly brackets. The structure looks as follows:
$"Hello {User().FullName}"
To include curly brackets within your string, you will have to use double curly brackets:
//Returns {This is text within curly brackets.}
$"{{This is text within curly brackets.}}"
In section ‘ (1) Keep code readability in mind‘ we’ll investigate a more complex example of string interpolation.
What method to pick?
As we’ve now seen there are multiple methods to concatenate individual string values. There is not really a ‘wrong’ way of concatenating text since the different methods will all result in the same concatenated output.
That said, I would propose the following 3 recommendations:
(1) Keep code readability in mind
When concatenating several dynamic parameters combined with literal text, the strength of string interpolation clearly shines. For these cases, I would highly advise you to take some time to familiarize yourself with string interpolation.
To showcase the benefits of this approach, consider the common use case of sending an email that contains dynamic data (SendEmailV2 body parameter):
//'&' operator
"Dear " & User().FullName & ",
Your PTO request from " & Text(gblApproval.StartDate, "dd/mm/yyyy") & " until " & Text(gblApproval.EndDate, "dd/mm/yyyy") & " has been " & gblApproval.Status & ".
Best regards,
LaurensM"
//String interpolation
$"Dear {User().FullName},
Your PTO request from {Text(gblApproval.StartDate, "dd/mm/yyyy")} until {Text(gblApproval.EndDate, "dd/mm/yyyy")} has been {gblApproval.Status}.
Best regards,
LaurensM"
As we can see above, string interpolation reduces the need of splicing the individual parameters and provides a more readable format for the developer.
(2) Know when to use string interpolation
Although I am a massive fan of string interpolation, in certain scenarios it actually results in slightly more work. Let’s take our initial user greeting label as an example.
We could just add a simple ‘&’ operator between both values:
"Hello " & User().FullName
String interpolation would result in slightly more work and thus unnecessary additional complexity in my opinion:
$"Hello {User().FullName}"
Due to the simplicity of the ‘&’ operator, I tend to stick with that method in very short and simple scenarios that don’t need to be extended later on.
That said, I guess combining both methods is more of a personal preference and my third recommendation could perfectly be used to overrule this one – if you wanted to. 😉
(3) Whatever method(s) you pick, be consistent
Realistically, you only need a maximum of 2 out of the 3 options for individual string concatenation. I tend to stick to the ‘&’ operator for very short concatenation cases and string interpolation once we have a few dynamic parameters.
I would advise against combining all 3 options within your app and especially within a single control property.
Concatenate a table of string values
Concatenate (single-column table)
Building on our knowledge of the Concatenate function, we can also use this function with a single-column table as input. The concatenate function will provide a concatenated string for each record in that table – resulting in a table output with a single column called ‘Value’ that contains the concatenated strings.
To showcase this feature, let’s create a collection containing cat breeds and their respective personality.
//E.g. OnSelect of a button
ClearCollect(
colCatBreeds,
{Breed: "Bengal", Personality: "Energetic"},
{Breed: "British Shorthair", Personality: "Calm"},
{Breed: "Selkirk Rex", Personality: "Friendly"}
)
//E.g. Items property of a gallery
Concatenate(
"The ",
colCatBreeds.Breed,
" cat breed is known for their ",
Lower(colCatBreeds.Personality),
" personality."
)
The concatenate function will return the following output:
| Value |
| The Bengal cat breed is known for their energetic personality. |
| The British Shorthair cat breed is known for their calm personality. |
| The Selkirk Rex cat breed is known for their friendly personality. |
Concat
Concat allows you to concatenate values within a table into a single string value. The function accepts 3 parameters – the third being optional:
Concat(
,
,
[Separator]
)
Using our cat breeds collection as an example:
//Bengal, British Shorthair, Selkirk Rex
Concat(
colCatBreeds,
Breed,
", "
)
Note that you are able to supply the formula with more complex, concatenated values:
//Bengal - Energetic, British Shorthair - Calm , Selkirk Rex - Friendly
Concat(
colCatBreeds,
//Alternatively: $"{Breed} - {Personality}"
Breed & " - " & Personality,
", "
)
-
Posted in:
Power Apps
Scroll to Top