if-statementpowerappsforall

Nested If function is not returning neither True or False


I am trying to use the ForAll function in Power FX to iterate over a collection called colMatSolicitados. Within the ForAll function, you are using an If function to check if a certain condition is met, and then setting the value of varT based on the result of that condition. The condition you are checking is whether the value of Ambito.CantidadRequerida is less than the result of a calculation that involves the LookUp and Filter functions, as well as the Sum function. Regardless of the calculation, the result shold be 'True' or 'False', but non of them is happening. I appreciate your help!!

enter image description here


Solution

  • The expressions varT = 1 and varT = 2 do not set the value of the variable varT to 1 or 2; they are comparing the value of varT with 1 or 2, and that result is not being used. Notice that ForAll will evaluate the expression for all records of the collection colMatSolicitados, so if you were to set the value of varT to one value for one row it would be overwritten in the next. In addition, the documentation of the ForAll function states that "that records can be processed in any order and, when possible, in parallel" - which is exactly why functions such as Set are forbidden in the expression, as it could lead to unpredictable results.

    If you want to find out how many records match your condition, you can do it with the Sum function, with an expression similar to the one below:

    Sum(
        colMatSolicitados As Ambito;
        If(
            Value(Ambito.CantidadRequerida) < (LookUp(Filter(...)) - Sum(...));
            1;
            0
        )
    )