cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
HansHeintz
Helper V
Helper V

Patching an item index number into a collection

I would like to have a column in my collection representing the index number for that item.

For that purpose I try to use the ForAll statement.

I am trying this

 

;;Clear(DummyCollection)
;;ForAll(DitDierEvents
			;Collect(DummyCollection;Blank())
			 ;;Patch(DitDierEvents;ThisItem;{Index:CountRows(DummyCollection)})
)

ThisItem does not compute in this construction I know, but how could I do this the right way

 

3 REPLIES 3
RezaDorrani
Dual Super User II
Dual Super User II

Hi @HansHeintz 

 

Below is example of a collection and how to create indexes in it

 

ClearCollect(colNonIndexed,{Class:"Cl1",Category:"Cat1",Item:"Item1"},{Class:"Cl1",Category:"Cat2",Item:"Item1"});ForAll(
colNonIndexed,
Collect(
colIndexed,
{
Class:colNonIndexed[@Class],
Category:colNonIndexed[@Category], Item:colNonIndexed[@Item],
Index:CountRows(colIndexed)+1
}
)
);

 

 

Regards,

Reza Dorrani

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

v-xida-msft
Community Support
Community Support

Hi @HansHeintz ,

Could you please share a bit more about your scenario?

Do you want to add a Row Index column value into your DitDierEvents collection?

Is there a Index column existed in your DitDierEvents collection?

 

I assume that there is already a Index type column existed in your DitDierEvents collection, based on the formula you provided, I think there is something wrong with it.

 

I have made a test on my side, please take a try with the following workaround:

Please consider modify your formula as below:

 

Clear(DummyCollection);;
ClearCollect(TempCollection, DitDierEvents);;
ForAll(
TempCollection;
Collect(DummyCollection; 1);; /* <-- Please type 1 rather than Blank() into your DumyCollection */
Patch(DitDierEvents; Last(FirstN(DitDierEvents, CountRows(DummyCollection))); {Index: CountRows(DummyCollection)}) )

Please take a try with above solution, then check if the issue is solved.

 

More details about the ForAll function, and Patch function, please check the following article:

ForAll function, Patch function

 

Best regards,

 

Community Support Team _ Kris Dai
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
CarlosFigueira
Power Apps
Power Apps

Using the ForAll function and assuming that it will run sequentially is not a safe thing to do. From its documentation:

When writing your formula, keep in mind that records can be processed in any order and, when possible, in parallel.

So if you use it, it's possible that while it works today, it may not work in all devices or tomorrow.

One way to add an index to a collection (or more specifically, to create a second collection with indices and the same items as the original collection) you can start from a collection of indices only, and use the AddColumns function to add the items from the original collection to it. For example, if you have this expression:

ClearCollect(
    Indexed1,
    AddColumns(
        RenameColumns(
            FirstN([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30], CountRows(DitDierEvents)),
            "Value", "Index"),
        "Item", Last(FirstN(DitDierEvents, Index))))

It will create a collection called Indexed1 with two properties: Index (number) and Item (record). You need to define the indices collection as large as you need (in the example above, it would work for collections up to 30 elements). You can then access the properties of the original item, for example in a label in a gallery:

ThisItem.Index & " - " & ThisItem.Item.Name

If you don't want to have the original item as a record, instead having all of its properties directly on the records of the new collection, you can add them over in the AddColumns call, like in the example below:

ClearCollect(
    Indexed2,
    AddColumns(
        RenameColumns(
            FirstN([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30], CountRows(DitDierEvents)),
            "Value", "Index"),
        "Name", Last(FirstN(DitDierEvents, Index)).Name,
        "Age", Last(FirstN(DitDierEvents, Index)).Age,
        "IsHuman", Last(FirstN(DitDierEvents, Index)).IsHuman))

The attached app shows those expressions. To open it, save it locally, then go to https://create.powerapps.com, select Open, Browse, then find the app that you saved previously.

Hope this helps!

Helpful resources

Announcements
PA User Group

Welcome to the User Group Public Preview

Check out new user group experience and if you are a leader please create your group

secondImage

Demo Extravaganza is Back!

We are excited to announce that Demo Extravaganza for 2021 has started!

MBAS on Demand

Microsoft Business Applications Summit sessions

On-demand access to all the great content presented by the product teams and community members! #MSBizAppsSummit #CommunityRocks

Power Apps June 2021

June Power Apps Community Call

Did you miss the call? Check out the recording here!

Top Solution Authors
Top Kudoed Authors
Users online (77,969)