cancel
Showing results for 
Search instead for 
Did you mean: 
Reply

I need insert two data entry in two coluns, but split data semi comma separate.

Hi guys,

I'm try insert two data entry in two colunms, but this work fine to one columns, i not know make work for two coluns.

Code that work fine one colunm:

 

ForAll(
        Trim(Split(Reserva.Text;";"));
Patch(FaturamentoHotel;
        Defaults(FaturamentoHotel);
        {'Hora de início':Now();
        'Hora de conclusão':Now();
        Email: User().Email;
        Nome:User().FullName;
        Reserva:Result}))

 

 
My Target:
Data Entry:

AdamLincoln_0-1644256259515.png


Target for data entry:

AdamLincoln_1-1644256440704.png

 

1 ACCEPTED SOLUTION

Accepted Solutions

@AdamLincoln 

To start, your formula has the ForAll backward. You are trying to use it like a ForLoop in some development language - which PowerApps is not.  It is a function that returns a table of records based on your iteration table and record schema.

It is more efficient to use the function as intended and will provide better performance - especially when trying to Patch.

 

 Please consider changing your Formula to the following:

With({_usr: User();
      _reserva: Split(Trim(Reserva.Text); ";");
      _valorParceiro: Split(Trim(ValorParceiro.Text); ";")
     };

    Patch(FaturamentoHotel;
        ForAll(Sequence(CountRows(_reserva));
            {'Hora de início':Now();
             'Hora de conclusão':Now();
             Email: _usr.Email;
             Nome: _usr.FullName;
             Reserva: Last(FirstN(_reserva, Value)).Result;
            'Valor Parceiro': Last(FirstN(_valorParceiro, Value)).Result
            }
        )
    )
)

 

Also, your User() function is expensive (performance), so best to only perform it once.

 

The above formula will create a record in your datasource for each split and include the associated item from the second split.

 

I hope this is helpful for you.

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

View solution in original post

7 REPLIES 7

@AdamLincoln 

To start, your formula has the ForAll backward. You are trying to use it like a ForLoop in some development language - which PowerApps is not.  It is a function that returns a table of records based on your iteration table and record schema.

It is more efficient to use the function as intended and will provide better performance - especially when trying to Patch.

 

 Please consider changing your Formula to the following:

With({_usr: User();
      _reserva: Split(Trim(Reserva.Text); ";");
      _valorParceiro: Split(Trim(ValorParceiro.Text); ";")
     };

    Patch(FaturamentoHotel;
        ForAll(Sequence(CountRows(_reserva));
            {'Hora de início':Now();
             'Hora de conclusão':Now();
             Email: _usr.Email;
             Nome: _usr.FullName;
             Reserva: Last(FirstN(_reserva, Value)).Result;
            'Valor Parceiro': Last(FirstN(_valorParceiro, Value)).Result
            }
        )
    )
)

 

Also, your User() function is expensive (performance), so best to only perform it once.

 

The above formula will create a record in your datasource for each split and include the associated item from the second split.

 

I hope this is helpful for you.

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

Your code formatted for readability:

ForAll(
    Trim(Split(Reserva.Text;";"));
    Patch(FaturamentoHotel;
        Defaults(FaturamentoHotel);
        {
            'Hora de início':Now();
            'Hora de conclusão':Now();
            Email: User().Email;
            Nome:User().FullName;
            Reserva:Result
        }
    )
)

 The problem is you are splitting your Reserva Text field, but you need to find the corresponding text in Valor Parcerio. This is tricky because you need the corresponding index. One way you could do it is:

 

