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

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / how to use if statemen...
Power Apps
Unanswered

how to use if statement or switch when commas are involved?

(0) ShareShare
ReportReport
Posted on by 256

I'm really struggling with power apps language because other languages that use semi-colons tell the language processor it's an end of line. Unfortunately what I'm discovering with power apps is that the comma (,) becomes the delimiter in IF (and switch) statements that really make it hard to do something like this. Here is my objective:

 

I have a pulldown menu that I was able to create a collection of my values (from SPList) and add another value to the top called "All". This way the top of the pulldown has "All" first, and then all the distinct values from my SPList column. Let's call that pulldown list 'PulldownList' for now. I also have a searchbox that is called "SearchInput.Text" that will filter the final results. The last step is to sortbycolumns. Here is what I'd like to do:

 

If(PulldownList.Selected.Result = "All",
   SortByColumns(
      Filter(
         Filter(
            colResults
            ),
           SearchInput.Text in Title
         ),
      ascending
   )

, // (This comma is the ELSE for the If Statement)

   SortByColumns(
      Filter(
         Filter(
            colResults,
               'Person'.DisplayName = 'PulldownList'.DisplayName
            ),
           SearchInput.Text in Title
         ),
      ascending
   )

) //This closes the IF statement

 

So basically, if the selection in the dropdown is "All", then perform the code in orange. If it's not all, sort by the person's name (or selected result form the pulldown) and execute the blue code.

 

Now you might be asking me, well, why don't you just put an if statement inside the second filter instead of using the whole filter command twice - well this is where I'm still stuck because there's a comma required in the if statement that I haven't figured out how to tell IF to not look at and bypass it. So for instance:

 

So take a standard sortbycolumns filter I'm using like this and attempt to inject an if statement right where it counts:

 

  SortByColumns(
      Filter(
         Filter(
            colResults,
                  If(PulldownList.Selected.Result <> "All", 'Person'.DisplayName = 'PulldownList'.DisplayName)
            ),
           SearchInput.Text in Title
         ),
      ascending
   )

 

the problem I face is the comma after the colResults.  In Filter, you have to pass a comma after the data source to indicate there is a condition to be met. Well, in this case, if the pulldown menu selection is "All", I just want it to be:

 

Filter(colResults)

and not

filter(colResults,)  //Note the comma after colResults

 

but I can't pass a comma in the IF statement, because the comma tells IF that it's part of the condition for IF. So my question to all you power apps users, is how do you pass a full filter statement, complete with commas, in an if condition?  I've tried this with switch also, but it doesn't work. I'm sure there's someone out there who has done this, but I'm so accustomed to other languages that use semicolons to end lines, that commas aren't an issue.

 

Anyone come across this before?

 

Thanks in advance!

