cancel
Showing results for 
Search instead for 
Did you mean: 

If Then statement issues

kevincharlton
Explorer
0 Kudos

I have a field that is date time field, using that field I need to determine what shift a person worked.

I was thinking about using an IF statement to determine based on the time, what shift a person worked.

I used this formula to convert the field to Time

TimeValue ({lwmain.dtrepor})

Then I tried to write an IF statement to determine the shift. This is a separate formula from the above. Or maybe it should all be one statement.

IF {lwmain.dtrepor} between 06:00 to 13:59 then "I" or

IF {lwmain.dtrepor} between 14:00 to 21:59 then "II" or

IF {lwmain.dtrepor} between 22:00 to 23:59 then "III" or

IF {lwmain.dtrepor} between 00:01 to 05:59 then "III"

But it errors out and I am unsure how to make it use the time only and then do the If then statement.

Any help is appreciated.

jerryjanda
Community Manager
Community Manager
0 Kudos

Thank you for visiting SAP Community to get answers to your questions. Since you're asking a question here for the first time, I recommend that you familiarize yourself with https://community.sap.com/resources/questions-and-answers (if you haven't already), as it provides tips for preparing questions that draw responses from our members.

Should you wish, you can revise your question by selecting Actions, then Edit (although once someone answers your question, you'll lose the ability to edit the question -- but if that happens, you can leave more details in a comment).

Finally, if you're hoping to connect with readers, please consider adding a picture to your profile. Here's how you do it: https://www.youtube.com/watch?v=F5JdUbyjfMA&list=PLpQebylHrdh5s3gwy-h6RtymfDpoz3vDS. By personalizing your profile with a photo of you, you encourage readers to respond.

Kind regards,

--Jerry

Accepted Solutions (1)

Accepted Solutions (1)

vitaly_izmaylov
Employee
Employee
0 Kudos

Try:

IF Time ({lwmain.dtrepor}) = CTime("06:00") to CTime("13:59") then "I" else...

vitaly_izmaylov
Employee
Employee
0 Kudos

corrected the formula

kevincharlton
Explorer
0 Kudos

Thank you so much, it worked.

kevincharlton
Explorer
0 Kudos

Thank you for your response. Here is what I attempted:

IF Time ({lwmain.dtrepor}) between CTime("06:00") to CTime("13:59") then "I" or IF Time ({lwmain.dtrepor}) between CTime("14:00") to CTime("21:59") then "II" or IF Time ({lwmain.dtrepor}) between CTime("22:00") to CTime("05:59") then "III" Else {lwmain.dtrepor}

I get an error that highlights the first between and says it expects a then

Ideas

kevincharlton
Explorer
0 Kudos

I changed the Between to = and it worked on the first line then it errors out, thoughts?

Answers (1)

Answers (1)

DellSC
Active Contributor
0 Kudos

You can't use "or" between If..then statements, Instead, you have to use "else", like this:

IF {lwmain.dtrepor} = CTime("06:00") to CTime("13:59")  then "I"
Else IF {lwmain.dtrepor} = CTime("14:00") to CTime("21:59") then "II"
Else IF {lwmain.dtrepor} = CTime(22:00") to CTime("23:59") then "III"
Else IF {lwmain.dtrepor} = CTime(00:01") to CTime("05:59") then "III"

Or you could use a Switch statement like this:

Switch
(
  {lwmain.dtrepor} = CTime("06:00") to CTime("13:59"), "I",
  {lwmain.dtrepor} = CTime("14:00") to CTime("21:59"), "II",
  {lwmain.dtrepor} = CTime(22:00") to CTime("23:59") "III",
  {lwmain.dtrepor} = CTime(00:01") to CTime("05:59") "III"
)

Either one of these should work for you.

-Dell