Assigning Quarter to a DateTime

0
Does the following look like it should correctly assign quarter to a datetime field? I’m finding quarter incorrectly assigned, e.g., where month is correctly set to 7, quarter is 3?   if  parseInteger(formatDateTimeUTC($Iterator_MTDHist/mindt,'MM')) <= 3 then 1 else if parseInteger(formatDateTimeUTC($Iterator_MTDHist/mindt,'MM')) <= 6  then 2  else if  parseInteger(formatDateTimeUTC($Iterator_MTDHist/mindt,'MM')) <= 9  then 3  else if parseInteger(formatDateTimeUTC($Iterator_MTDHist/mindt,'MM')) >= 10  then 4  else 0
asked
5 answers
3

All those ifs can go out the window. Always try to use as little cylces as possible and stay in Mendix and out of Java if you can. This task can get solved with one statement:

floor(trimToMonths($Iterator_MTDHist) div 3)+1

And always, if you not 100% certain of the correctness: build a unittest for it.

answered
2

Hey SL, 

The way you compare the month from date is perfectly good. But, you have given only the top range for quater and didn’t mention anything for the start range. so, the condition here should be 

 

if 
parseInteger(formatDateTimeUTC($Iterator_MTDHist/mindt,'MM')) <= 3 then 1


else if 

parseInteger(formatDateTimeUTC($Iterator_MTDHist/mindt,'MM')) >3  and 
parseInteger(formatDateTimeUTC($Iterator_MTDHist/mindt,'MM')) <= 6  then 2 
else if 
parseInteger(formatDateTimeUTC($Iterator_MTDHist/mindt,'MM')) >6  and 
parseInteger(formatDateTimeUTC($Iterator_MTDHist/mindt,'MM')) <= 9  then 3 
else if
parseInteger(formatDateTimeUTC($Iterator_MTDHist/mindt,'MM')) >= 10  then 4 
else 0

Note:  If you are calculating using UTC time. Make suer you have set the attribute’s localize property to NO . 

In accurate way of doing this replicate the same condition check inside a Java Action where you can get the month value with this code.

answered
0

or has anyone found a more reliable way to assign month and quarter to a datetime field?

answered
0

Sorry no dice on the suggestion – I added the lower bounds but am still ending up weird values, e.g., month 7 mapping to quarter 2. I actually see evidence that the same date is being assigned different quarters. Any idea on this? I’m not familiar how to incorporate a JAVA action, so not sure how to go about that.

answered
0

is the above expression taking a date field? My date field is$Iterator_MTDHist/mindt.  When I plug it in as follows, I get the error further down?

floor(trimToMonths($Iterator_MTDHist/mindt div 3)) +1

INvalid argument types Date/time and Integer/Long. Operator div expects numeric

I had unit tested this, but the issue with the incorrect quarter is not happening in my test environments> only prod

answered