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 / Need some help with 's...
Power Apps
Answered

Need some help with 'switch' and 'if' statements

(0) ShareShare
ReportReport
Posted on by 503

Hi all 

 

This is following on from my previous post and thought I'd start a new one to see if someone knows about the issue i'm experiencing as I need to resolve it by Monday. Currently I have a Switch and I'm trying to also make it recognize the second IF statement in the formula. Currently it only recognizes the first IF statement for both users I'm trying to setup. 

So if the first user has value 'DefualtStartTime7am' next to their name then I want to execute the first IF statement, but if the user has value 'DefualtStartTime8am_1' next to their name then I want to execute the second IF statement. Right now it's only detecting the first IF statement for both users in this case. 

 

I have researched what the Switch function does so I thought the below would work. It seems like it can only detect what wUserStart has next to it in the first part of the formula. 

With(
 {
 wUserStart: "DefaultStartTime7am"
 },
 Switch(
 wUserStart,
 "DefaultStartTime7am",
 If(
 Now() > DateTimeValue("7:00 AM"),
 Now(),
 DateTimeValue("7:00 AM")
 ),
 "DefaultStartTime8am_1",
 If(
 Now() > DateTimeValue("8:00 AM"),
 Now(),
 DateTimeValue("8:00 AM") 
 )
 
 )
), 

 

I have also tried - 

With(
 {
 wUserStart: "DefaultStartTime7am"
 wSecondUser: "DefaultStartTime8am_1"
 },
 Switch(
 wUserStart,
 "DefaultStartTime7am",
 If(
 Now() > DateTimeValue("7:00 AM"),
 Now(),
 DateTimeValue("7:00 AM")
 ),
 wSecondUser, 
 "DefaultStartTime8am_1",
 If(
 Now() > DateTimeValue("8:00 AM"),
 Now(),
 DateTimeValue("8:00 AM") 
 )
 
 )
), 

But no luck with that one either. 

 

I have a feeling it has something to do with the syntax that I cant figure out. 

 

Thanks for your generous help everyone. 

 

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

    Hi @Usernametwice23 ,'

    You have a fundamental error in your statement - you are defining a value in the With() statement and then testing for it, where it will always be true (so it will never progress past the first test). Where are you getting the value you want to test from?

  • Verified answer
    Digital Profile Picture
    1,207 on at

     

    @Usernametwice23 ,

     

    Hi, I commented on your original post. The fist switch case is executing because it is matching the literal text value in wUserStart. I should've made it clear in my reply that this needs to be dynamic. 

     

    I also recommended a switch because I assumed multiple default start times.

     

    How many default start times are there?

    What datatype are the start time values fetched from Dataverse? 

    Can you give an example of a value for DefaultStartTime7am or DefaultStartTime8am_1

     

    With(
     {
     wUserStart: [DEFAULT START FOR CURRENT USER]
     },
     Switch(
     wUserStart,
     "DefaultStartTime7am",
     If(
     Now() > DateTimeValue("7:00 AM"),
     Now(),
     DateTimeValue("7:00 AM")
     ),
     "DefaultStartTime8am_1",
     If(
     Now() > DateTimeValue("8:00 AM"),
     Now(),
     DateTimeValue("8:00 AM") 
     )
     
     )
    )

     

  • Usernametwice Profile Picture
    503 on at

    Hi @Digital @WarrenBelz 

    Thanks for your help. Okay I now understand that this was meant for multiple start times which is fine because I think in the future this is how I will want to set it up anyway and I may want to add more. 

     

    Lets say there's 3 start times. I have 3 choice fields/columns (7am start time, 8am start time and 9am start time) in dataverse against each user and if one field is selected as 'yes' in dataverse, that means a label will be populated with 'yes' in the powerapp (either DefaultStartTime7am or DefaultStartTime8am or DefaultStartTime9am) against that user that wants to clock-in.  

    So when 2 labels are blank then it should execute the 'if' statement that doesnt have the blank label.

     

    From my understanding, a switch function will go through each branch and evaluate which IF statement to use. I have tried the below but I think it's still only recognizing the first branch. So in this case if DefaultStartTime7am label is blank then move onto the next if statement and check that one instead. But I know i've written this out incorrectly but may give you some idea on what im trying to do. 

     

    Again thanks for your help. Im new to Switch and With functions and it makes it more complicated when times/dates are being used to learn this. 

    Switch(
     If(IsBlank(DefaultStartTime7am)
     || (Now() > DateTimeValue("7:00 AM")),
     Now(),
     DateTimeValue("7:00 AM")
     ),
     
     If(IsBlank(DefaultStartTime8am)
     || (Now() > DateTimeValue("8:00 AM")),
     Now(),
     DateTimeValue("8:00 AM") 
     ),
     
     If(IsBlank(DefaultStartTime9am)
     || (Now() > DateTimeValue("9:00 AM")),
     Now(),
     DateTimeValue("9:00 AM") 
     ) 
    ), 

     

     

     

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

    @Usernametwice23 ,

    I will leave you with @Digital here (I don't do three way conversations as they confuse everyone), just one comment - your Switch has to use the same field/variable and branch on different values.

  • Verified answer
    Usernametwice Profile Picture
    503 on at

    Hi @WarrenBelz @Digital 

     

    Thanks again for helping me. I figured it out after a while. Here was the formula that I needed in the end - 

    With(
     {
     wUserStart: DefaultTime.Text
     },
     Switch(
     wUserStart,
     StartTime7am.Text,
     If(
     Now() > DateTimeValue("7:00 AM"),
     Now(),
     DateTimeValue("7:00 AM")
     ),
     StartTime8am.Text,
     If(
     Now() > DateTimeValue("8:00 AM"),
     Now(),
     DateTimeValue("8:00 AM") 
     ),
     StartTime9am.Text,
     If(
     Now() > DateTimeValue("9:00 AM"),
     Now(),
     DateTimeValue("9:00 AM") 
     ),
     If(IsBlank(DefaultTime.Text),
     Now() 
     )
     
     )),

     

    I needed to change the column to a single column in dataverse (Date/Time) and retrieve the start time for each user instead of having 3 different columns. 

  • Digital Profile Picture
    1,207 on at

    Glad you got it sorted. Yes much better to have 1 column with the default start time set for each user rather than multiple yes/no columns.

     

    You could even get rid of the With and reference the DefaultTime.Text in your Switch. 

     

    With is good for separating a bunch of data from the code. Almost like a record of local variables that only work inside that property. But as you've only got the one you don't really need it. 

     Switch(
     DefaultTime.Text,
     StartTime7am.Text,
     If(
     Now() > DateTimeValue("7:00 AM"),
     Now(),
     DateTimeValue("7:00 AM")
     ),
     StartTime8am.Text,
     If(
     Now() > DateTimeValue("8:00 AM"),
     Now(),
     DateTimeValue("8:00 AM") 
     ),
     StartTime9am.Text,
     If(
     Now() > DateTimeValue("9:00 AM"),
     Now(),
     DateTimeValue("9:00 AM") 
     ),
     If(IsBlank(DefaultTime.Text),
     Now() 
     )
     
     )

     

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 739 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 343 Super User 2025 Season 2

#3
Power Platform 1919 Profile Picture

Power Platform 1919 268

Last 30 days Overall leaderboard