Categories:
I have the same question (0)
  • Verified answer
    WarrenBelz Profile Picture
    153,040 Most Valuable Professional on at

    Hi @galos ,

    Firstly try this (note you left out the sort field) and also I assume your drop-down has a Distinct filter in it.

    SortByColumns(
     Filter(
     colResults,
     (
     PulldownList.Selected.Result = "All" ||
     'Person'.DisplayName = 'PulldownList'.Selected.Result
     ) &&
     SearchInput.Text in Title
     ),
     "YourSortFieldHere",
     Ascending
    )

     

    Please click Accept as solution if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider giving it Thumbs Up.

    MVP (Business Applications)   Visit my blog Practical Power Apps

     

  • galos Profile Picture
    256 on at

    Thank you @WarrenBelz !!! I'm not sure what is being passed to the filter command with "PulldownList.Selected.Result = "All"  but this is working!! Thank you so much!

  • WarrenBelz Profile Picture
    153,040 Most Valuable Professional on at

    HI @galos ,

    This bit

    (
     PulldownList.Selected.Result = "All" ||
     'Person'.DisplayName = 'PulldownList'.Selected.Result
    ) 

    is simply an either/or statement - if the first is true, then do not filter (use all records) or if not, filter on the second line. The second filter then acts in conjunction with the output of this,

     

  • galos Profile Picture
    256 on at

    ah ok, I see now. @WarrenBelz one other question. I do have another screen where I'm searching multiple columns of people. So in this example I have 'Person'.DisplayName, but I also have another pulldown with 'Person2'.displayName and 'Person3'.displayname. These are mapped based on heirarchy in the company (ie: each person's leadership). How would I do this with 3 pulldowns on the same screen? Any recommendations? (all three would have All along with each manager's name)

    Thanks in advance!!

  • WarrenBelz Profile Picture
    153,040 Most Valuable Professional on at

    @galos ,

    I am not totally clear on what you want here, but the pattern would be

    (
     PulldownList.Selected.Result = "All" ||
     'Person'.DisplayName = 'PulldownList'.Selected.Result
    ) &&
    (
     PulldownList2.Selected.Result = "All" ||
     'Person2'.DisplayName = 'PulldownList2'.Selected.Result
    ) &&
    (
     PulldownList3.Selected.Result = "All" ||
     'Person3'.DisplayName = 'PulldownList3'.Selected.Result
    )
    
  • galos Profile Picture
    256 on at

    @WarrenBelzthanks again. I'm going to try experimenting with this and post back with updates. One last question - I'm trying to add a second search criteria using the original method shown above but it doesn't seem to take. Perhaps I'm not following the || statement correctly?

     

    for the statement:

     

    Filter(
     colData,
     PulldownList.Selected.Result = "All" ||
     'Person'.DisplayName = 'PulldownList'.Selected.Result
    ) 

     

    I'm trying to add another criteria like this:

     

    Filter(
     colData,
     (PulldownList.Selected.Result = "All" ) || 
     ('Person'.DisplayName = 'PulldownList'.Selected.Result && 'Temperature'.Value="Green")
     )

     

     

    but the filter doesn't seem to work as expected. I'm trying to filter the temperature field in the same collection along with the Person.DisplayName. Is this still possible using this method?

     

    Thanks again!

  • WarrenBelz Profile Picture
    153,040 Most Valuable Professional on at

    @galos ,

    Just bracketing I think

    Filter(
     colData,
     (
     PulldownList.Selected.Result = "All" || 
     'Person'.DisplayName = 'PulldownList'.Selected.Result
     ) && 
     'Temperature'.Value = "Green"
    )

     

    Please click Accept as solution if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider giving it Thumbs Up.

    MVP (Business Applications)   Visit my blog Practical Power Apps

  • galos Profile Picture
    256 on at

    @WarrenBelzthanks for this response, unfortunately I tried this and it wouldn't work. What I noticed was the result only worked when the manager was in line with the employee. 

     

    So, if the manager pulldown value was ALL and an employee that wasn't under that manager was selected, nothing would display. Same the other way around - when the employee pulldown was ALL and the manager was selected, nothing displayed. But when the Manager was selected and the correct employee that reports under that manager was displayed, then it showed results that matched both.

     

    The unfortunate matter is that I need to display all items that involve the manager while ALL is selected for the employee. This doesn't seem to take. I think it's because the 'All' is conflicting with the other search field.

  • Verified answer
    WarrenBelz Profile Picture
    153,040 Most Valuable Professional on at

    @galos ,

    I am a bit perplexed as to why this code does not work for you assuming 'Person' is a Person/Group field and 'PulldownList' is a single-select combo box or drop-down with a Distinct Filter on display names. I am also assuming that your Temperature (choice) field has some items containing "Green". This part

    PulldownList.Selected.Result = "All" || 
    'Person'.DisplayName = 'PulldownList'.Selected.Result

    is saying that:

    • If "All" is selected in PulldownList, then do not filter (show all "green" records) OR 
    • Show all "green" records where 'Person' .DisplayName equals the item selected in 'PulldownList'

    Is all that correct ?

    You could also allow for nothing selected

    Filter(
     colData,
     (
     PulldownList.Selected.Result = "All" || 
     Len(PulldownList.Selected.Result) = 0 ||
     'Person'.DisplayName = 'PulldownList'.Selected.Result
     ) && 
     'Temperature'.Value = "Green"
    )

     

    Please click Accept as solution if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider giving it Thumbs Up.

    MVP (Business Applications)   Visit my blog Practical Power Apps

     

  • galos Profile Picture
    256 on at

    My sincere apologies @WarrenBelz . This is what happens to me when I am coding at 18 hour-days trying to get this completed. I mixed up two questions and gave you a headache. I am truly sorry.

     

    This screen that I'm working on, actually involves multiple levels of pulldowns using person fields, not temperature. (this is where I mixed it up with another screen I'm working on.  ( ...when I don't get enough sleep.... sorry)

     

    I'm using this same code on a similar search screen, but with 3 different pull-down menus. Each of the pull-downs is another level of hierarchy in the company. So the power app pulls data correctly when one of the drop-downs is "All", and it will show every record available from all three pull-downs, however that's where it finishes working. I have two other layers of people pull-down menus (based on level in the company). What I'd like to do is have an option for "All" for each of the 'Person' type field pull-down menus, but whenever I select one of the pull-downs and choose a specific person, only the records that match that person, in that level of hierarchy would be shown. I also have a search field on that page that I'm using as well, to search through the available records shown.

     

    So visually, here is what I'm looking at:

     

    Organization           VP              Director             Manager

    HR                           Joelle         Mike                  Tammy

    HR                           Joelle         Rebecca             John

    HR                           Joelle         Rebecca             Sam

    HR                           Joelle         Rebecca             Michelle

    Sales                       Chris           Jim                     John

    Sales                       Laura          Kim                    Joe

    Sales                       Laura          Kim                    Lily

     

     

    So as an example, when I select Joelle, I'd like to see all records associated with Mike and Rebecca as director.  Then if I select Mike, I'd only see Tammy's records. Keep in mind I'm not as interested in cascading the available pull-down values, if that makes this easier, but if it's doable, that would be a nice-to-have.

     

    I'm trying to see how to use that criteria in the base code I've built. Additionally, at the top of this form I have search box that will search only the records showing.

     

    thank you for sticking in there with me on this - I really do appreciate your help!

     

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

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 717 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 329 Super User 2025 Season 2

#3
Power Platform 1919 Profile Picture

Power Platform 1919 268

Last 30 days Overall leaderboard