As operator in Power Fx

 As operator in Power Fx

Use the As operator to name a record in a gallery or record scope function, overriding the default ThisItem or ThisRecord. Naming the record can make your formulas easier to understand and may be required in nested situations to access records in other scopes.

A few controls and functions apply formulas to individual records of a table. To refer to the individual record in a formula, use one of the following:


For example, you can modify the Items property of our gallery to use As to identify that we are working with an Employee:

Power Apps
Employees As Employee

Gallery of employees, using the As operator.

The formulas for the picture and name are adjusted to use this name for the current record:

Power Apps
Employee.Picture

Picture of an employee using the Employee name set with the As operator.

Power Apps
Employee.'First Name' & " " & Employee.'Last Name'

First and last name of an employee using the Employee name set with the As operator.

As can also be used with record scope functions to replace the default name ThisRecord. We can apply this to our previous example to clarify the record we are working with:

Power Apps
With( { InactiveEmployees: Filter( Employees, Status = 'Status (Employees)'.Inactive ) },
      ForAll( InactiveEmployees As Employee, 
              Patch( Employees, Employee, { Status: 'Status (Employees)'.Active } ) ) )

When nesting galleries and record scope functions, ThisItem and ThisRecord always refers to the inner most scope, leaving records in outer scopes unavailable. Use As to make all record scopes available by giving each a unique name.

For example, this formula produces a chessboard pattern as a text string by nesting two ForAll functions:

Power Apps
Concat( 
    ForAll( Sequence(8) As Rank,
        Concat( 
            ForAll( Sequence(8) As File, 
                    If( Mod(Rank.Value + File.Value, 2) = 1, " X ", " . " ) 
            ),
            Value 
        ) & Char(10) 
    ), 
    Value 
)

Setting a Label control's Text property to this formula displays:

Chessboard text shown in a label control.

Let's unpack what is happening here:

  • We start by iterating an unnamed table of 8 numbered records from the Sequence function. This loop is for each row of the board, which is commonly referred to as Rank and so we give it this name.
  • For each row, we iterate another unnamed table of 8 columns, and we give the common name File.
  • If Rank.Value + File.Value is an odd number, the square gets an X, otherwise a dot. This part of the formula is referencing both ForAll loops, made possible by using the As operator.
  • Concat is used twice, first to assemble the columns and then the rows, with a Char(10) thrown in to create a new line.

A similar example is possible with nested Gallery controls instead of ForAll functions. Let's start with the vertical gallery for the Rank. This gallery control will have an Items formula of:

Power Apps
Sequence(8) as Rank

Illustration of the outer gallery that provides the Rank iteration.

Within this gallery, we'll place a horizontal gallery for the File, that will be replicated for each Rank, with an Items property of:

Power Apps
Sequence(8) as File

Illustration of the inner gallery that provides the File iteration.

And finally, within this gallery, we'll add a Label control that will be replicated for each File and each Rank. We'll size it to fill the entire space and use the Fill property to provide the color with this formula:

Power Apps
If( Mod( Rank.Value + File.Value, 2 ) = 1, Green, Beige )

Label control within the two galleries that provides the alternating colors for the chessboard.

No comments:

Post a Comment