Disambiguation operator in Power Fx

 Disambiguation operator in Power Fx

Reference 1 2 3

Some functions create record scopes for accessing the fields of table while processing each record, such as FilterAddColumns, and Sum. Field names added with the record scope override the same names from elsewhere in the app. When this happens, you can still access values from outside the record scope with the @ disambiguation operator:

  • To access values from nested record scopes, use the @ operator with the name of the table being operated upon using this pattern:
    Table[@FieldName]
  • To access global values, such as data sources, collections, and context variables, use the pattern [@ObjectName] (without a table designation).

For more information and examples, see record scopes.

If the table being operated upon is an expression, such as Filter( Table, ... ), then the disambiguation operator cannot be used. Only the innermost record scope can access fields from this table expression, by not using the disambiguation operator.

For example, imagine having a collection X:

X value.

You can create this collection with ClearCollect( X, [1, 2] ).

And another collection Y:

Y value.

You can create this collection with ClearCollect( Y, ["A", "B"] ).

In addition, define a context variable named Value with this formula: UpdateContext( {Value: "!"} )

Let's put it all together. In this context, the following formula:

Power Apps
Ungroup(
    ForAll( X,
        ForAll( Y,
            Y[@Value] & Text( X[@Value] ) & [@Value]
        )
    ),
    "Value"
)

produces this table:

XY value.

What is going on here? The outermost ForAll function defines a record scope for X, allowing access to the Value field of each record as it is processed. It can be accessed by simply using the word Value or by using X[@Value].

The innermost ForAll function defines another record scope for Y. Since this table also has a Value field defined, using Value here refers to the field in Y's record and no longer the one from X. Here, to access X's Value field, we must use the longer version with the disambiguation operator.

Since Y is the innermost record scope, accessing fields of this table do not require disambiguation, allowing us to use this formula with the same result:

Power Apps
Ungroup(
    ForAll( X,
        ForAll( Y,
            Value & Text( X[@Value] ) & [@Value]
        )
    ),
    "Value"
)

All the ForAll record scopes override the global scope. The Value context variable we defined isn't available by name without the disambiguation operator. To access this value, use [@Value].

Ungroup flattens the result because nested ForAll functions result in a nested result table.

Another example:

AddColumns(
    Filter(Collection, Field1="matched") As your_other_table,
    "NewColumn", 
    Lookup(table, Field2 = your_other_table[@Field2], Field3)
)


No comments:

Post a Comment