cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
_ru
Helper I
Helper I

Patching collection from gallery is overwriting data as blank / creating duplicates

Hello!

 

I am trying to get all of the data in a gallery to patch to a collection.

 

My app has a gallery with questions and controls so users can answer the questions, and then hit a button to save their answers that are patched to a collection. 

 

The gallery has 66 questions, and I have a tab list that filters the gallery questions based on the tab selected. The tabs are sections, each section has 3 or 4 questions, so when you select the tab, the appropriate 3 or 4 questions pop up in the gallery for the user to answer and then hit a save button to patch to the collection.

 

The problem however is when a users answers the first 3 questions and hits save, it saves the data to the collection just fine, but when they select the next tab and answer the next 3 questions and hit save, the first 3 answers are overwritten as blank and then only the second set of 3 questions are patched to the collection. And so on. 

 

For instance my collection will look like this when they answer the first 3 questions (it goes to 66 rows):

 

DataQuestionTitle
Answer 1Question 1Section 1
Answer 2Question 2Section 1
Answer 3Question 3Section 1
 Question 4Section 2
 Question 5Section 2
 Question 6Section 2

 

And then it will look like this when they answer the next 3 questions:

 

DataQuestionTitle
 Question 1Section 1
 Question 2Section 1
 Question 3Section 1
Answer 4Question 4Section 2
Answer 5Question 5Section 2
Answer 6Question 6Section 2

 

The OnSelect of my Save button is as follows:

 

 

ClearCollect(
	colFinal,
	colFormTemplate
);
ClearCollect(
	galData,
	galForm.AllItems
);
ForAll(
	galData,
	UpdateIf(
		colFinal,
		Title = galData[@Title] && Question = galData[@Question],
		{

			Data: Switch(
				ThisRecord.QuestionType,
				"LongText",
				txt_LongText.Text,
				"Choice",
				drp_Choice.Selected.Result
			)
		}
	)
);
Patch(
	'Results Data',
	varItem,
	{
		Title = "title here",
		AnswerData: JSON(
				colFinal,
				Compact
		)
	}
)

 

 

 

Now I did find that if I use "Collect" instead of "ClearCollect" it WILL keep the data previously entered for the first 3 questions and add on the data for the next 3 questions, HOWEVER it also adds all of the gallery data again, so instead of 66 rows, there's 132 which is not correct. 

I just need colFinal to be 66 rows and patch 3 or 4 rows at a time. 

How can I accomplish this? I feel like it should be simple but I just can't seem to get it right.

 

Please let me know if screenshots would help as well. Thank you so much in advance!

1 ACCEPTED SOLUTION

Accepted Solutions

@_ru  I enclose power apps app to show principle what i mean. Just import to your PowerApps.

 

Here i initialize collections wiht questions

Clear(colAnswers);
ClearCollect(
	colFinal,
	{Question: "Question1", Section: 1},
    {Question: "Question2", Section: 1},
    {Question: "Question3", Section: 1},
    {Question: "Question4", Section: 2},
    {Question: "Question5", Section: 2},
    {Question: "Question6", Section: 2},
    {Question: "Question7", Section: 3}
);

 

Here I add collect answer to separate collection colAnswers. 

If(
    ThisItem.Question in colAnswers.Question,
    UpdateIf(
        colAnswers,
        Question = ThisItem.Question,
        {Data: Self.Text}
    ),
    Collect(
        colAnswers,
        {
            Question: ThisItem.Question,
            Data: Self.Text
        }
    )
)

 

Please note that for InputField default property is

LookUp(colAnswers, Question = ThisItem.Question).Data

 

And then you transform results for patching to datasource.

ClearCollect(
    colResults,
    AddColumns(
        colFinal,
        "Data", LookUp(colAnswers, Question = colFinal[@Question]).Data
    )
)

 

View solution in original post

9 REPLIES 9
opit
Helper III
Helper III

@_ru what I would do: I would have an original collection with a questions loaded and when user answers 1 question I would make a collect answer to separate gallery (e.g. colAnswers). Before collecting answer I would search if there is already such answer and if it is then I update it and if not I add it.

 

for example:

// load questions to collection

ClearCollect(colQuestions, ...)

// then when user answer the question

If(

  ThisItem.ID in colQuestions.ID,
  update record in colAnswers,
  insert recorc in colAnswers
)

 

 

_ru
Helper I
Helper I

@opit I'm sorry, I must be confused because that's kind of what I'm doing already, no?

Collect(
	colFinal,
	colFormTemplate
);

This gives me a collection of all 66 rows of my collection that is Answer | Question | Title. Question and Title are filled out, Answer is blank. 

Collect(
	galData,
	galForm.AllItems
);

