Cognos-11 Date Prompts - How do I add in an filter for 'previous 8 weeks'? - cognos

I am very new to the coding side of Cognos!
I am trying to set up an auto-report that will run every Monday but calculate the previous 8 weeks worth of data. I have added the prompt button to show 'Last 8 weeks', but now I am not sure how to add it as an actual filter. These are my current date filters:
case
when ?p_DateCat? = 'EnteredDates'
then case
when ?p_StartDate? <> current_date
then ?p_StartDate?
else _add_days (current_date, -1)
end
when ?p_DateCat? = 'Yesterday'
then _add_days (current_date, -1)
when ?p_DateCat? = 'ThisWeek'
then _add_days (current_date, 1-(_day_of_week (current_date, 1)))
when ?p_DateCat? = 'ThisMonth'
then _add_days (current_date, 1-(_day (current_date)))
when ?p_DateCat? = 'LastMonth'
then _add_months (_first_of_month (current_date), -1)
end
and
case
when ?p_DateCat? = 'EnteredDates'
then case
when ?p_EndDate? <> current_date
then ?p_EndDate?
else _add_days (current_date, -1)
end
when ?p_DateCat? = 'Yesterday'
then _add_days (current_date, -1)
when ?p_DateCat? = 'ThisWeek'
then _add_days (current_date, -1)
when ?p_DateCat? = 'ThisMonth'
then _add_days (current_date, -1)
when ?p_DateCat? = 'LastMonth'
then _last_of_month(_add_months (current_date, -1))
end```

Found the answer:
[DateField] >= _add_days (current_date, -56)
or
[DateField] between _add_days (current_date, -56) and _add_days (current_date, -1)
Syntax error was because of the other lines. Once removed it ran perfectly for the requested 56 day period!

Related

cuDF - UDF failed in cuda pipeline

I want based on DateTime select a row, if it is selected, then is processed Time, if it is good outcol turns into 1
import cudf
import numpy as np
df = cudf.DataFrame({ 'DateTime' : ['20080501 00:00:00.000','20080502 00:01:00.000', '20080503 00:02:00.000', '20080504 00:03:00.000', '20080505 00:04:00.000','20080506 00:05:00.000','20080507 00:06:00.000','20080508 00:07:00.000','20080509 00:08:00.000','20080510 00:09:00.000'],
'Time' : [0,1,2,3,4,5,6,7,8,9]})
df['DateTime'] = cudf.to_datetime(df['DateTime'], errors='coerce')
date1 = cudf.to_datetime("2008-05-01")
date2 = cudf.to_datetime("2008-05-03")
date3 = cudf.to_datetime("2008-05-06")
date4 = cudf.to_datetime("2008-05-08")
date1 = cudf.Series(date1)
date2 = cudf.Series(date2)
date3 = cudf.Series(date3)
date4 = cudf.Series(date4)
so i created a function
def timex(incol1, incol2, outcol, date1,date2,date3,date4,time1,time2,time3,time4):
for i, (a,b) in enumerate(zip(incol1,incol2)):
if date1 <= a < date2:
if time1 <= b <= time2:
outcol[i] = 1
elif date3 <= a < date4:
if time3 <= b <= time4:
outcol[i] = 1
else:
outcol[i] = 0
df.apply_rows(timex,
incols={'DateTime':'incol1', 'Time':'incol2'},
outcols=dict(outcol=np.int64),
kwargs=dict(date1 = date1,date2 = date2,date3=date3,date4=date4,time1 = 0,time2 = 2, time3 = 6, time4 = 8))
but it returns me
TypingError: Failed in cuda mode pipeline (step: nopython frontend)
can someone tell me how to do it? or where is the mistake

I am using Pandas for a sentiment analysis problem, I want to change my categories to ['Agree', 'Disagree', 'Neutral'] to a scale from 1 to 5

recsys_file = pd.read_csv('full_chat_questions.csv')
for elem in recsys_file["item3G_new"]:
if recsys_file["elem"] == 'Strongly disagree':
recsys_file['elem'].replace(to_replace = elem, value = 1)
if recsys_file["elem"] == 'Disagree':
recsys_file['elem'].replace(to_replace = elem, value = 2)
if recsys_file["elem"] == 'Neutral':
recsys_file['elem'].replace(to_replace = elem, value = 3)
if recsys_file["elem"] == 'Agree':
recsys_file['elem'].replace(to_replace = elem, value = 4)
if recsys_file["elem"] == 'Strongly agree':
recsys_file['elem'].replace(to_replace = elem, value = 5)
trying to change the categories to values from 1 to 5 but I am getting errors
Try this, it uses np.select() to make your code more concise and gives a default value if there is nothing matching (which might be part of the problem)
import pandas as pd
import numpy as np
recsys_file = pd.read_csv('full_chat_questions.csv')
condition_list = [recsys_file["elem"] == 'Strongly disagree', recsys_file["elem"] == 'Disagree', recsys_file["elem"] == 'Neutral', recsys_file["elem"] == 'Agree', recsys_file["elem"] == 'Strongly agree']
choice_list = [1, 2, 3, 4, 5]
recsys_file["elem"] = np.select(condition_list, choice_list, 0)

Expression.Error: We cannot convert the value #date(2022, 3, 14) to type Logical

I converted an excel formula over to power query to try to speed it up and filter before it enters the sheet, can I get some help on why im getting this error:
Expression.Error: We cannot convert the value #date(2022, 3, 14) to type Logical.
Details:
Value=3/14/2022
Type=[Type]
Here is the code line:
= Table.AddColumn(
#"Changed Type",
"Custom",
each
if (
if [PO_TYPE] = "Intransit ASN" then
if [RECEIPT_DATE] = "" then
Date.From(DateTime.LocalNow())
else if [INV_ORG] = "GSN" then
if Date.Day([RECEIPT_DATE]) > 15 then
Date.AddDays([RECEIPT_DATE], 25)
else
[RECEIPT_DATE]
else if Date.Day([RECEIPT_DATE]) > 15 then
Date.AddDays([RECEIPT_DATE], 25)
else
[RECEIPT_DATE]
else if [RECEIPT_DATE] = "" then
Date.From(DateTime.LocalNow())
else if [INV_ORG] = "GSN" then
if Date.Day([NEED_BY_DATE]) > 15 then
Date.AddDays([NEED_BY_DATE], 25)
else
[NEED_BY_DATE]
else if Date.Day([NEED_BY_DATE]) > 15 then
Date.AddDays([NEED_BY_DATE], 25)
else
[NEED_BY_DATE] < Date.From(DateTime.LocalNow())
)
then
Date.From(DateTime.LocalNow())
else if [PO_TYPE] = "Intransit ASN" then
if [RECEIPT_DATE] = "" then
Date.From(DateTime.LocalNow())
else if [INV_ORG] = "GSN" then
if Date.Day([RECEIPT_DATE]) > 15 then
Date.AddDays([RECEIPT_DATE], 25)
else
[RECEIPT_DATE]
else if Date.Day([RECEIPT_DATE]) > 15 then
Date.AddDays([RECEIPT_DATE], 25)
else
[RECEIPT_DATE]
else if [RECEIPT_DATE] = "" then
Date.From(DateTime.LocalNow())
else if [INV_ORG] = "GSN" then
if Date.Day([NEED_BY_DATE]) > 15 then
[NEED_BY_DATE] + 25
else
[NEED_BY_DATE]
else if Date.Day([RECEIPT_DATE]) > 15 then
Date.AddDays([RECEIPT_DATE], 25)
else
[RECEIPT_DATE]
)
Just eyeballing it, it has to do with the first if above the red box
The code in the red box returns some form of date, shown by the blue boxes
So then your if statement gets evaluated as:
= Table.AddColumn(#"Changed Type", "Custom", each if( somedate else
and that is trying to use a date as a logical
As an aside, the code is not pretty. Do you want to explain what it is meant to do for some help on that?
The main reason for your issue is that this parenthesis needs to be moved
[...]
else if Date.Day([NEED_BY_DATE]) > 15 then
Date.AddDays([NEED_BY_DATE], 25)
else
[NEED_BY_DATE] < Date.From(DateTime.LocalNow())
>>> ) <<<
then
Date.From(DateTime.LocalNow())
else if [PO_TYPE] = "Intransit ASN" then
if [RECEIPT_DATE] = "" then
[...]
to before today's date:
[...]
else if Date.Day([NEED_BY_DATE]) > 15 then
Date.AddDays([NEED_BY_DATE], 25)
else
[NEED_BY_DATE] ) < Date.From(DateTime.LocalNow())
then
Date.From(DateTime.LocalNow())
else if [PO_TYPE] = "Intransit ASN" then
if [RECEIPT_DATE] = "" then
[...]
Apart from that, I would strongly recommend using variables to make your code more readable and more efficient. For example, your basic logic can be expressed more like this:
= Table.AddColumn(
#"Changed Type",
"Custom",
each
let
rdate =
if Date.Day([RECEIPT_DATE]) > 15
then Date.AddDays([RECEIPT_DATE], 25)
else [RECEIPT_DATE],
today = Date.From(DateTime.LocalNow()),
tdate = if [RECEIPT_DATE] = "" then today else rdate
in
if tdate < today then today else tdate
)

How to get last wednesday for current month in python?

my code as follows
today = todayte
print('today1 =', today)
offset = (today.weekday() - 2) % 7
print('offset1=', offset)
last_wednesday = today - timedelta(days=offset)
print('last_wednesday1 = ', last_wednesday)
my current output as follows
today1 = 2018-03-05
offset1 = 5
last_wednesday1 = 2018-02-28
in the above case i am getting previous month last wednesday
but i need current month last wednesday.
my expected output is as follows
last_wednesday = 2018-03-28
Here is a way:
from datetime import datetime , timedelta
todayDT = datetime.today()
currentMonth = todayDT.month
nWed = todayDT
while todayDT.month == currentMonth:
todayDT += timedelta(days=1)
if todayDT.weekday()==2: #this is Wednesday
nWed = todayDT
print (nWed)
you can use a combination of datetime and calendar modules:
from datetime import datetime, timedelta
import calendar
today = datetime.now()
# find first day of the month and then, find first wednesday of the month and replace it
# weekday of wednesday == 2
first_day = datetime.today().replace(day=1)
while first_day.weekday() != 2:
first_day += timedelta(days=1)
number_of_days_in_month = calendar.monthrange(today.year, today.month)[1]
last_wend = first_day + timedelta(days=(((number_of_days_in_month - first_day.day) // 7) * 7))
print(last_wend)
or as #Mark Ransom suggested:
from datetime import datetime, timedelta
day_ = (datetime.now().replace(day=1) + timedelta(days=32)).replace(day=1)
while True:
day_ -= timedelta(days=1)
if day_.weekday() == 2:
break
print(day_)
How about this, we first jump to the next month and re-use your existing code:
import datetime as dt
todayte = dt.date(2018, 3, 5)
today = todayte
d = dt.date(today.year, today.month, 28) # the 28th day of a month must exist
d = d + dt.timedelta(days=7) # so we are sure d is in the next month
# then we apply your original logic
offset = (d.weekday() - 2) % 7
last_wednesday = d - dt.timedelta(days=offset)
print(last_wednesday)
Result:
2018-04-04

Iterate rows in df and insert new row with result of column operations of the matching rows

I wanted to create a new row with difference in values of a particular column of two other rows with matching columns and another variable column
Project Cost element Version Value Cur
A A1 000 -10000 USD
A A1 ASO -8000 USD
A A1 CHO -2000 USD
A A1 CHr -2000 USD create third & fourth rows
Similarly for costements starting with 5 say B1. the delta row should have version ADJ.
After browsing through old posts got some looping logic.. but feel it is too cumbersome. Any smart ideas welcome.
for i in range(0,len(Project_Forecast_Source)):
if Project_Forecast_Source['CostElem'].loc[i] =='4020101000' :
if (Project_Forecast_Source['ProjDef'].loc[i] == Project_Forecast_Source['ProjDef'].loc[i+1] and
Project_Forecast_Source['CostElem'].loc[i] == Project_Forecast_Source['CostElem'].loc[i+1] and
Project_Forecast_Source['Version'].loc[i] == "000") :
Val = float(Project_Forecast_Source['Amount'].loc[i] - Project_Forecast_Source['Amount'].loc[i+1])
row_number = Project_Forecast_Source.index.max()+1
row_Value = [Project_Forecast_Source['ProjDef'].loc[i], Project_Forecast_Source['CostElem'].loc[i+1],"CHO", Project_Forecast_Source['ObCur'].loc[i], Val]
Project_Forecast_Source.loc[row_number] = row_Value
row_number1 = Project_Forecast_Source.index.max()+1
row_Value1 = [Project_Forecast_Source['ProjDef'].loc[i], Project_Forecast_Source['CostElem'].loc[i+1],"CHR", Project_Forecast_Source['ObCur'].loc[i], Val]
Project_Forecast_Source.loc[row_number1] = row_Value1
else :
if (Project_Forecast_Source['ProjDef'].loc[i] == Project_Forecast_Source['ProjDef'].loc[i+1] and
Project_Forecast_Source['CostElem'].loc[i] == Project_Forecast_Source['CostElem'].loc[i+1] and
Project_Forecast_Source['Version'].loc[i] == "000") :
Val = float(Project_Forecast_Source['Amount'].loc[i] - Project_Forecast_Source['Amount'].loc[i+1])
row_number = Project_Forecast_Source.index.max()+1
row_Value = [Project_Forecast_Source['ProjDef'].loc[i], Project_Forecast_Source['CostElem'].loc[i],"ADJ", Project_Forecast_Source['ObCur'].loc[i], Val]
Project_Forecast_Source.loc[row_number] = row_Value
elif ((Project_Forecast_Source['Version'].loc[i] == "ASO") and
# (Project_Forecast_Source['ProjDef'].loc[i] != Project_Forecast_Source['ProjDef'].loc[i+1]) and
# (Project_Forecast_Source['ProjDef'].loc[i] != Project_Forecast_Source['ProjDef'].loc[i-1]) and
(Project_Forecast_Source['CostElem'].loc[i] != Project_Forecast_Source['CostElem'].loc[i+1]) and
(Project_Forecast_Source['CostElem'].loc[i] != Project_Forecast_Source['CostElem'].loc[i-1])) :
Val = 0.0 - float(Project_Forecast_Source['Amount'].loc[i])
row_number = Project_Forecast_Source.index.max()+1
row_Value = [Project_Forecast_Source['ProjDef'].loc[i], Project_Forecast_Source['CostElem'].loc[i],"ADJ", Project_Forecast_Source['ObCur'].loc[i], Val]
Project_Forecast_Source.loc[row_number] = row_Value
elif Project_Forecast_Source['Version'].loc[i] == "000" :
Val = float(Project_Forecast_Source['Amount'].loc[i])
row_number = Project_Forecast_Source.index.max()+1
row_Value = [Project_Forecast_Source['ProjDef'].loc[i], Project_Forecast_Source['CostElem'].loc[i],"ADJ", Project_Forecast_Source['ObCur'].loc[i], Val]
Project_Forecast_Source.loc[row_number] = row_Value

Resources