Skip to main content

Notifications

Power Apps - Building Power Apps
Answered

'As' operator inside With or ForAll - is it a pointer or a separate variable scoped to With/ForAll?

(0) ShareShare
ReportReport
Posted on by 38

I am trying to understand when 'As' is used inside With or ForAll, is it a creation of separate variable or just a pointer to data in the source? Below is an example (let's say there is a button on a screen with code below in OnSelect of the button):

 

ClearCollect(IceCreamSales,

 Table(

            { Flavor: "Strawberry", UnitPrice: 1.99, QuantitySold: 20 },

            { Flavor: "Chocolate", UnitPrice: 2.99, QuantitySold: 45 },

            { Flavor: "Vanilla", UnitPrice: 1.50, QuantitySold: 35 }

           )

);

With({X:LookUp(IceCreamSales,Flavor="Vanilla")},

     With({P:X.UnitPrice},

          Set(varBefore,X);

          UpdateIf(IceCreamSales As K, K.Flavor=X.Flavor, {UnitPrice: K.UnitPrice+1.00});

          Set(varAfter,LookUp(IceCreamSales,Flavor="Vanilla"));

          Set(varPrice,P)

     )

)

 

After clicking the button first time, I expect:

varBefore = {Flavor: "Vanilla", UnitPrice: 1.50, QuantitySold: 35}

varAfter = {Flavor: "Vanilla", UnitPrice: 2.50, QuantitySold: 35}

varPrice = 1.50

 

But what I get is:

varBefore = {Flavor: "Vanilla", UnitPrice: 2.50, QuantitySold: 35}

varAfter = {Flavor: "Vanilla", UnitPrice: 2.50, QuantitySold: 35}

varPrice = 1.50

 

This makes me think that X is just a pointer to the row containing Flavor="Vanilla"; otherwise the varBefore should have had the old price.

And in case of varPrice, it keeps the old price as what I expected, that means it is instantiated variable and not a pointer.

 

I'll appreciate any feedback

  • RandyHayes Profile Picture
    RandyHayes 76,287 on at
    Re: 'As' operator inside With or ForAll - is it a pointer or a separate variable scoped to With/ForAll?

    @shdfbasfgagfkay 

    No problem.  Yes, the docs leave a lot of things very obscured.  I have found over the years when reading certain parts of the docs that they might state something that is VERY subtle, and if you actually already know what they are talking about, it makes sense, but is again, very subtle.  The downfall of that - if you *don't* know what they are talking about, it is so easy to gloss over and the point is then completely missed.

     

    Glad you have some good insight now though on it.

  • shdfbasfgagfkay Profile Picture
    shdfbasfgagfkay 38 on at
    Re: 'As' operator inside With or ForAll - is it a pointer or a separate variable scoped to With/ForAll?

    Thanks, Randy for the details. I wish it was detailed in the Microsoft help rather than https://docs.microsoft.com/en-us/power-apps/maker/canvas-apps/working-with-formulas-in-depth#:~:text=Actions%20are%20performed%20in%20the%20order%20in%20which%20they%20appear%20in%20the%20formula.%20The%20next%20function%20won%27t%20start%20until%20the%20current%20function%20has%20completed.%20If%20an%20error%20occurs%2C%20subsequent%20functions%20might%20not%20start.

    (..and I expected code work in progression it was laid out).

    Anyways, good to know and at least I have work around. I appreciate the quick responses. 

     

  • Verified answer
    RandyHayes Profile Picture
    RandyHayes 76,287 on at
    Re: 'As' operator inside With or ForAll - is it a pointer or a separate variable scoped to With/ForAll?

    @shdfbasfgagfkay 

    Yes, so again it is all about the discretion of the formula editor.  In your first Formula you were only using X to set a variable.  What the evaluation precedence is for that within the formula itself is not really known (except by the dev team). 

    Since you did not use the X scoped variable except to set a variable, this is not something that PowerApps needs to evaluate to perform the inner actions.  It is a simple assign-to-assign.  So it is very likely that it would evaluate that at the end of the inner actions that are more "complex".

    In your second Formula, you were doing a Patch on the record.  This is an action that has to be evaluated more than just assigned.  So, the formula evaluate will perform that action and hold it in X - as expected. 

     

    But in the context of the ForAll, there should never be any assignments of variables - or any actions in the ForAll.  That makes it into a For Loop that it is not.  Instead, the inner part of the ForAll is the record schema definition for the resulting table of your ForAll.

    SO...ForAll is totally separate in the context of the scenario you are stating.  Your "beef" is with With.  With itself "returns" what is done inside of it and thus it acts like a function that PowerApps needs to evaluate.  How it evaluates it into actual computer operations is completely at the discretion of the internals of PowerApps.  If I had to wager a guess, I would say that the logic is to evaluate inner actions that rely on values that are going to be used/manipulated within the With based on complexity and then evaluate based on least complexity...which would explain what you are seeing more.

     

     

  • shdfbasfgagfkay Profile Picture
    shdfbasfgagfkay 38 on at
    Re: 'As' operator inside With or ForAll - is it a pointer or a separate variable scoped to With/ForAll?

    @RandyHayes - thank you for the explanation. You're correct that I don't need to use With in this particular example but this was my test case for something little more complex that I need to do.

     

    Just one more question though, you said I wasn't using X anywhere and therefore it may be evaluated after inner action, but I do use X to set varBefore (even before the UpdateIf), does it not count as its use?

     

    BTW, if I define the same example little differently it does work as I expect:

    By replacing

    With({X:LookUp(IceCreamSales,Flavor="Vanilla")},

    in the original code with:

    With({X:Patch(LookUp(IceCreamSales,Flavor="Vanilla"),{})},

     

    Thanks again

  • RandyHayes Profile Picture
    RandyHayes 76,287 on at
    Re: 'As' operator inside With or ForAll - is it a pointer or a separate variable scoped to With/ForAll?

    @shdfbasfgagfkay 

    Yes, the formula editor will evaluate your formula in its own way.  In your case "X" is not a "pointer", but when it is evaluated in the formula itself is not always clear.

    Since the X and P are not used in the inner formula, the editor may very well evaluate them after it has performed the inner action.

     

    You mentioned ForAll (which is not in your formula), but if you are concerned over that particular formula, then it should be changed to the following:

    ClearCollect(IceCreamSales,
     Table(
     { Flavor: "Strawberry", UnitPrice: 1.99, QuantitySold: 20 },
     { Flavor: "Chocolate", UnitPrice: 2.99, QuantitySold: 45 },
     { Flavor: "Vanilla", UnitPrice: 1.50, QuantitySold: 35 }
     )
    );
    
    Set(varBefore, LookUp(IceCreamSales,Flavor="Vanilla"));
    Set(varPrice, varBefore.UnitPrice);
    UpdateIf(IceCreamSales As K, K.Flavor=X.Flavor, {UnitPrice: K.UnitPrice+1.00});
    Set(varAfter,LookUp(IceCreamSales,Flavor="Vanilla"));
    

     

    I feel you are grasping at something entirely different because the above does not incorporate any With or ForAll, but provides the results you would expect.

     

    When using the With function, it, like most all formulas in PowerApps, is evaluated at the discretion of the formula editor.

  • shdfbasfgagfkay Profile Picture
    shdfbasfgagfkay 38 on at
    Re: 'As' operator inside With or ForAll - is it a pointer or a separate variable scoped to With/ForAll?

    In my example, the variable 'varBefore' shows UnitPrice as 2.50 instead of showing 1.50. I'm making that assignment (using Set) before I run the Update formula. That's what I'm trying to understand. Thanks.

  • RandyHayes Profile Picture
    RandyHayes 76,287 on at
    Re: 'As' operator inside With or ForAll - is it a pointer or a separate variable scoped to With/ForAll?

    @shdfbasfgagfkay 

    You're trying to overthink the Formula evaluation process in PowerApps formulas!  You should be looking at everything in PowerApps as Excel formulas, not "code".  PowerApps is not a development language/platform.  So trying to impose things like pointers and such is just going to confuse things more.

     

    Using a With statement provides a scoped variable for the duration of the With function.  It is equivalent to a context variable except that it goes away and does not clog up your app with variables (should always be kept to a bare minimum).

     

    ForAll is a function that builds tables.  It is not a For Loop.  If you are looking at it like a For Loop than you will have unpredictable results in what you are setting out to do.

     

    What is the issue you are running into in reality?

     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Microsoft Kickstarter Events…

Register for Microsoft Kickstarter Events…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Announcing Forum Attachment Improvements!

We're excited to announce that attachments for replies in forums and improved…

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 145,526

#2
RandyHayes Profile Picture

RandyHayes 76,287

#3
Pstork1 Profile Picture

Pstork1 64,907

Leaderboard