cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
tianaranjo
Continued Contributor
Continued Contributor

Add ID to collection

 

Looking for help with creation of a collection utilized only within the app, no external datasource.  I want to include an ID column in the collection with a count of each item.  The trick is that the data being collected is from a ComboBox so all standard ways of adding the id are not working, i.e. Collect(colThisItem, {id: CountRows(colThisItem) + 1, Result: ComboBox.Selected}) (@ericonline - thanks for the formula).  

 

The ComboBox1 items are populated from another collection:

  • Items - colComboBoxChoices
  • OnChange - Collection(colSelectedItems, ComboBox.Selected)

This works fine for collecting.  However, if I add the CountRows within the OnChange formula , it does not work as intended.  Again, I believe it has to do with the results for the colSelectedItems coming from a ComboBox - just not sure how to resolve.

Help would be appreciated.

1 ACCEPTED SOLUTION

Accepted Solutions

Thank you for clarification. Pease try setting this to OnChange:

Clear(colThisItem);ForAll(ComboBox1.SelectedItems, Collect(colThisItem, {ID: CountRows(colThisItem)+1, Result:Last(FirstN(ComboBox1.SelectedItems, CountRows(colThisItem)+1)).Value}))

If you're experimenting with a new ComboBox control with a default data set then change the last "Value" to "Value1".

Let me know if you have any questions in regards to the proposed formula.

View solution in original post

8 REPLIES 8
dinusc
Microsoft
Microsoft

Hello tianaranjo,

 

Please note that "OnChange" on the ComboBox is actually fired every time the selection is changed internally or by user interaction. As a result, it fires far more often that expected. Please consider one of the following workarounds:

1. Replace ComboBox control with a DropDown, which fires the "OnChange" only on user interaction (on closing the flyout). DropDown does not support multiple selections but since you're using ComboBox.Selected (and not ComboBox.SelectedItems), I assume you do not need that.

2. Store the previously selected value and perform the increment (+1) only in case the currently selected value is different from the previously selected value (in which case the previous value is also updated). This way, the "OnChange" event will fire only once.

Hope this helps.

tianaranjo
Continued Contributor
Continued Contributor

@dinusc  Thanks for your reply.  Thing is, I do need the user to be able to select multiple items if they choose; that need eliminated ability to utilize drop down.  

 

Any other thoughts that would allow user to select one or more?

 

Thanks again!

Just so I understand the intended behavior, let's say I have a ComboBox with 10 items. If I want to select 3 of them (1, 4 and 6 for example), do you want to have a collection with 3 rows as following?

{id:1, Result:["Item1"]},{id:2, Result:["Item1,"Item4""]},{id:3, Result:["Item1","Item4","Item6]}

 

If you need just one row (as the last one) in your collection then you will need to have a button that the user clicks when he/she is done selecting items.

 

In your formula, you use ComboBox.Selected which should not be used in multi-select mode.

tianaranjo
Continued Contributor
Continued Contributor

So, if you have 10 items and select three used in example below, I would like them to appear in collection as:

ID  Result

1    Item1

2    Item4

3    Item6

 

A user might pick one of the selected or all 10. Did this help?

 

If so, how do I:

  1. Create a collection so the results are as shown above
  2. What do I do in place of ComboBox.Selected

Thanks!  I've been banging my head against my desk for days on this one.

Thank you for clarification. Pease try setting this to OnChange:

Clear(colThisItem);ForAll(ComboBox1.SelectedItems, Collect(colThisItem, {ID: CountRows(colThisItem)+1, Result:Last(FirstN(ComboBox1.SelectedItems, CountRows(colThisItem)+1)).Value}))

If you're experimenting with a new ComboBox control with a default data set then change the last "Value" to "Value1".

Let me know if you have any questions in regards to the proposed formula.

View solution in original post

Thank you so much for this @dinusc !

It worked for me but I needed to experiment with it a bit.

But could you please explain in detail the formula

ForAll(ComboBox1.SelectedItems, 
Collect(colThisItem,
{ID: CountRows(colThisItem)+1,
Result:Last(
FirstN(ComboBox1.SelectedItems, CountRows(colThisItem)+1)).Value}))

 Especially the Result:Last(... part?

Thanks

Hello @peter_not . As you probably noticed, the "Result" is just a field name. Note the "Collect" function which adds one item from SelectedItems to colThisItem pear each ForAll loop iteration. The Last(FirstN(sourceCol, number)) is just a workaround to extract the Nth item. The same can be done by First(LastN(sourceCol, number). For example, on the fourth iteration (N = 4), the thisColItem contains 3 items (since the first 3 iterations completed) so the "FirstN(ComboBox1.SelectedItems, CountRows(colThisItem)+1)" will reference the fourth item from "ComboBox1.SelectedItems" in order to add it to the "colThisItem". I hope this helps. 

Thank you for sharing this solution! It really helped me in building my app!

Helpful resources

Announcements
PA_User Group Leader_768x460.jpg

Manage your user group events

Check out the News & Announcements to learn more.

Power Query PA Forum 768x460.png

Check it out!

Did you know that you can visit the Power Query Forum in Power BI and now Power Apps

Carousel 2021 Release Wave 2 Plan 768x460.jpg

2021 Release Wave 2 Plan

Power Platform release plan for the 2021 release wave 2 describes all new features releasing from October 2021 through March 2022.

PowerPlatform 768x460.png

Microsoft Learn

Check out our new Discover Your Career Path blog post series and get all the details.

Users online (1,129)