With({
        ReservaTable:Trim(Split(Reserva.Text;";"));
        ValorParcerioTable:Trim(Split(Reserva.Text;";"));
    };
    ForAll(Sequence(CountA(ReservaTable));
        Patch(FaturamentoHotel;
            Defaults(FaturamentoHotel);
            {
                'Hora de início':Now();
                'Hora de conclusão':Now();
                Email: User().Email;
                Nome:User().FullName;
                Reserva:Last(FirstN(ReservaTable,Value).Result;
                'Valor Parcerio':Last(FirstN(ValorParcerioTable,Value).Result
            }
        )
    )
)

I'm not entirely sure about where you will need ".Result" and ".Value" but you should get code suggestions while typing to help you figure that out.

@sopatte 

Keeping in mind that your ForAll is still used backward and performance will suffer from using a Patch in a ForAll when trying to use it as a ForLoop - that it's not!

 

You have to realize that ForAll is a function that creates a Table.  It will perform that action internally whether you use it correctly or not.  As well, Patch will return a record.  These two things in mind tell you that, since the record definition is not specified in the ForAll formula you provided, then your resulting table will be a table of rows for each iteration (in this case of the split) that will have a record with a Value column that will have the results of the Patch.

So, in the case of a couple of records, the performance hit might be minimal, but the performance starts to decrease sharply for the more records that you have as the ForAll will still (regardless of use) create a table to return that has all of the results of all of the patches.

 

When used properly, with a formula such as I had suggested - Patch(datasource, ForAll(iterations, {record Definition}))

Then the Patch will only take the overhead of instantiating once.  It is then provided with a table of all of the records to patch.  So, it will not then have the additional overhead of retrieving each record as it patches in order to populate a ForAll table.

 

The Defaults function is not needed.  Patch is smart enough to know that a primary key is not provided, so it will produce a new record.  Should a primary key exist, Patch is smart enough to then lookup and update that record.  Even a combination of records returned from the ForAll (those with primary and those without), Patch will create and update as needed.

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

@RandyHayes 
I didn't see your response. I was composing my response while you (apparently) were composing yours.

One comment though. We touched on this before. The documentation of ForAll does state, "If the return value from ForAll is not used, which is often the case with data modification functions, then the return value will not be created and there are no resource or order concerns." I think that indicates it can be used like a for loop without quite as much concern as you attribute. That said, it makes sense that a single call to Patch is better than multiple calls to Patch, and in general I have no doubt that your insight is greater than mine, and that your suggestions are (a) worth considering and (b) in fact, better than mine. I had no idea, for example, that patch was "smart enough" to do the things you mentioned, and particularly I look forward to not using Defaults() so much.

Peace.

Thanks so much Randy, you solution work perfectly.

Thanks so much for you help sopatte.

@sopatte 

Biggest issue with using the ForAll like a ForLoop is that it implies a level of "development".  PowerApps is not a development platform.  It is formulas...just like Excel, because it was modeled around Excel.

So, I personally push people away from ForAll like a loop, because it is too common to try to bring "bad development" habits into PowerApps.  So, best to avoid them and use the functions and formulas as they were originally designed.  There ARE times that it has to be used as a for loop...BUT, they should be rare!  

One distinction with the Patch in a ForAll is that it will instantiate the Patch for each action over and over.  On small datasets, perhaps not an issue, but the larger, the more performance hits there are.  This is a very common answer I resolve on this forum often - "Help - My ForAll patch is very slow".  When I provide the proper way to use that with the ForAll as a table, performance becomes a tremendous change.

It is misleading though because, if you watch the monitor tool, you will see the Patch perform for each record, regardless of the way the formula is written.  This is because, technically, the entire table of records cannot be delegated to the source, so each must be handled.  However, the difference in this case is that the Patch function itself instantiates only once.  One once does it have to gather datasource info, and session table info.  So, all of that overhead is removed. 

 

The other aspect of ForAll when used as the Table function it was designed for, is that it becomes the MOST powerful function in PowerApps because it provides a way to shape data dynamically (not in some behavioral action) in evaluated formulas.  This is HUGE for PowerApps design!

 

And yes - Patch is smart!  It will know what to do simply based on the primary key.  If the key is blank, it will create a record.  If it has a value, it will update the corresponding record.

The Defaults are helpful for two cases -

1) If you need all of the columns of a record to be part of the return from a Patch.

So - 

Set(record, Patch(dataSource, {Title: "Hi"})) 

This will create a new record in the Datasource because there is no primary key.  The resulting record variable will have ONE column - Title

Set(record, Patch(dataSource, Defaults(DataSource), {Title: "Hi"})) will do the same thing, except the record variable now will contain ALL of the columns of the datasource record.

 

2) If you actually have defaults!  If there are some columns that need to have specific datasource specified defaults.

Using that in a ForAll is not an issue.  It would just introduce a Patch.

Ex.

Patch(dataSource, ForAll(recordList, Patch(Defaults(dataSource), {column:Values})))

 

