Today function is not working in Column Expression Node

I want create custom column which calcualte quarter based on current date and logic is
If the current month is not January, then the code checks the current month and assigns a corresponding quarter value based on the month. For example, if the month is April, February, or March, it assigns the value “1” for the first quarter. If the month is July, May, or June, it assigns the value “2” for the second quarter, and so on. If the month is October, August, or September, it assigns the value “3” for the third quarter. If month is November or December, it assigns value “4”.

If the current month is January, it means it’s a new year, so the code adds the value “4” to the previous year’s value.

In simpler terms, the code determines the quarter of the year based on the current date. If it’s not January, it assigns a quarter value based on the month. If it’s January, it assigns the fourth quarter of the previous year.

and I used for the logic is
import datetime

today = datetime.date.today()
month = today.month
year = today.year

if month != 1:
if month in [2, 3, 4]:
quarter = 1
elif month in [5, 6, 7]:
quarter = 2
elif month in [8, 9, 10]:
quarter = 3
elif month in [11, 12]:
quarter = 4
else:
year -= 1
quarter = 4

quarter_year = str(year)+str(quarter)
print(quarter_year)

but I am facing error and this error is

To me this sounds like today is a reserved keyword in JavaScript. Did you try using a different variable name?

1 Like

Just use this for Column Expression

today()

Hi @Vinay1995 , welcome to the KNIME community.

As @thor noted, the specify error is because today is not available as a variable name. However there are further issues as your code is currently somewhere between Python and JavaScript so is not going to work with KNIME…

My other question is over the specifics of the code… The quarters that you specify are not “common”
i.e. where Q1 is Feb, Mar, Apr, and Q4 consists of Nov, Dec, Jan
but I presume this is specific to a particular company.

Anyway, try this:

tday =  today()
month = getMonthOfYear(tday) // this returns 1=Jan, 2=Feb and so on
year =  getYear(tday)
quarter = 0 // initialise

switch (month) {
case 1:
    year -= 1
    quarter = 4
    break;
case 2:
case 3:
case 4:
    quarter = 1;
    break;
case 5:
case 6:
case 7:
    quarter = 2;
    break;
case 8:
case 9:
case 10:
    quarter = 3;
    break;
case 11:
case 12:
    quarter = 4;
    break;
}

string(year) + string(quarter)

4 Likes

Thank You so much. This is very helpful

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.