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 / Switch function using ...
Power Apps
Answered

Switch function using ";" or "," between options

(0) ShareShare
ReportReport
Posted on by 124

For some reasons Powerapps refused to accept my code with Switch like this:

 

 

Switch(
varButtonPressed,
"Trash_FileSaved",
<do something>,
"Trash_FileUploaded",
<do something>,
"Trash_AllFilesUploaded",
<do something>
)

 

 

The only way to make it work was the following:

 

 

 

Switch(
varButtonPressed,
"Trash_FileSaved",
<do something>;
"Trash_FileUploaded",
<do something>;
"Trash_AllFilesUploaded",
<do something>
)

 

 

By using ";" between options, not ",". I have never experienced this before.

The code actually works, but is this a normal syntax for the switch function?

 

Categories:
I have the same question (0)
  • RandyHayes Profile Picture
    76,299 Super User 2024 Season 1 on at

    @lk777 

    It's not normal.  Each switch condition is separated by a comma (unless your language entry uses semicolons for separation).

    In your situation, it is most likely the "Do something" that is the factor to consider.  

    In general, formulas return a value.  When you have multiple actions in a formula, the return types either need to be the same or have no result.

     

    I hope this is helpful for you.

  • lk777 Profile Picture
    124 on at

    This is my code with the switch function which caused this behavior:

     

     

     

    Switch(
     varButtonPressed,
     "Trash_FileSaved",
     If(
     varPopResponse.type = "Yes",
     Remove(
     colDocs,
     LookUp(
     colDocs,
     FileNameDoc = galFilesSaved_1.Selected.FileNameDoc
     )
     );
     UpdateContext({varShowPopup: false}),
     varPopResponse.type = "Cancel",
     UpdateContext({varShowPopup: false})
     );
    
     "Trash_FileUploaded",
    
     If(varPopResponse.type = "Yes", 
     UpdateContext({varLoadingSpinnerVisibilty: true});
     If( !IsBlank(varFileSelectedDelete.Link),
     PowerAppsDeleteImageFileSP.Run(varFileSelectedDelete.DocFileID)
     );
     Remove(
     colMerged,
     LookUp(
     colMerged,
     FileNameDoc = galFilesUploaded_1.Selected.FileNameDoc
     )
     );
     Remove(
     colDocs,
     LookUp(
     colDocs,
     FileNameDoc = galFilesSaved_1.Selected.FileNameDoc
     )
     );
     UpdateContext({varShowPopup: false});
     UpdateContext({varLoadingSpinnerVisibilty: false});
     If(
     IsEmpty(colDocs),
     Reset(dropPhysician_1);
     Reset(dateOfService_1);
     Clear(colDocs);
     Clear(colCombined);
     Clear(colMerged)
     ),
     varPopResponse.type="Cancel",
     UpdateContext({varShowPopup: false})
    );
     
     "Trash_AllFilesUploaded",
     
     If(varPopResponse.type = "Yes" ,
     If(
     !IsEmpty(colMerged),
     Set(
     gblAllImageFilesJson,
     JSON(
     colMerged,
     JSONFormat.IgnoreBinaryData
     )
     )
     );
     UpdateContext({varLoadingSpinnerVisibilty: true});
     If(
     !IsBlank(gblAllImageFilesJson),
     PowerAppsDeleteAllImageFilesInFolderSP.Run(gblAllImageFilesJson)
     );
     Clear(colDocs);
     Clear(colCombined);
     Clear(colMerged);
     Set(
     gblAllImageFilesJson,
     Blank()
     );
     UpdateContext({varShowPopup: false});
     UpdateContext({varLoadingSpinnerVisibilty: false});
    	UpdateContext({varDeleteAllFiles: true}),
     varPopResponse.type = "Cancel",
     UpdateContext({varShowPopup: false}))
     
    )

     

     

    This code works. Why?

  • Verified answer
    RandyHayes Profile Picture
    76,299 Super User 2024 Season 1 on at

    @lk777 

    Try refactoring your Formula to the following:

    If(varPopResponse.type = "Yes" , 
     Switch( varButtonPressed, 
     "Trash_FileSaved", 
     UpdateContext({empty: RemoveIf(colDocs, FileNameDoc = galFilesSaved_1.Selected.FileNameDoc)}); 
     UpdateContext({varShowPopup: false}),
     
     "Trash_FileUploaded", 
     UpdateContext({varLoadingSpinnerVisibilty: true}); 
     If( !IsBlank(varFileSelectedDelete.Link), PowerAppsDeleteImageFileSP.Run(varFileSelectedDelete.DocFileID)); 
     UpdateContext({empty: RemoveIf(colMerged, FileNameDoc = galFilesUploaded_1.Selected.FileNameDoc)});
     UpdateContext({empty: RemoveIf(colDocs, FileNameDoc = galFilesSaved_1.Selected.FileNameDoc)}); 
     UpdateContext({varShowPopup: false, varLoadingSpinnerVisibilty: false}); 
     If(IsEmpty(colDocs), 
     Reset(dropPhysician_1); Reset(dateOfService_1); Clear(colDocs); Clear(colCombined); Clear(colMerged) 
     ), 
     
     "Trash_AllFilesUploaded", 
     If( !IsEmpty(colMerged), Set( gblAllImageFilesJson, JSON( colMerged, JSONFormat.IgnoreBinaryData ) ) ); 
     UpdateContext({varLoadingSpinnerVisibilty: true}); 
     If( !IsBlank(gblAllImageFilesJson), PowerAppsDeleteAllImageFilesInFolderSP.Run(gblAllImageFilesJson) ); 
     Clear(colDocs); Clear(colCombined); Clear(colMerged); 
     Set( gblAllImageFilesJson, Blank() ); 
     UpdateContext({varShowPopup: false, varLoadingSpinnerVisibilty: false, varDeleteAllFiles: true}) 
     ),
     varPopResponse.type = "Cancel", 
     UpdateContext({varShowPopup: false})
    ) 
    

    The thing about your Formula is that you have multiple return types in it.  Most return nothing, but Remove returns a table.  So, in the above (first I replaced Remove with RemoveIf, as this will be better performance) I am capturing the result of the table from RemoveIf into a variable.  Not for any other reason except that UpdateContext will return nothing...just list all the rest of your functions in the formula.

     

    See if that works. 

  • lk777 Profile Picture
    124 on at

    If I remove "Trash_FileUploaded" part of the Switch function, PowerApps accepts "," between 2 remaining options. It seems that this removed part causes this issue.

  • RandyHayes Profile Picture
    76,299 Super User 2024 Season 1 on at

    @lk777 

    Ah yes...I missed that one.  Your PowerAutomate call will return a value as well...you should capture that into a variable as well.

     If( !IsBlank(varFileSelectedDelete.Link), UpdateContext({runResult: PowerAppsDeleteImageFileSP.Run(varFileSelectedDelete.DocFileID)}) ); 
  • lk777 Profile Picture
    124 on at

    When I mentioned that part "Trash_FileUploaded", I meant that the original code worked.

    And I tried your code, it actually accepted a flow without capturing a result of a flow into a variable.

    There is something mysterious about this Switch function, my code worked with ";", though it shouldn't ?
    Can it be some Microsoft forgiveness approach to help code illiterate people to do their job 🙂 ?

     

  • RandyHayes Profile Picture
    76,299 Super User 2024 Season 1 on at

    @lk777 

    Not so sure on the last part of that statement.  I do know that in many situations (usually when using formula in a With block) the returns of the conditional (if or switch) formulas have to match signature...otherwise, things go a little wacky. 

  • lk777 Profile Picture
    124 on at

    @RandyHayes 

    I have just checked your code and it works. Thank you. It is good to know what Switch expects as a return. But I am still wondering why my code worked as well.

  • RandyHayes Profile Picture
    76,299 Super User 2024 Season 1 on at

    @lk777 

    Yes...why yours worked is a bit of a mystery.  But, I would suspect that would be one of those things where, it works now, but somewhere down the road (on a new PowerApp release) it doesn't.

  • lk777 Profile Picture
    124 on at

    I decided not to use the Switch function. IF is more flexible:

     

     

    If(varButtonPressed="Trash_FileSaved",
     If(
     varPopResponse.type = "Yes",
     Remove(
     colDocs,
     LookUp(
     colDocs,
     FileNameDoc = galFilesSaved_1.Selected.FileNameDoc
     )
     );
     UpdateContext({varShowPopup: false}),
     varPopResponse.type = "Cancel",
     UpdateContext({varShowPopup: false})
     ),varButtonPressed= "Trash_FileUploaded",
     If(varPopResponse.type = "Yes", 
     UpdateContext({varLoadingSpinnerVisibilty: true});
     If( !IsBlank(varFileSelectedDelete.Link),
     PowerAppsDeleteImageFileSP.Run(varFileSelectedDelete.DocFileID)
     );
     Remove(
     colMerged,
     LookUp(
     colMerged,
     FileNameDoc = galFilesUploaded_1.Selected.FileNameDoc
     )
     );
     Remove(
     colDocs,
     LookUp(
     colDocs,
     FileNameDoc = galFilesSaved_1.Selected.FileNameDoc
     )
     );
     UpdateContext({varShowPopup: false});
     UpdateContext({varLoadingSpinnerVisibilty: false});
     If(
     IsEmpty(colDocs),
     Reset(dropPhysician_1);
     Reset(dateOfService_1);
     Clear(colDocs);
     Clear(colCombined);
     Clear(colMerged)
     ),
     varPopResponse.type="Cancel",
     UpdateContext({varShowPopup: false})
    ),varButtonPressed="Trash_AllFilesUploaded",
     
     If(varPopResponse.type = "Yes" ,
     If(
     !IsEmpty(colMerged),
     Set(
     gblAllImageFilesJson,
     JSON(
     colMerged,
     JSONFormat.IgnoreBinaryData
     )
     )
     );
     UpdateContext({varLoadingSpinnerVisibilty: true});
     If(
     !IsBlank(gblAllImageFilesJson),
     PowerAppsDeleteAllImageFilesInFolderSP.Run(gblAllImageFilesJson)
     );
     Clear(colDocs);
     Clear(colCombined);
     Clear(colMerged);
     Set(
     gblAllImageFilesJson,
     Blank()
     );
     UpdateContext({varShowPopup: false});
     UpdateContext({varLoadingSpinnerVisibilty: false});
    	UpdateContext({varDeleteAllFiles: true}),
     varPopResponse.type = "Cancel",
     UpdateContext({varShowPopup: false}))
     
    );

     

    It looks like the Switch function  is more suitable for something like this:

    Switch(varSelectedFruit, APL,"Apple",  ....) and not for something more complex.

    But I suspect, in my case, using ";" between options, PowerApps translates it as using the IF function. I am just guessing.

     

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!

Congratulations to the April Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
Vish WR Profile Picture

Vish WR 846

#2
Valantis Profile Picture

Valantis 532

#3
Haque Profile Picture

Haque 410

Last 30 days Overall leaderboard