Another BONUS...some functions have little known signatures.  For example, Removing records - this is another place where people complain on performance when they do the following:

ForAll(table As _item, RemoveIf(dataSource, ID=_item.ID))

The formula is better written because of one additional signature to the Remove function - the one where you specify a DataSource and a Table.  In this case, it is looking always for the primary key.

So, the above formula would be written as:

Remove(dataSource, ForAll(table, {ID: ID}))

The ForAll would produce a table of ID's (the primary key in this case).  Remove then knows exactly what to do with that.

However, in the above, the ForAll really isn't needed as the following is equivalent:

Remove(dataSource, table.ID)

 

I do fear that more and more will adopt trying to use the ForAll as a for loop and cringe when I see the docs try to placate to those that use it as such, because, ultimately yes, you can use it that way, but, there are performance implications and, it really detracts from the way that PowerApps was designed - as a no-code Excel-like app designer.  The more people try to make it "development", the harder things get and the more difficult and complicated things are to maintain and manage.

And, the simplicity of the formulas produced when using it properly are superior to when used the other way around.

I just hope they never allow setting variables or other things in a ForAll as that will completely detract from the purpose.  And as mentioned before, when using the ForAll as intended - it becomes the most powerful function in PowerApps.  When used as a For Loop, it is a "so-so" function, and annoying to many because you can't do certain things in it.

 

Hopefully that is clear and helpful for you.

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

Helpful resources

Announcements

Community will be READ ONLY July 16th, 5p PDT -July 22nd

Dear Community Members,   We'd like to let you know of an upcoming change to the community platform: starting July 16th, the platform will transition to a READ ONLY mode until July 22nd.   During this period, members will not be able to Kudo, Comment, or Reply to any posts.   On July 22nd, please be on the lookout for a message sent to the email address registered on your community profile. This email is crucial as it will contain your unique code and link to register for the new platform encompassing all of the communities.   What to Expect in the New Community: A more unified experience where all products, including Power Apps, Power Automate, Copilot Studio, and Power Pages, will be accessible from one community.Community Blogs that you can syndicate and link to for automatic updates. We appreciate your understanding and cooperation during this transition. Stay tuned for the exciting new features and a seamless community experience ahead!

Summer of Solutions | Week 4 Results | Winners will be posted on July 24th

