Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Power Apps - Power Apps Pro Dev & ISV
Answered

Unable to create a Collection inside a Switch sentence

(0) ShareShare
ReportReport
Posted on by 9

Hi Comunity,

 

I need to create a Collection dinamically depending on the value of a variable. The name of the collection must be always the same because after I show the content in a table but the data source must be different: This is the code I'm trying to use:

 

Switch(
Last(Linea).Value;
      "Renewables"; ClearCollect(Plani; PlaniRen);
      "Corporate"; ClearCollect(Plani; PlaniCorp);
      "Liberalized"; ClearCollect(Plani; PlaniLib);
      "Networks"; ClearCollect(Plani; PlaniNet)
);;

 

If I use only one row, the code works fine but once I add additional line, the system shows me an error in the ClearCollect sentence (Invalid arguments).

 

Does somebody know how to manage?

 

Thanks in advanced,

Edorta

  • v-jefferni Profile Picture
    on at
    Re: Unable to create a Collection inside a Switch sentence

    Hi @Edorta ,

     

    If you renamed only the display names of the columns, this may be the reason. Need to rename list columns in list settings.

     

    Best regards,

    Community Support Team _ Jeffer Ni
    If this post helps, then please consider Accept it as the solution to help the other members find it.

  • Verified answer
    Edorta Profile Picture
    9 on at
    Re: Unable to create a Collection inside a Switch sentence

    Thanks Jeffer,

    I ensured that the estructure of the different Excel files that act as sources for collection filling are exactly the same (I copied the headers) but the error still remains.

     

    Finally I decided to create 5 different collections and make the visualization of the correct one dynamic using a Switch sentence.

     

    Best Regards,

    Edorta

     

  • v-jefferni Profile Picture
    on at
    Re: Unable to create a Collection inside a Switch sentence

    Hi @Edorta ,

     

    The issue is not relate to the Switch statement but to the collection. When you first time use Collect or ClearCollect to create a collection, the data structure is determined and relies on the collection name, which means if you want to update the collection elsewhere you need an exact same data structure.

     

    I assume the four data tables will have columns with different names, so the collection columns will be "dynamic" which is not allowed. To solve this issue, you could use ShowColumns and RenameColumns functions to artificially rename the columns to be totally same:

    Switch(
     Last(Linea).Value;
     "Renewables"; ClearCollect(Plani; RenameColumns(ShowColumns(PlaniRen; "Column1.1";"Column2.1";"Column3.1");"Column1.1";"Column1.2";"Column2.1";"Column2.2";"Column3.1";"Column3.2");
     "Corporate"; ClearCollect(Plani; RenameColumns(ShowColumns(PlaniCorp; "Column1.1";"Column2.1";"Column3.1");"Column1.1";"Column1.2";"Column2.1";"Column2.2";"Column3.1";"Column3.2");
     "Liberalized"; ClearCollect(Plani; RenameColumns(ShowColumns(PlaniLib; "Column1.1";"Column2.1";"Column3.1");"Column1.1";"Column1.2";"Column2.1";"Column2.2";"Column3.1";"Column3.2");
     "Networks"; ClearCollect(Plani; RenameColumns(ShowColumns(PlaniPlaniNet; "Column1.1";"Column2.1";"Column3.1");"Column1.1";"Column1.2";"Column2.1";"Column2.2";"Column3.1";"Column3.2");
    );;

    ShowColumns to retrieve all the columns you need from each data source, RenameColumns to rename them as same columns.

     

    Hope this helps.

     

    Best regards,

    Community Support Team _ Jeffer Ni
    If this post helps, then please consider Accept it as the solution to help the other members find it.

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

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Warren Belz – Community Spotlight

We are honored to recognize Warren Belz as our May 2025 Community…

Congratulations to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Power Apps - Power Apps Pro Dev & ISV

#1
WarrenBelz Profile Picture

WarrenBelz 85 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 57 Super User 2025 Season 1

#3
mmbr1606 Profile Picture

mmbr1606 55 Super User 2025 Season 1

Overall leaderboard