Power Fx: String interpolation, Index function, and RandBetween function
We are pleased to announce three great new features in Power Fx! These features are available in version 3.22041 and later of Power Apps Studio.
String interpolation
Note: There is a known issue with this feature when used in some apps that also use service functions through a connector. On save of the app, $” will be replaced with Concatenate. The problem has been fixed in 3.22051 which will begin deployment shortly.
Have you ever spliced together a long string with the & operator or Concatenate function?
"Welcome " & FirstName & " " & LastName & ", it is great to meet you!"
What if instead, you could embed those references to FirstName and LastName directly into the string?
$"Welcome {FirstName} {LastName}, it is great to meet you!"
The second form is a lot cleaner and easier to read. Most modern programming languages have the ability to embed expressions within a string and now Power Fx does too. Like C#, we call this feature string interpolation and it follows the same syntax as C#. You can read all about the details in the docs. You can of course continue to use Excel compatible standard strings and the concatenate operator and function; string interpolation only adds a new option.
The basic rules are:
- String interpolation strings begin with a $” instead of just “.
- Curly braces delineate the embedded expression.
- To include a curly brace in the string, double it such as {{ or }}, just as we do to escape double quotes in a standard string.
- Expressions can include standard strings or string interpolated strings.
Here are a few more interesting examples:
$"2+3 = {2+3}" // result: 2+3 = 5 $"{{this is inside curly braces}}" // result: {this is inside curly braces} With( {x:5, y:7}, $"Point ({x},{y}) is {Text( Sqrt( x^2+y^2 ), "#.###" )} from the origin" ) // result: Point (5,7) is 8.602 from the origin // shows a standard string nested within string interpolation With( {FirstName: "John", MiddleName: "Q.", LastName: "Doe"}, $"{Trim( $"Welcome {FirstName} {MiddleName} {LastName}" )}, we're glad you are here!" ) // result: Welcome John Q. Doe, we're glad you are here! // spacing is correct with 0, 1, 2, or 3 name parts that are non-blank // shows nested string interpolation
Why add this now? As we expand Power Fx to other products in the Power Platform, we have found that many of them use string interpolation by default. For example, consider the below action in Power Virtual Agents. The maker enters a string for the Message and can embed dynamic expressions within the string, in this case for the variables UserName and AgentName. This is a form of string interpolation with the leading $” provided by the system.
Index function
Have you run across Last( FirstN( Table, N ) ) in your Power Fx travels? Yes, that is a rather roundabout way to get to the Nth record of Table.
No longer! Excel has an Index function for, among other things, returning a row out of a range and so we adopted the same function. Index( Table, N ) is equivalent to the above. You can read all about it in the docs.
Why add this now? General goodness and because we are working on JSON parsing and we needed a more convenient way to index into a JSON array. Oops, general JSON parsing, did I say that out loud?! Stay tuned…
RandBetween function
And finally another Excel function, RandBetween( Low, High ) that returns an integer between Low and High inclusive. Much easier to use than multiplying a range by Rand() and making sure you have the lower and upper bounds handled correctly. Docs tell the tale.
Why add this now? General goodness and I think the team was tired of me using this as my example of a pure function I could create with enhanced component properties. Now I need to find another example.
Feedback welcome
As always, your feedback is most welcome here and in the Power Apps Community forum. Your input is very helpful to us in prioritizing what we do next.