cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Triplee23
Regular Visitor

Adding additional data to nested collection

I have a simple setup but just can't get it to work. Understanding the logic behind nested collections is just a little too difficult. 

I have simplified my code below:

First I create the general collection with a nested collection under Item2:

 

 

ClearCollect(col_test1, {Item1: "Text", Item2: {Child1: "Random"}});

 

 

How do add (Collect) a new row to the nested collection under Item2?

I just cannot get it to work. 

I get Patch to work, changing "Random" to "Random_new".

 

 

Patch(col_test1,LookUp(col_test1,Item1="Text"),{Item2:{Child1:"Random_new"}})

 

 

But adding a new row to the nested collection is just impossible for me. Can anyone help me?

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
v-bofeng-msft
Community Support
Community Support

Hi @Triplee23 :

Do you want to add records to a subtable in the collection?

My method is to use Split and concat to construct the table to update the data.

The key is to construct a new table as the value to be updated.

Case1:The value of Iterm2 is a single column table

Ceate collection:

 

ClearCollect(col_test1,{Item1: "Text", Item2: Table({Child1: "Random"})})

 

Add Record:

 

Patch(
    col_test1,
    LookUp(col_test1,Item1="Text"),
    {
        Item2: RenameColumns(
            Split(
                Concatenate(
                    Concat(
                        LookUp(
                            col_test1,
                            Item1 = "Text"
                        ).Item2,
                        Child1,
                        "#"
                    ),
                    "#","Random_new"
                ),
                "#"
            ),
            "Result",
            "Child1"
        )
    }
)

 

1.JPG

Case2:The value of Iterm2 is a multiple columns table

Ceate collection:

 

ClearCollect(col_test1, {Item1: "Text", Item2: Table({Child1: "Random"})})

 

 Add Record:

 

Patch(
    col_test1,
    LookUp(
        col_test1,
        Item1 = "Text"
    ),
    {
        Item2: ShowColumns(
            AddColumns(
                Split(
                    Concatenate(
                        Concat(
                            Concatenate(
                                LookUp(
                                    col_test1,
                                    Item1 = "Text"
                                ).Item2.Child1,
                                "%%",
                                LookUp(
                                    col_test1,
                                    Item1 = "Text"
                                ).Item2.Child2,
                                "%%",
                                LookUp(
                                    col_test1,
                                    Item1 = "Text"
                                ).Item2.Child3
                            ),
                            Result,
                            "#"
                        ),
                        "#",
                        "Random_new",
                        "%%",
                        "Random_new",
                        "%%",
                        "Random_new"
                    ),
                    "#"
                ),
                "Child1",
                First(
                    Split(
                        Result,
                        "%%"
                    )
                ).Result,
                "Child2",
                Last(
                    FirstN(
                        Split(
                            Result,
                            "%%"
                        ),
                        2
                    )
                ).Result,
                "Child3",
                Last(
                    FirstN(
                        Split(
                            Result,
                            "%%"
                        ),
                        3
                    )
                ).Result
            ),
            "Child1",
            "Child2",
            "Child3"
        )
    }
)

 

2.JPG

Best Regards,

Bof

 

View solution in original post

3 REPLIES 3
ZabiBabar
Continued Contributor
Continued Contributor

The way you are doing it,  will always overwrite the Item2 because Item2 is not a collection or a table. It is record with one column. To add a new child to item 2 you can do the following.

I did this on my end with 2 collections so you can add to them separately. 

I added this line on the app OnStart property 

ClearCollect(col_test2, {Child1: "Random"}); ClearCollect(col_test1, {Item1: "Text", Item2: col_test2});

And this on a button which adds a new item

Patch(col_test2, Defaults(col_test2), {Child1: "New Random"}); ClearCollect(col_test1, {Item1: "Text", Item2: col_test2});

 

Thanks for the explanation, I got it to work. I understand that the principle here is that I create 2 collections and place one inside the other. 

 

I am planning to use this collection (col_test1) in a nested Gallery, Gallery_Child (Item2: col_test2 collection) inside Gallery_Parent (col_test1 collection). With you solution, I would need as many child collections as records in col_test1.

 

Keeping your first ClearCollect, is there no way to avoid your second piece of code, meaning adding a new record to Item2 without going through Patch(col_test2)? Can I not add a new record directly referring only to col_test1?

v-bofeng-msft
Community Support
Community Support

Hi @Triplee23 :

Do you want to add records to a subtable in the collection?

My method is to use Split and concat to construct the table to update the data.

The key is to construct a new table as the value to be updated.

Case1:The value of Iterm2 is a single column table

Ceate collection:

 

ClearCollect(col_test1,{Item1: "Text", Item2: Table({Child1: "Random"})})

 

Add Record:

 

Patch(
    col_test1,
    LookUp(col_test1,Item1="Text"),
    {
        Item2: RenameColumns(
            Split(
                Concatenate(
                    Concat(
                        LookUp(
                            col_test1,
                            Item1 = "Text"
                        ).Item2,
                        Child1,
                        "#"
                    ),
                    "#","Random_new"
                ),
                "#"
            ),
            "Result",
            "Child1"
        )
    }
)

 

1.JPG

Case2:The value of Iterm2 is a multiple columns table

Ceate collection:

 

ClearCollect(col_test1, {Item1: "Text", Item2: Table({Child1: "Random"})})

 

 Add Record:

 

Patch(
    col_test1,
    LookUp(
        col_test1,
        Item1 = "Text"
    ),
    {
        Item2: ShowColumns(
            AddColumns(
                Split(
                    Concatenate(
                        Concat(
                            Concatenate(
                                LookUp(
                                    col_test1,
                                    Item1 = "Text"
                                ).Item2.Child1,
                                "%%",
                                LookUp(
                                    col_test1,
                                    Item1 = "Text"
                                ).Item2.Child2,
                                "%%",
                                LookUp(
                                    col_test1,
                                    Item1 = "Text"
                                ).Item2.Child3
                            ),
                            Result,
                            "#"
                        ),
                        "#",
                        "Random_new",
                        "%%",
                        "Random_new",
                        "%%",
                        "Random_new"
                    ),
                    "#"
                ),
                "Child1",
                First(
                    Split(
                        Result,
                        "%%"
                    )
                ).Result,
                "Child2",
                Last(
                    FirstN(
                        Split(
                            Result,
                            "%%"
                        ),
                        2
                    )
                ).Result,
                "Child3",
                Last(
                    FirstN(
                        Split(
                            Result,
                            "%%"
                        ),
                        3
                    )
                ).Result
            ),
            "Child1",
            "Child2",
            "Child3"
        )
    }
)

 

2.JPG

Best Regards,

Bof

 

Helpful resources

Announcements
PA Virtual Workshop Carousel 768x460.png

Register for a Free Workshop

This training provides practical hands-on experience in creating Power Apps solutions in a full-day of instructor-led App creation workshop.

Microsoft Build 768x460.png

Microsoft Build is May 24-26. Have you registered yet?

Come together to explore latest innovations in code and application development—and gain insights from experts from around the world.

May UG Leader Call Carousel 768x460.png

What difference can a User Group make for you?

At the monthly call, connect with other leaders and find out how community makes your experience even better.

Top Solution Authors
Top Kudoed Authors
Users online (1,690)