30
Views
6
Comments
Solved
problem in IF condition

hello 

on this condition I have a strange thing: it treats the condition for A, B and WEA

but for C and WEB it does not take it 

I don't know why




If(DayOfWeek(PrepareLog.Log.DetectedOn) > 0 and DayOfWeek(PrepareLog.Log.DetectedOn) < 6    and (Hour(PrepareLog.Log.DetectedOn) >= 6 and Hour(PrepareLog.Log.DetectedOn) < 14), "A",     If(DayOfWeek(PrepareLog.Log.DetectedOn) > 0 and DayOfWeek(PrepareLog.Log.DetectedOn) < 6    and (Hour(PrepareLog.Log.DetectedOn) >= 14 and Hour(PrepareLog.Log.DetectedOn) < 22), "B",    If(DayOfWeek(PrepareLog.Log.DetectedOn) > 0 and DayOfWeek(PrepareLog.Log.DetectedOn) < 6    and (Hour(PrepareLog.Log.DetectedOn) >=22 and Hour(PrepareLog.Log.DetectedOn) < 6), "C",    If(DayOfWeek(PrepareLog.Log.DetectedOn) = 0 or DayOfWeek(PrepareLog.Log.DetectedOn) = 6    and (Hour(PrepareLog.Log.DetectedOn) >= 6 and Hour(PrepareLog.Log.DetectedOn) < 18), "WEA",    If(DayOfWeek(PrepareLog.Log.DetectedOn) = 0 or DayOfWeek(PrepareLog.Log.DetectedOn) = 6    and (Hour(PrepareLog.Log.DetectedOn) >= 18 and Hour(PrepareLog.Log.DetectedOn) < 6), "WEB",    "Nothing")))))


Thank you 

2020-09-15 13-07-23
Kilian Hekhuis
 
MVP
Solution

Hi Ibtissam,

Let's first simplify your condition by replacing the DayOfWeek and Hour by a simple variable:

If(Day > 0 and Day < 6 and (Hour >= 6 and Hour < 14), "A",
If(Day > 0 and Day < 6 and (Hour >= 14 and Hour < 22), "B",
If(Day > 0 and Day < 6 and (Hour >= 22 and Hour < 6), "C",
If(Day = 0 or Day = 6  and (Hour >= 6 and Hour < 18), "WEA",
If(Day = 0 or Day = 6  and (Hour >= 18 and Hour < 6), "WEB",
"Nothing")))))

The first thing I notice is that you repeat the "Day > 0 and Day < 6" three times, and the "Day = 0 or Day = 6" two times. My recommendation would be to simplify that, which makes it faster and much more readible:

if (Day > 0 and Day < 6,
	if (Hour >= 6 and Hour < 14, "A",
	if (Hour >= 14 and Hour < 22, "B",
	if (Hour >= 22 and Hour < 6, "C", "???"))),
// else
	if (Hour >= 6 and Hour < 18, "WEA",
	if (Hour >= 18 and Hour < 6), "WEB", "???")
)

As Dorine already told you, something can't be both be larger than 22 and smaller than 6 (or larger than 18). That's just a logical error in your code. I truely hope you can see that there's an error there. That said, the solution is easy, just do not specify that condition at all but use it as an else:

if (Day > 0 and Day < 6,
	if (Hour >= 6 and Hour < 14, "A",
	if (Hour >= 14 and Hour < 22, "B",
	"C"))),
// else
	if (Hour >= 6 and Hour < 18, "WEA",
	"WEB")
)

Note though that I'd consider this all very, very basic programming skills, so if you feel this is above your head, perhaps you should consider some more training material...

UserImage.jpg
Ibtissam MS
Hi @Kilian thank you very much so it's as simple as that even for me it's clear and better structured, can you recommend lessons to improve this side in my development or is it a matter of practice?
2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

I'd say most of it is logical thinking. And practice would help, definitely, as well as checking existing code. And sometimes taking a step back and try to think in functional terms rather than technical ones.

2021-09-06 15-09-53
Dorine Boudry
 
MVP

Hi Ibtissam, an integer can't be at the same time larger than 22 and smaller than 6

you probably want a few or instead of and in there

UserImage.jpg
Ibtissam MS
Hi Dorine, If you have a solution for this problem how can I do Thank you
2021-09-06 15-09-53
Dorine Boudry
 
MVP
euhm, well, i can see that hour > 22 and hour < 6 can never be true, so you could try replacing that and by an or. But I can't offer a solution, as I said in your previous post about this condition, it is way to complex for my simple brain to understand, so I would solve it in a more verbose way instead of all cramming it in a single expression.
2020-09-15 13-07-23
Kilian Hekhuis
 
MVP
Solution

Hi Ibtissam,

Let's first simplify your condition by replacing the DayOfWeek and Hour by a simple variable:

If(Day > 0 and Day < 6 and (Hour >= 6 and Hour < 14), "A",
If(Day > 0 and Day < 6 and (Hour >= 14 and Hour < 22), "B",
If(Day > 0 and Day < 6 and (Hour >= 22 and Hour < 6), "C",
If(Day = 0 or Day = 6  and (Hour >= 6 and Hour < 18), "WEA",
If(Day = 0 or Day = 6  and (Hour >= 18 and Hour < 6), "WEB",
"Nothing")))))

The first thing I notice is that you repeat the "Day > 0 and Day < 6" three times, and the "Day = 0 or Day = 6" two times. My recommendation would be to simplify that, which makes it faster and much more readible:

if (Day > 0 and Day < 6,
	if (Hour >= 6 and Hour < 14, "A",
	if (Hour >= 14 and Hour < 22, "B",
	if (Hour >= 22 and Hour < 6, "C", "???"))),
// else
	if (Hour >= 6 and Hour < 18, "WEA",
	if (Hour >= 18 and Hour < 6), "WEB", "???")
)

As Dorine already told you, something can't be both be larger than 22 and smaller than 6 (or larger than 18). That's just a logical error in your code. I truely hope you can see that there's an error there. That said, the solution is easy, just do not specify that condition at all but use it as an else:

if (Day > 0 and Day < 6,
	if (Hour >= 6 and Hour < 14, "A",
	if (Hour >= 14 and Hour < 22, "B",
	"C"))),
// else
	if (Hour >= 6 and Hour < 18, "WEA",
	"WEB")
)

Note though that I'd consider this all very, very basic programming skills, so if you feel this is above your head, perhaps you should consider some more training material...

UserImage.jpg
Ibtissam MS
Hi @Kilian thank you very much so it's as simple as that even for me it's clear and better structured, can you recommend lessons to improve this side in my development or is it a matter of practice?
2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

I'd say most of it is logical thinking. And practice would help, definitely, as well as checking existing code. And sometimes taking a step back and try to think in functional terms rather than technical ones.

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.