We are excited to announce the Summer of Solutions Challenge!   This challenge is kicking off on Monday, June 17th and will run for (4) weeks.  The challenge is open to all Power Platform (Power Apps, Power Automate, Copilot Studio & Power Pages) community members. We invite you to participate in a quest to provide solutions in the Forums to as many questions as you can. Answers can be provided in all the communities.    Entry Period: This Challenge will consist of four weekly Entry Periods as follows (each an “Entry Period”)   - 12:00 a.m. PT on June 17, 2024 – 11:59 p.m. PT on June 23, 2024 - 12:00 a.m. PT on June 24, 2024 – 11:59 p.m. PT on June 30, 2024 - 12:00 a.m. PT on July 1, 2024 – 11:59 p.m. PT on July 7, 2024 - 12:00 a.m. PT on July 8, 2024 – 11:59 p.m. PT on July 14, 2024   Entries will be eligible for the Entry Period in which they are received and will not carryover to subsequent weekly entry periods.  You must enter into each weekly Entry Period separately.   How to Enter: We invite you to participate in a quest to provide "Accepted Solutions" to as many questions as you can. Answers can be provided in all the communities. Users must provide a solution which can be an “Accepted Solution” in the Forums in all of the communities and there are no limits to the number of “Accepted Solutions” that a member can provide for entries in this challenge, but each entry must be substantially unique and different.    Winner Selection and Prizes: At the end of each week, we will list the top ten (10) Community users which will consist of: 5 Community Members & 5 Super Users and they will advance to the final drawing. We will post each week in the News & Announcements the top 10 Solution providers.  At the end of the challenge, we will add all of the top 10 weekly names and enter them into a random drawing.  Then we will randomly select ten (10) winners (5 Community Members & 5 Super Users) from among all eligible entrants received across all weekly Entry Periods to receive the prize listed below. If a winner declines, we will draw again at random for the next winner.  A user will only be able to win once overall. If they are drawn multiple times, another user will be drawn at random.  Individuals will be contacted before the announcement with the opportunity to claim or deny the prize.  Once all of the winners have been notified, we will post in the News & Announcements of each community with the list of winners.   Each winner will receive one (1) Pass to the Power Platform Conference in Las Vegas, Sep. 18-20, 2024 ($1800 value). NOTE: Prize is for conference attendance only and any other costs such as airfare, lodging, transportation, and food are the sole responsibility of the winner. Tickets are not transferable to any other party or to next year’s event.   ** PLEASE SEE THE ATTACHED RULES for this CHALLENGE**   Week 1 Results: Congratulations to the Week 1 qualifiers, you are being entered in the random drawing that will take place at the end of the challenge. Community MembersNumber of SolutionsSuper UsersNumber of Solutions @anandm08  23 @WarrenBelz  31 @DBO_DV  10 @Amik  19 AmínAA 6 @mmbr1606  12 @rzuber  4 @happyume  7 @Giraldoj  3@ANB 6 (tie)   @SpongYe  6 (tie)     Week 2 Results: Congratulations to the Week 2 qualifiers, you are being entered in the random drawing that will take place at the end of the challenge. Community MembersSolutionsSuper UsersSolutions @anandm08  10@WarrenBelz 25 @DBO_DV  6@mmbr1606 14 @AmínAA 4 @Amik  12 @royg  3 @ANB  10 @AllanDeCastro  2 @SunilPashikanti  5 @Michaelfp  2 @FLMike  5 @eduardo_izzo  2   Meekou 2   @rzuber  2   @Velegandla  2     @PowerPlatform-P  2   @Micaiah  2     Week 3 Results: Congratulations to the Week 3 qualifiers, you are being entered in the random drawing that will take place at the end of the challenge.   Week 3:Community MembersSolutionsSuper UsersSolutionsPower Apps anandm0861WarrenBelz86DBO_DV25Amik66Michaelfp13mmbr160647Giraldoj13FLMike31AmínAA13SpongYe27     Week 4 Results: Congratulations to the Week 4 qualifiers, you are being entered in the random drawing that will take place at the end of the challenge.   Week 4:Community MembersSolutionsSuper UsersSolutionsPower Apps DBO-DV21WarranBelz26Giraldoj7mmbr160618Muzammmil_0695067Amik14samfawzi_acml6FLMike12tzuber6ANB8   SunilPashikanti8

Check Out | 2024 Release Wave 2 Plans for Microsoft Dynamics 365 and Microsoft Power Platform

On July 16, 2024, we published the 2024 release wave 2 plans for Microsoft Dynamics 365 and Microsoft Power Platform. These plans are a compilation of the new capabilities planned to be released between October 2024 to March 2025. This release introduces a wealth of new features designed to enhance customer understanding and improve overall user experience, showcasing our dedication to driving digital transformation for our customers and partners.    The upcoming wave is centered around utilizing advanced AI and Microsoft Copilot technologies to enhance user productivity and streamline operations across diverse business applications. These enhancements include intelligent automation, AI-powered insights, and immersive user experiences that are designed to break down barriers between data, insights, and individuals. Watch a summary of the release highlights.    Discover the latest features that empower organizations to operate more efficiently and adaptively. From AI-driven sales insights and customer service enhancements to predictive analytics in supply chain management and autonomous financial processes, the new capabilities enable businesses to proactively address challenges and capitalize on opportunities.    

Updates to Transitions in the Power Platform Communities

We're embarking on a journey to enhance your experience by transitioning to a new community platform. Our team has been diligently working to create a fresh community site, leveraging the very Dynamics 365 and Power Platform tools our community advocates for.  We started this journey with transitioning Copilot Studio forums and blogs in June. The move marks the beginning of a new chapter, and we're eager for you to be a part of it. The rest of the Power Platform product sites will be moving over this summer.   Stay tuned for more updates as we get closer to the launch. We can't wait to welcome you to our new community space, designed with you in mind. Let's connect, learn, and grow together.   Here's to new beginnings and endless possibilities!   If you have any questions, observations or concerns throughout this process please go to https://aka.ms/PPCommSupport.   To stay up to date on the latest details of this migration and other important Community updates subscribe to our News and Announcements forums: Copilot Studio, Power Apps, Power Automate, Power Pages

Users online (1,459)