web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Adding a line to a ran...
Power Apps
Answered

Adding a line to a randomized collection

(3) ShareShare
ReportReport
Posted on by 10
I'm trying to do something unusual and creative, but running into errors. Hoping somebody can help me identify a solution. 
 
I essentially have a quiz, and I want PowerApps to create a log of the answers so that I can mark them as correct or not correct (and the correct answer) in a gallery at the end of the quiz. 
I used a ClearCollect and a FirstN(Shuffle to choose 10 questions at random from my SharePoint list and put them into a collection. That works great. 
ClearCollect(PatientRecords, FirstN(Shuffle('MCI Game: Patient Information'),10))
One of the variables in the collection includes the correct answer, and the answer buttons are set up to add to the VarScore if the button they select matches the correct answer. Also works great. 
If(Index(PatientRecords,VarStep).'Best Answer'.Value in "Green",
Set(VarScore, VarScore+5);Set(VarCorrect, VarCorrect+1),false);
Set(VarStep,VarStep+1); If(VarStep = 11, Navigate('Results Screen'),false)
Now, I'm trying to either temporarily write what answer they selected into a column of the collection, so that I can later compare the correct and selected answers to indicate if they got that particular question write OR
Create a new collection and put just the important few variables into it (Question Name, Correct Answer, Selected Answer) so I can display those on the results screen. 
 
I've tried using PATCH, but LOOKUP cannot seem to find that particular line of the collection, because I can't use any particular variable, such as ID number because it's randomized each time.
I tried creating a new collection and adding those few items to it, but it again can't seem to gather the correct row from the randomized collection that allows me to put it into a new collection.
Patch(PatientRecords,Lookup(PatientRecords, VarStep,{ 'Choosen Anwser': "Green"})
Any suggestions?
Categories:
I have the same question (0)
  • Verified answer
    Valantis Profile Picture
    6,735 on at
     
    The issue with your Patch is that LookUp needs a condition that evaluates to true/false per row, not a position number. VarStep is an index, not a column value, so LookUp can't use it directly.
    The simplest fix: use a unique identifier to match the row. Since you're using FirstN(Shuffle(...)), each row still has its original SharePoint ID. Use that to match:
    Patch(
        PatientRecords,
        LookUp(PatientRecords, ID = Index(PatientRecords, VarStep).ID),
        { 'Chosen Answer': "Green" }
    )
    This finds the exact row at position VarStep by its ID and patches the chosen answer into it.
    Alternatively, the cleanest approach for a quiz is to build a separate results collection as you go. On each answer button's OnSelect, add a Collect action:
    Collect(
        colResults,
        {
            Question: Index(PatientRecords, VarStep).'Question Name',
            CorrectAnswer: Index(PatientRecords, VarStep).'Best Answer'.Value,
            SelectedAnswer: "Green"
        }
    )
    This builds the results collection step by step as the user answers. On the Results Screen you display colResults directly. No patching needed, no LookUp issues, and it's easy to compare CorrectAnswer vs SelectedAnswer per row in the gallery.
    The second approach is simpler and more reliable for a quiz pattern.
     

     

    Best regards,

    Valantis

     

    ✅ If this helped solve your issue, please Accept as Solution so others can find it quickly.

    ❤️ If it didn’t fully solve it but was still useful, please click “Yes” on “Was this reply helpful?” or leave a Like :).

    🏷️ For follow-ups  @Valantis.

    📝 https://valantisond365.com/

    💼 LinkedIn

    ▶️ YouTube

  • Suggested answer
    11manish Profile Picture
    3,333 on at
    Your approach is actually very good — the main issue is just with how Patch() and LookUp() are being used against the randomized collection.Your approach is
     
    actually very good — the main issue is just with how Patch() and LookUp() are being used against the randomized collection.
     
    On Answer Button :
     
    Collect(
        QuizResults,
        {
            Question: Index(PatientRecords, VarStep).Title,
            CorrectAnswer: Index(PatientRecords, VarStep).'Best Answer'.Value,
            SelectedAnswer: "Green",
            IsCorrect: Index(PatientRecords, VarStep).'Best Answer'.Value = "Green"
        }
    );
    
    If(
        Index(PatientRecords, VarStep).'Best Answer'.Value = "Green",
        Set(VarScore, VarScore + 5);
        Set(VarCorrect, VarCorrect + 1)
    );
    
    Set(VarStep, VarStep + 1);
    
    If(
        VarStep = 11,
        Navigate('Results Screen')
    )
     
  • Ram Prakash Duraisamy Profile Picture
    5,877 Super User 2026 Season 1 on at
    Hi,
     
    Can you give a try for below code
     
     
    // Initialize (put this in Screen OnVisible or App OnStart)
    ClearCollect(colQuestions, FirstN(Shuffle(PatientRecords), 10));
    Clear(colResults);
    Set(VarStep, 1);
    
    // Answer button (example: Green)
    Collect(
        colResults,
        {
            QuestionID: Last(FirstN(colQuestions, VarStep)).ID,
            Question: Last(FirstN(colQuestions, VarStep)).'Question Name',
            CorrectAnswer: Last(FirstN(colQuestions, VarStep)).'Best Answer'.Value,
            SelectedAnswer: "Green"
        }
    );
    If(
        VarStep < CountRows(colQuestions),
        Set(VarStep, VarStep + 1),
        Navigate(ResultScreen)
    )
    
    // Display current question (Label Text)
    Last(FirstN(colQuestions, VarStep)).'Question Name'
    
    // Results Gallery Items
    colResults
    
    // Score (Label)
    CountIf(colResults, CorrectAnswer = SelectedAnswer)
     
    Please mark as answer if my suggestion helps.
    Subscribe here for More Useful videos : https://www.youtube.com/@rampprakash3991
  • Suggested answer
    timl Profile Picture
    37,214 Super User 2026 Season 1 on at
     
    >> Now, I'm trying to either temporarily write what answer they selected into a column of the collection, so that I can later compare the correct and selected answers to indicate if they got that particular question write 
     
    On the basis that you want to the approach of writing the answer into a column of the PatientRecords collection, the first step is to add a column to store the answer (if you don't already have an existing column in your 'MCI Game: Patient Information' list to store the answer).
     
    You would modify your call to ClearCollect like so. This will add a 'ChosenAnswer' column with a blank value against each question.
     
    ClearCollect(PatientRecords, 
        AddColumns(
          FirstN(Shuffle('MCI Game: Patient Information'),10)),
          ChosenAnswer,
          ""
        )
    )
     
    In your original formula, you're already picking up the record and answer by ordinal number through the use of the Index function. So to Patch, you would use the same approach. The Patch syntax would look like this:
     
    Patch(PatientRecords,
          Index(PatientRecords,VarStep),
          {ChoosenAnwser: "Green"}
    )

     
  • Suggested answer
    Vish WR Profile Picture
    3,748 on at
     
    Instead of trying to update the original randomized collection, the better and simpler approach is to create a separate results collection while the user answers. So on each answer click, you should: * Use Index() to get the current question * Collect() a new collection (e.g. colResults) * Store Question, Correct Answer, and Selected Answer together This avoids lookup issues completely and makes your results screen much easier to build and manage.

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

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
Valantis Profile Picture

Valantis 424

#2
WarrenBelz Profile Picture

WarrenBelz 355 Most Valuable Professional

#3
11manish Profile Picture

11manish 290

Last 30 days Overall leaderboard