This gives me the current 3 questions AND answers that are being filtered in the gallery

ForAll(
	galData,
	UpdateIf(
		colFinal,
		Title = galData[@Title] && Question = galData[@Question],
		{

			Data: Switch(
				ThisRecord.QuestionType,
				"LongText",
				txt_LongText.Text,
				"Choice",
				drp_Choice.Selected.Result
			)
		}
	)
);

This is where I'm (trying at least) to do what you say. I want to Look at the colFinal collection, and ForAll of the current items in galData (which is the current 3 questions and answers), I want to add those 3 answers to colFinal where the Title and Question match. This works, except it also adds 66 more rows of everything and I don't want it to do that. I only want it to update the new stuff, not patch 66 duplicate items.

 

Do you know what syntax I would use to accomplish what you say? I thought UpdateIf would give me what I need, but if I need to wrap my ForAll(..) statement in an If statement, how would I do that?

_ru
Helper I
Helper I

bump...anyone have any ideas as to what i might be doing wrong? 

@_ru  I enclose power apps app to show principle what i mean. Just import to your PowerApps.

 

Here i initialize collections wiht questions

Clear(colAnswers);
ClearCollect(
	colFinal,
	{Question: "Question1", Section: 1},
    {Question: "Question2", Section: 1},
    {Question: "Question3", Section: 1},
    {Question: "Question4", Section: 2},
    {Question: "Question5", Section: 2},
    {Question: "Question6", Section: 2},
    {Question: "Question7", Section: 3}
);

 

Here I add collect answer to separate collection colAnswers. 

If(
    ThisItem.Question in colAnswers.Question,
    UpdateIf(
        colAnswers,
        Question = ThisItem.Question,
        {Data: Self.Text}
    ),
    Collect(
        colAnswers,
        {
            Question: ThisItem.Question,
            Data: Self.Text
        }
    )
)

 

Please note that for InputField default property is

LookUp(colAnswers, Question = ThisItem.Question).Data

 

And then you transform results for patching to datasource.

ClearCollect(
    colResults,
    AddColumns(
        colFinal,
        "Data", LookUp(colAnswers, Question = colFinal[@Question]).Data
    )
)

 

_ru
Helper I
Helper I

@opit thank you for the clarification!

Unfortunately when I import your app it's all blank:

_ru_0-1704462115639.png

 

Do you have any idea why that might be the case? Was it not saved/published before exporting?

 

In any case, I will try and replicate this in the meantime with the code you've given me and will report back if that works.

 

 

@_ru try this on

_ru
Helper I
Helper I

@opit Thank you so much! This actually works exactly how I need it to except for one small problem:

When I'm replicating it for my app, I need it to match a row in the gallery based on 2 keys (the section and the question) because some sections have questions that are the same. 

 

So I tried to alter your OnChange code here:

 

If(
    ThisItem.Question in colAnswers.Question,
    UpdateIf(
        colAnswers,
        Question = ThisItem.Question,
        {Data: Self.Text}
    ),
    Collect(
        colAnswers,
        {
            Question: ThisItem.Question,
            Data: Self.Text
        }
    )
)

 

 

To this instead (just the If/UpdateIf lines are changed):

 

If(
    ThisItem.Question && ThisItem.Title in colAnswers.Question,
    UpdateIf(
        colAnswers,
        Question = ThisItem.Question && Title = ThisItem.Title,
        {Data: Self.Text}
    ),
    Collect(
        colAnswers,
        {
            Question: ThisItem.Question,
            Data: Self.Text
        }
    )
)

 

 

It writes the answer correctly, but when I change an answer, it patches a new row instead of updating. So I must be doing something wrong but I can't seem to get the syntax right for this either. 

 

Any ideas what it might be?

 

I tried:

