cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
opit
Helper II
Helper II

how to transform data from one type of collection to other

Hi,

Could somebody give me some ideas for below problem?

I have collection like below:

 

ClearCollect(_testGamyba;
    {Id: 1; OperacijosId: 1; Tipas: "Fasadas"; Reiksme: 2};
    {Id: 2; OperacijosId: 1; Tipas: "Korpusas"; Reiksme: 0};
    {Id: 3; OperacijosId: 2; Tipas: "Fasadas"; Reiksme: 1};
    {Id: 4; OperacijosId: 2; Tipas: "Korpusas"; Reiksme: 0};
    {Id: 5; OperacijosId: 3; Tipas: "Fasadas"; Reiksme: 0};
    {Id: 6; OperacijosId: 3; Tipas: "Korpusas"; Reiksme: 2};
    {Id: 7; OperacijosId: 4; Tipas: "Fasadas"; Reiksme: 0};
    {Id: 8; OperacijosId: 4; Tipas: "Korpusas"; Reiksme: 2}
)

And I would like to transform it to following on screen loading:

 

ClearCollect(_testGamyba;
    {Id: 1; OperacijosId: 1; Fasadas: 2; Korpusas: 0};
    {Id: 2; OperacijosId: 2; Fasadas: 1; Korpusas: 0};
    {Id: 3; OperacijosId: 3; Fasadas: 0; Korpusas: 2};
    {Id: 4; OperacijosId: 4; Fasadas: 0; Korpusas: 2}
)

The purpose of that is to transform data to be available for form with checkboxes:

Capture.PNG

1 ACCEPTED SOLUTION

Accepted Solutions

@opit 

I believe that @v-yutliu-msft has the right info for you.  I had worked this out as well, so thought I would share also.

 

@v-yutliu-msft - didn't see your posting until after I had already took a crack at this...

 

Here is what I had come up with for your OnVisible action (you did mention you wanted this to happen on screen load).

Clear(_testGamybaTransform);

ForAll(
    AddColumns(
        GroupBy(
            AddColumns(_testGamyba, "Fas", If(Tipas="Fasadas", Reiksme,0), "Kor", If(Tipas="Korpusas", Reiksme,0)),
           "OperacijosId", "Items"),
    "Fasadas", Sum(Items, Fas),
    "Korpusas", Sum(Items, Kor)
    ),
    Collect(_testGamybaTransform,
    {Id: CountRows(_testGamybaTransform)+1,
     OperacijosId: OperacijosId,
     Fasadas: Fasadas,
     Korpusas: Korpusas
    }
    )
)

Pretty much the same formula, but combined into one.  The only difference was with the Id.  I wasn't sure if your OperacijosId would always be the same as your "Id" that you wanted, so, my formula calculates the sequential Id as it builds the collection.

 

I hope the two formulas provided here are helpful for you.

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

View solution in original post

2 REPLIES 2
v-yutliu-msft
Community Support
Community Support

Hi @opit,

Do you want to transfer collection1 to the format of collection2?

I’ve made a similar test as follows:

Create four buttons

Set button1’s OnSelect:

ClearCollect(_testGamyba,{Id: 1,OperacijosId:1,Tipas: "Fasadas",Reiksme: 2},
    {Id: 2,OperacijosId: 1,Tipas: "Korpusas", Reiksme: 0},
    {Id: 3, OperacijosId: 2,Tipas: "Fasadas",Reiksme: 1},
    {Id: 4,OperacijosId: 2,Tipas: "Korpusas",Reiksme: 0},
    {Id: 5,OperacijosId: 3,Tipas: "Fasadas",Reiksme: 0},
    {Id: 6,OperacijosId: 3,Tipas: "Korpusas", Reiksme: 2},
    {Id: 7,OperacijosId: 4,Tipas: "Fasadas",Reiksme: 0},
    {Id: 8, OperacijosId: 4,Tipas: "Korpusas",Reiksme: 2}
)                                                                                                                                     
                                                                        /////create collection1

4221.jpg

 

Set button2’s OnSelect:

ClearCollect(_testGamyba1,AddColumns(_testGamyba,
"Fasadas",If(Tipas="Fasadas",Reiksme,Blank()),
"Korpusas",If(Tipas="Korpusas",Reiksme,Blank())
)
)                    
        ////update value to Fasadas、Korpusas column

4222.jpg

 

Set button3’s OnSelect:

ClearCollect(_testGamyba2,GroupBy(_testGamyba1,"OperacijosId","old"))       
                                                           /////group according to OperacijosId column

4223.jpg

 

4224.jpg

 

Set button4’s OnSelect:

ClearCollect(_testGamyba3,AddColumns(_testGamyba2,
"id",OperacijosId,
"Fasadas",Concat(Filter(old,!IsBlank(Fasadas)),Fasadas&""),
"Korpusas",Concat(Filter(old,!IsBlank(Korpusas)),Korpusas&"")
)
)     
/////combine two fields’ value

4226.png

 

After these practices, collection1 will be transferred to the format of collection2.

 

 

 

 

 

Best regards,

Community Support Team _ Phoebe Liu

Community Support Team _ Phoebe Liu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

@opit 

I believe that @v-yutliu-msft has the right info for you.  I had worked this out as well, so thought I would share also.

 

@v-yutliu-msft - didn't see your posting until after I had already took a crack at this...

 

Here is what I had come up with for your OnVisible action (you did mention you wanted this to happen on screen load).

Clear(_testGamybaTransform);

ForAll(
    AddColumns(
        GroupBy(
            AddColumns(_testGamyba, "Fas", If(Tipas="Fasadas", Reiksme,0), "Kor", If(Tipas="Korpusas", Reiksme,0)),
           "OperacijosId", "Items"),
    "Fasadas", Sum(Items, Fas),
    "Korpusas", Sum(Items, Kor)
    ),
    Collect(_testGamybaTransform,
    {Id: CountRows(_testGamybaTransform)+1,
     OperacijosId: OperacijosId,
     Fasadas: Fasadas,
     Korpusas: Korpusas
    }
    )
)

Pretty much the same formula, but combined into one.  The only difference was with the Id.  I wasn't sure if your OperacijosId would always be the same as your "Id" that you wanted, so, my formula calculates the sequential Id as it builds the collection.

 

I hope the two formulas provided here are helpful for you.

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

Helpful resources

Announcements
Microsoft 365 Conference – December 6-8, 2022

Microsoft 365 Conference – December 6-8, 2022

Join us in Las Vegas to experience community, incredible learning opportunities, and connections that will help grow skills, know-how, and more.

Difinity Conference 2022

Difinity Conference 2022

Register today for two amazing days of learning, featuring intensive learning sessions across multiple tracks, led by engaging and dynamic experts.

European SharePoint Conference

European SharePoint Conference

The European SharePoint Conference returns live and in-person November 28-December 1 with 4 Microsoft Keynotes, 9 Tutorials, and 120 Sessions.

Power Apps Ideas

Changes to Ideas Coming

We are excited to announce a new way to share your ideas for Power Apps!

Top Solution Authors
Top Kudoed Authors
Users online (2,578)