cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Usernametwice23
Helper V
Helper V

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

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. 

 

2 ACCEPTED SOLUTIONS

Accepted Solutions
Digital
Super User
Super User

 

@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") 
        )
        
    )
)

 

View solution in original post

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. 

View solution in original post

6 REPLIES 6
WarrenBelz
Super User
Super User

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?

Digital
Super User
Super User

 

@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") 
        )
        
    )
)

 

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") 
        )  
),   

 

 

 

@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.

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. 

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() 
        )
        
    )

 

Helpful resources

Announcements
Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

May UG Leader Call Carousel 768x460.png

June User Group Leader Call

Join us on June 28 for our monthly User Group leader call!

PA Virtual Workshop Carousel 768x460.png

Register for a Free Workshop

This training provides practical hands-on experience in creating Power Apps solutions in a full-day of instructor-led App creation workshop.

PA.JPG

New Release Planning Portal (Preview)

Check out our new release planning portal, an interactive way to plan and prepare for upcoming features in Power Platform.

Top Solution Authors
Users online (1,368)