If(
    ThisItem.Question in colAnswers.Question && ThisItem.Title in colAnswers.Title,
    UpdateIf(
        colAnswers,
        Question = ThisItem.Question && Title = ThisItem.Title,
        {Data: Self.Text}
    ),

But then that doesn't patch anything at all. 

 

 

And thank you a million times over for your help so far, I genuinely appreciate you and can't thank you enough.





_ru
Helper I
Helper I

@opit I just realized, I think it's looking at the following statement 

If(
	ThisItem.Question in colAnswers.Question && ThisItem.Title in colAnswers.Title,

as in if the ThisItem.Question is ANYWHERE in the collection Question column, and ThisItem.Title is ANYWHERE in the collection Title column. 

I need it to be "If ThisItem.Question AND ThisItem.Title in the SAME ROW Together (they're both used to create a unique key) THEN update."

If that makes sense. Hopefully it does. I threw a text label in the app to test why it wasn't working and I set it to

galForm.Selected.Question in colAnswer.Question && galForm.Selected.Title in colAnswers.Title 

 

and saw that it was displaying "true" for a something that it should've been displaying false for. So again, it seems like it's not looking for the Question and Title in the same row like I need them to be. 

 

The Titles and Questions are used together to create a unique key. "Section 1 Question 1" for instance. Since there is another question titled "Question 1" in the other sections, i.e. "Section 2 Question 1"

_ru
Helper I
Helper I

I was able to fix my own issue above by using an ID column instead. It fixed this problem, and then created another one (of course), but I'll create a separate thread for that.

 

Thank you so much @opit for all your help! I really appreciate it!

Helpful resources

Announcements

Check out the Copilot Studio Cookbook today!

We are excited to announce our new Copilot Cookbook Gallery in the Copilot Studio Community. We can't wait for you to share your expertise and your experience!    Join us for an amazing opportunity where you'll be one of the first to contribute to the Copilot Cookbook—your ultimate guide to mastering Microsoft Copilot. Whether you're seeking inspiration or grappling with a challenge while crafting apps, you probably already know that Copilot Cookbook is your reliable assistant, offering a wealth of tips and tricks at your fingertips--and we want you to add your expertise. What can you "cook" up?   Click this link to get started: https://aka.ms/CS_Copilot_Cookbook_Gallery   Don't miss out on this exclusive opportunity to be one of the first in the Community to share your app creation journey with Copilot. We'll be announcing a Cookbook Challenge very soon and want to make sure you one of the first "cooks" in the kitchen.   Don't miss your moment--start submitting in the Copilot Cookbook Gallery today!     Thank you,  Engagement Team

Announcing Power Apps Copilot Cookbook Gallery

We are excited to share that the all-new Copilot Cookbook Gallery for Power Apps is now available in the Power Apps Community, full of tips and tricks on how to best use Microsoft Copilot as you develop and create in Power Apps. The new Copilot Cookbook is your go-to resource when you need inspiration--or when you're stuck--and aren't sure how to best partner with Copilot while creating apps.   Whether you're looking for the best prompts or just want to know about responsible AI use, visit Copilot Cookbook for regular updates you can rely on--while also serving up some of your greatest tips and tricks for the Community. Check Out the new Copilot Cookbook for Power Apps today: Copilot Cookbook - Power Platform Community.  We can't wait to see what you "cook" up!      

Tuesday Tip | How to Report Spam in Our Community

It's time for another TUESDAY TIPS, your weekly connection with the most insightful tips and tricks that empower both newcomers and veterans in the Power Platform Community! Every Tuesday, we bring you a curated selection of the finest advice, distilled from the resources and tools in the Community. Whether you’re a seasoned member or just getting started, Tuesday Tips are the perfect compass guiding you across the dynamic landscape of the Power Platform Community.   As our community family expands each week, we revisit our essential tools, tips, and tricks to ensure you’re well-versed in the community’s pulse. Keep an eye on the News & Announcements for your weekly Tuesday Tips—you never know what you may learn!   Today's Tip: How to Report Spam in Our Community We strive to maintain a professional and helpful community, and part of that effort involves keeping our platform free of spam. If you encounter a post that you believe is spam, please follow these steps to report it: Locate the Post: Find the post in question within the community.Kebab Menu: Click on the "Kebab" menu | 3 Dots, on the top right of the post.Report Inappropriate Content: Select "Report Inappropriate Content" from the menu.Submit Report: Fill out any necessary details on the form and submit your report.   Our community team will review the report and take appropriate action to ensure our community remains a valuable resource for everyone.   Thank you for helping us keep the community clean and useful!

Community Roundup: A Look Back at Our Last 10 Tuesday Tips

As we continue to grow and learn together, it's important to reflect on the valuable insights we've shared. For today's #TuesdayTip, we're excited to take a moment to look back at the last 10 tips we've shared in case you missed any or want to revisit them. Thanks for your incredible support for this series--we're so glad it was able to help so many of you navigate your community experience!   Getting Started in the Community An overview of everything you need to know about navigating the community on one page!  Community Links: ○ Power Apps ○ Power Automate  ○ Power Pages  ○ Copilot Studio    Community Ranks and YOU Have you ever wondered how your fellow community members ascend the ranks within our community? We explain everything about ranks and how to achieve points so you can climb up in the rankings! Community Links: ○ Power Apps ○ Power Automate  ○ Power Pages  ○ Copilot Studio    Powering Up Your Community Profile Your Community User Profile is how the Community knows you--so it's essential that it works the way you need it to! From changing your username to updating contact information, this Knowledge Base Article is your best resource for powering up your profile. Community Links: ○ Power Apps ○ Power Automate  ○ Power Pages  ○ Copilot Studio    Community Blogs--A Great Place to Start There's so much you'll discover in the Community Blogs, and we hope you'll check them out today!  Community Links: ○ Power Apps ○ Power Automate  ○ Power Pages  ○ Copilot Studio    Unlocking Community Achievements and Earning Badges Across the Communities, you'll see badges on users profile that recognize and reward their engagement and contributions. Check out some details on Community badges--and find out more in the detailed link at the end of the article! Community Links: ○ Power Apps  ○ Power Automate  ○ Power Pages  ○ Copilot Studio    Blogging in the Community Interested in blogging? Everything you need to know on writing blogs in our four communities! Get started blogging across the Power Platform communities today! Community Links: ○ Power Apps  ○ Power Automate  ○ Power Pages  ○ Copilot Studio   Subscriptions & Notifications We don't want you to miss a thing in the community! Read all about how to subscribe to sections of our forums and how to setup your notifications! Community Links: ○ Power Apps  ○ Power Automate  ○ Power Pages  ○ Copilot Studio   Getting Started with Private Messages & Macros Do you want to enhance your communication in the Community and streamline your interactions? One of the best ways to do this is to ensure you are using Private Messaging--and the ever-handy macros that are available to you as a Community member! Community Links: ○ Power Apps  ○ Power Automate  ○ Power Pages  ○ Copilot Studio   Community User Groups Learn everything about being part of, starting, or leading a User Group in the Power Platform Community. Community Links: ○ Power Apps  ○ Power Automate  ○ Power Pages  ○ Copilot Studio   Update Your Community Profile Today! Keep your community profile up to date which is essential for staying connected and engaged with the community. Community Links: ○ Power Apps  ○ Power Automate  ○ Power Pages  ○ Copilot Studio   Thank you for being an integral part of our journey.   Here's to many more Tuesday Tips as we pave the way for a brighter, more connected future! As always, watch the News & Announcements for the next set of tips, coming soon!

Hear what's next for the Power Up Program

Hear from Principal Program Manager, Dimpi Gandhi, to discover the latest enhancements to the Microsoft #PowerUpProgram, including a new accelerated video-based curriculum crafted with the expertise of Microsoft MVPs, Rory Neary and Charlie Phipps-Bennett. If you’d like to hear what’s coming next, click the link below to sign up today! https://aka.ms/PowerUp  

Welcome to the Power Apps Community

Welcome! Congratulations on joining the Microsoft Power Apps community! You are now a part of a vibrant group of peers and industry experts who are here to network, share knowledge, and even have a little fun! Now that you are a member, you can enjoy the following resources:   The Microsoft Power Apps Community Forums If you are looking for support with any part of Microsoft Power Apps, our forums are the place to go. They are titled "Get Help with Microsoft Power Apps " and there you will find thousands of technical professionals with years of experience who are ready and eager to answer your questions. You now have the ability to post, reply and give "kudos" on the Power Apps community forums! Make sure you conduct a quick search before creating a new post because your question may have already been asked and answered!   Microsoft Power Apps IdeasDo you have an idea to improve the Microsoft Power Apps experience, or a feature request for future product updates? Then the "Power Apps Ideas" section is where you can contribute your suggestions and vote for ideas posted by other community members. We constantly look to the most voted Ideas when planning updates, so your suggestions and votes will always make a difference.   Community Blog & NewsOver the years, more than 600 Power Apps Community Blog Articles have been written and published by our thriving community. Our community members have learned some excellent tips and have keen insights on building Power Apps. On the Power Apps Community Blog, read the latest Power Apps related posts from our community blog authors around the world. Let us know if you would like to become an author and contribute your own writing — everything Power Apps related is welcome!   Power Apps Samples, Learning and Videos GalleriesOur galleries have a little bit of everything to do with Power Apps. Our galleries are great for finding inspiration for your next app or component. You can view, comment and kudo the apps and component gallery to see what others have created! Or share Power Apps that you have created with other Power Apps enthusiasts. Along with all of that awesome content, there is the Power Apps Community Video & MBAS gallery where you can watch tutorials and demos by Microsoft staff, partners, and community gurus in our community video gallery.   Again, we are excited to welcome you to the Microsoft Power Apps community family! Whether you are brand new to the world of process automation or you are a seasoned Power Apps veteran. Our goal is to shape the community to be your ‘go to’ for support, networking, education, inspiration and encouragement as we enjoy this adventure together!   Let us know in the Community Feedback if you have any questions or comments about your community experience.To learn more about the community and your account be sure to visit our Community Support Area boards to learn more! We look forward to seeing you in the Power Apps Community!The Power Apps Team

Top Solution Authors
Top Kudoed Authors
Users online (4,796)