web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / I don't see it... Swit...
Power Apps
Answered

I don't see it... Switch statement and if statement together with collections

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

Hi guys,

 

I've been struggling with this for quite some time now and I don't know what is going on.. I don't see it 😞

 

I build a PowerApp that needs to distribute players between 1 and 4 collections of teams (it's a game). 

So I have 4 teams, with matching collections:

- colBlueTeam

- colPurpleTeam

- colGreenTeam

- colOrangeTeam

 

First I select how many teams it needs to create via a dropdown menu. In the OnSelect I create all the collections and remove the first blank record in the collection and I give the variable "varNextColor" the value: "blue" since the blue team is always the first team who gets filled with players.

 

 

ClearCollect(
 colBlueTeam,
 {PlayerBlue: Blank()}
);
ClearCollect(
 colPurpleTeam,
 {PlayerPurple: Blank()}
);
ClearCollect(
 colGreenTeam,
 {PlayerGreen: Blank()}
);
ClearCollect(
 colOrangeTeam,
 {PlayerOrange: Blank()}
);
Remove(
 colBlueTeam,
 First(colBlueTeam)
);
Remove(
 colPurpleTeam,
 First(colPurpleTeam)
);
Remove(
 colGreenTeam,
 First(colGreenTeam)
);
Remove(
 colOrangeTeam,
 First(colOrangeTeam)
);
UpdateContext({varNextColor: "blue"})

 

 

 I then add members using a textbox and add them to a collection "colAllPlayers"

Then I press a button: "Divide players randomly". Which does the following:

 

 

UpdateContext({varRandomPlayer: First(Shuffle(ColAllPlayers).Name)});
Remove(
 ColAllPlayers,
 {Name: varRandomPlayer.Name}
);
Switch(
 varNumberOfTeams.Value,
 2,
 If(
 varNextColor = "blue",
 UpdateContext({varNextColor: "purple"});
 Collect(
 colBlueTeam,
 {PlayerBlue: varRandomPlayer.Name}
 ),
 UpdateContext({varNextColor: "blue"});
 Collect(
 colPurpleTeam,
 {PlayerPurple: varRandomPlayer.Name}
 )
 ),
 3,
 If(
 varNextColor = "blue",
 UpdateContext({varNextColor: "purple"});
 Collect(
 colBlueTeam,
 {PlayerBlue: varRandomPlayer.Name}
 );
 
 );
 If(
 varNextColor = "purple",
 UpdateContext({varNextColor: "green"});
 Collect(
 colPurpleTeam,
 {PlayerPurple: varRandomPlayer.Name}
 );
 );
 If(
 varNextColor = "green",
 UpdateContext({varNextColor: "blue"});
 Collect(
 colGreenTeam,
 {PlayerGreen: varRandomPlayer.Name}
 );
 ),
 4,
 If(
 varNextColor = "blue",
 Collect(
 colBlueTeam,
 {PlayerBlue: varRandomPlayer.Name}
 );
 UpdateContext({varNextColor: "purple"})
 );
 If(
 varNextColor = "purple",
 Collect(
 colPurpleTeam,
 {PlayerPurple: varRandomPlayer.Name}
 );
 UpdateContext({varNextColor: "green"})
 );
 If(
 varNextColor = "green",
 Collect(
 colGreenTeam,
 {PlayerGreen: varRandomPlayer.Name}
 );
 UpdateContext({varNextColor: "orange"})
 );
 If(
 varNextColor = "orange",
 Collect(
 colOrangeTeam,
 {PlayerOrange: varRandomPlayer.Name}
 );
 UpdateContext({varNextColor: "blue"})
 )
)

 

 

 

 I then get a random record from the collection colAllPlayers and I remove the name from the collection since it's going to be assigned to a team. 

So I use a switch to determine how many teams are selected (minimal is 2 teams, maximum is 4). If in the dropdown menu the value: 3 is selected. I want to add someone to the blue team first since that is the default first team. Then I update the varNextColor value with purple, so if I click on the divide button again (you have to keep pressing it till all players have been assigned) it needs to:

- skip the first IF statement since the color is purple not blue.

- enter the second IF statement since the color is purple, add a player to the purple team. Change the variable varNextColor to green. etc.... etc... 

 

But what is actually happening is:

 

- I press the divide button, the first user is being added to all the collections.... Instead of the collection I am specifying. I checked via File -> Collections that there 4 TeamCollections, and each of them have the same users added.. Also the varNextColor does not get updated, it keeps on blue. So there is something going wrong.... 

 

How?!

 

**** I fixed it by using another switch statement instead of all those if statements. But I still would like to know why it wasn't working with the if statements.

 

 

Categories:
I have the same question (0)
  • Verified answer
    v-bofeng-msft Profile Picture
    Microsoft Employee on at

    Hi @ dENNtoetert:

    Firstly, let me explain why you why you encountered this problem.(there 4 TeamCollections, and each of them have the same users added.. Also the varNextColor does not get updated, it keeps on blue. So there is something going wrong.... )

    The point is formulas separated by ";" will run one by one in order.

    For example, I assume varNumberOfTeams.Value=4:

    While you select the button(first time):

    The first persons name will be add to colPurpleTeam, and the varNextColor will be set to purple. But the process will not break, It will also continue to execute this code:

     If(
     varNextColor = "purple",
     Collect(
     colPurpleTeam,
     {PlayerPurple: varRandomPlayer.Name}
     );
     UpdateContext({varNextColor: "green"})
    );

    At this time, Since the varNextColor had been set to purple ,the condition "VarNextColor =" purple " will be triggered.

    Until the entire formula is executed, the same name will be added to these 4 collections.

    Secondly, I suggest you try this code:

    UpdateContext({varRandomPlayer: First(Shuffle(ColAllPlayers).Name)});
    Remove(
     ColAllPlayers,
     {Name: varRandomPlayer.Name}
    );
    Switch(
     varNumberOfTeams.Value,
    2,
     Switch(varNextColor,
     "blue",
     UpdateContext({varNextColor: "purple"});Collect(colBlueTeam,{PlayerBlue: varRandomPlayer.Name}),
     UpdateContext({varNextColor: "blue"});Collect(colPurpleTeam,{PlayerPurple: varRandomPlayer.Name})
     ),
     3,
     Switch(
     varNextColor,
     "blue",
     UpdateContext({varNextColor: "purple"});Collect(colBlueTeam,{PlayerBlue: varRandomPlayer.Name}),
     "purple",
     UpdateContext({varNextColor: "green"});Collect(colPurpleTeam,{PlayerPurple: varRandomPlayer.Name}),
     "green",
     UpdateContext({varNextColor: "blue"});Collect(colGreenTeam,{PlayerGreen: varRandomPlayer.Name})),
     4,
     Switch(
     varNextColor,
     "blue",
     Collect(colBlueTeam,{PlayerBlue: varRandomPlayer.Name});UpdateContext({varNextColor: "purple"}),
     "purple",
     Collect(colPurpleTeam,{PlayerPurple: varRandomPlayer.Name});UpdateContext({varNextColor: "green"}),
     "green",
     Collect(colGreenTeam,{PlayerGreen: varRandomPlayer.Name});UpdateContext({varNextColor: "orange"}),
     "orange",
     Collect(colOrangeTeam,{PlayerOrange: varRandomPlayer.Name});UpdateContext({varNextColor: "blue"}))
    )

     

    Best Regards,

    Bof

  • Community Power Platform Member Profile Picture
    Microsoft Employee on at

    Aaaaaaahhh right!!! That's why, I thought it would break the process but it didn't. Awesome! Many thanks!

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Users!

Kudos to our 2025 Community Spotlight Honorees

Congratulations to our 2025 community superstars!

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 549 Most Valuable Professional

#2
Kalathiya Profile Picture

Kalathiya 225 Super User 2026 Season 1

#3
Haque Profile Picture

Haque 224

Last 30 days Overall leaderboard