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

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
)

Related

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

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!

Tkinter convert entry.get() to integer problem

I am coding a program with Tkinter to calculate wind values. It takes Rwy Heading, Wind Direction and Wind Speed to calculate the wind whether headwind, tailwind or crosswind with the calculated relevant wind speed.
I get this error:
Traceback (most recent call last):
File "C:\Users\altug\Desktop\PROJECTS\ÇALIŞMA.py", line 40, in <module>
rwy_var= int(rwy)
ValueError: invalid literal for int() with base 10: ''
I both tried IntVar() and int(str) method to convert string into integer but it didn't sort out the problem.
Below you can find my code:
ent1 = Entry(root)
ent1.grid(row = 0, column = 1)
ent2 = Entry(root)
ent2.grid(row = 1, column = 1)
ent3 = Entry(root)
ent3.grid(row = 2, column = 1)
rwy = ent1.get()
wind_direction = ent2.get()
wind_speed = ent3.get()
rwy_var= IntVar()
wind_direction_var= IntVar()
wind_speed_var = IntVar()
rwy_var= int(rwy)
wind_direction_var= int(wind_direction)
wind_speed_var = int(wind_speed)
x = rwy_var - wind_direction_var
radx = math.radians(x)
sinx = math.sin(radx)
cosx = math.cos(radx)
def htwind():
txt.delete(0.0, "end")
b = rwy_var + 90
a = rwy_var - 90
if b > 360:
b -= 360
elif a <0:
a+= 360
if x == abs(90):
result = 0
txt.insert(0.0, result +'\n')
elif a <= wind_direction_var or wind_direction_var<= b :
sh = wind_speed_var * cosx
result = "Headwind"+ " " + round(abs(sh))+ " " + "kt"
txt.insert(0.0, result +'\n')
elif a >= wind_direction_var or wind_direction_var >= b :
st = wind_speed_var * cosx
result = "Tailwind"+ " " + round(abs(st))+ " " + "kt"
txt.insert(0.0, result +'\n')
return ""
def xwind():
txt.delete(0.0, "end")
c=rwy_var-180
d= rwy_var + 180
if d > 360:
d -= 360
elif c<0:
c+= 360
if x == 0 or x == abs(180) or y == 0 or y == abs(180):
print("0")
elif c< wind_direction_var:
sxwl = wind_speed_var * sinx
rslt = "L"+""+round(abs(sxwl))+""+"kt"
txt.insert(0.0, rslt +'\n')
elif wind_direction_var<d:
sxwr = wind_speed_var * sinx
rslt = "R"+""+round(abs(sxwr))+""+"kt"
txt.insert(0.0, rslt +'\n')
return ""
txt = Text(root, width=6, height=2, wrap =WORD)
txt.grid(row =1, column = 1)
button_1 =Button(root, text = "search", command=lambda:[htwind(),xwind()])
button_1.grid(row =0, column = 5)
What is my mistake ?
Thanks in advance.
The IntVar is special in that it can't be directly converted to an int. To get its int value, do
rwy_var = rwy.get()

python3, difflib SequenceMatcher

the following takes in two strings, compares differences and return them both as identicals as well as their differences, separated by spaces (maintaining the length of the longest sting.
The commented area in the code, are the 4 strings that should be returned.
from difflib import SequenceMatcher
t1 = 'betty: backstreetvboysareback"give.jpg"LAlarrygarryhannyhref="ang"_self'
t2 = 'bettyv: backstreetvboysareback"lifeislike"LAlarrygarryhannyhref="in.php"_self'
#t1 = 'betty : backstreetvboysareback" i e "LAlarrygarryhannyhref=" n "_self'
#t2 = 'betty : backstreetvboysareback" i e "LAlarrygarryhannyhref=" n "_self'
#o1 = ' g v .jpg g '
#o2 = ' v l f islike i .php '
matcher = SequenceMatcher(None, t1, t2)
blocks = matcher.get_matching_blocks()
bla1 = []
bla2 = []
for i in range(len(blocks)):
if i != len(blocks)-1:
bla1.append([t1[blocks[i].a + blocks[i].size:blocks[i+1].a], blocks[i].a + blocks[i].size, blocks[i+1].a])
bla2.append([t2[blocks[i].b + blocks[i].size:blocks[i+1].b], blocks[i].b + blocks[i].size, blocks[i+1].b])
cnt = 0
for i in range(len(bla1)):
if bla1[i][1] < bla2[i][1]:
num = bla2[i][1] - bla1[i][1]
t2 = t2[0:bla2[i][1]] + ' '*num + t2[bla2[i][1]:len(t2)]
bla2[i][0] = ' '*num + bla2[i][0]
bla2[i][1] = bla1[i][1]
if bla2[i][1] < bla1[i][1]:
num = bla1[i][1] - bla2[i][1]
t1 = t1[0:bla1[i][1]] + ' '*num + t1[bla1[i][1]:len(t1)]
bla1[i][0] = ' '*num + bla1[i][0]
bla1[i][1] = bla2[i][1]
if bla1[i][2] > bla2[i][2]:
num = bla1[i][2] - bla2[i][2]
t2 = t2[0:bla2[i][2]] + ' '*num + t2[bla2[i][2]:len(t2)]
bla2[i][0] = bla2[i][0] + ' '*num
bla2[i][2] = bla1[i][2]
if bla2[i][2] > bla1[i][2]:
num = bla2[i][2] - bla1[i][2]
t1 = t1[0:bla1[i][2]] + ' '*num + t1[bla1[i][2]:len(t1)]
bla1[i][0] = bla1[i][0] + ' '*num
bla1[i][2] = bla2[i][2]
t11 = []
t11 = t1[0:bla1[0][1]]
t11 += t1[bla1[0][2]:bla1[1][1]]
t11 += t1[bla1[1][2]:bla1[2][1]]
t11 += t1[bla1[2][2]:bla1[3][1]]
t11 += t1[bla1[3][2]:bla1[4][1]]
t11 += t1[bla1[5][2]:bla1[6][1]]
t11 += t1[bla1[6][2]:len(t1)]
t12 = []
t12 = t2[0:bla1[0][1]]
t12 += t2[bla1[0][2]:bla1[1][1]]
t12 += t2[bla1[1][2]:bla1[2][1]]
t12 += t2[bla1[2][2]:bla1[3][1]]
t12 += t2[bla1[3][2]:bla1[4][1]]
t12 += t2[bla1[5][2]:bla1[6][1]]
t12 += t2[bla1[6][2]:len(t2)]
After ranging the blocks into an organised format bla1, bla2 where each difference is stored as a string with its start and end position eg ['v', 33, 34] for each separate string. After this, I attempt to insert spaces to match the length and separation factors necessary and this is where the code starts to break.
Please if someone could take a look!
I have worked through resolving this, and since no one has posted a response I will post the progress and solution. The following code is progress ... it worked well when dealing with variations that had less offset but began to break when getting into larger differences, specifically in maintaining spacing (offset) in matching up the two.
from difflib import SequenceMatcher
import pdb
t1 = 'betty: backstreetvboysareback"give.jpg"LAlarrygarryhannyhref="ang"_self'
t2 = 'betty: backstreetvboysareback"lol.jpg"LAlarrygarryhannyhref="ang"_self'
#t2 = 'bettyv: backstreetvboysareback"lifeislike"LAlarrygarryhannyhref="in.php"_selff'
#t2 = 'LA'
#t2 = 'c give.'
#t2 = 'give.'
#t1 = 'betty : backstreetvboysareback" i e "LAlarrygarryhannyhref=" n "_self'
#t2 = 'betty : backstreetvboysareback" i e "LAlarrygarryhannyhref=" n "_self'
#o1 = ' g v .jpg g '
#o2 = ' v l f islike i .php '
matcher = SequenceMatcher(None, t1, t2)
blocks = matcher.get_matching_blocks()
#print(len(blocks))
bla1 = []
bla2 = []
#bla = (string), (first pos), (second pos), (pos1 + pos2), (pos + pos2 total positions added togeather)
dnt = False
for i in range(len(blocks)):
if i == 0:
if blocks[i].a != 0 and dnt == False:
bla1.append([t1[blocks[i].a:blocks[i].b], 0, blocks[i].a, 0, 0])
bla2.append([t2[blocks[i].a:blocks[i].b], 0, blocks[i].b, 0, 0])
dnt = True
if blocks[i].b != 0 and dnt == False:
bla2.append([t2[blocks[i].a:blocks[i].b], 0, blocks[i].b, 0, 0])
bla1.append([t1[blocks[i].a:blocks[i].b], 0, blocks[i].a, 0, 0])
dnt = True
if i != len(blocks)-1:
print(blocks[i])
bla1.append([t1[blocks[i].a + blocks[i].size:blocks[i+1].a], blocks[i].a + blocks[i].size, blocks[i+1].a, 0, 0])
bla2.append([t2[blocks[i].b + blocks[i].size:blocks[i+1].b], blocks[i].b + blocks[i].size, blocks[i+1].b, 0, 0])
#pdb.set_trace()
ttl = 0
for i in range(len(bla1)):
cnt = bla1[i][2] - bla1[i][1]
if cnt != 0:
bla1[i][3] = cnt
ttl = ttl + cnt
bla1[i][4] = ttl
ttl = 0
for i in range(len(bla2)):
cnt = bla2[i][2] - bla2[i][1]
if cnt != 0:
bla2[i][3] = cnt
ttl = ttl + cnt
bla2[i][4] = ttl
print(bla1)
print(bla2)
tt1 = ''
dif = 0
i = 0
while True:
if i == 0:
if bla1[i][3] >= bla2[i][3]: dif = bla1[i][3]
if bla1[i][3] < bla2[i][3]: dif = bla2[i][3]
tt1 += t1[:bla1[i][1]] + '_'*dif
if i <= len(bla1) -1:
if bla1[i][3] >= bla2[i][3]: dif = bla1[i][3]
if bla1[i][3] < bla2[i][3]: dif = bla2[i][3]
if len(bla1) != 1:
if i == 0: tt1 += t1[bla1[i][1] + bla1[i][3]:bla1[i+1][1]]
if i != 0 and i != len(bla1)-1: tt1 += '_'*dif + t1[bla1[i][1] + bla1[i][3]:bla1[i+1][1]]
if i == len(bla1)-1: tt1 += '_'*dif + t1[bla1[i][1] + bla1[i][3]:len(t1)]
i = i+1
print('t1 = ' + tt1)
else:
break
tt2 = ''
i = 0
dif = 0
while True:
if i == 0:
if bla1[i][3] >= bla2[i][3]: dif = bla1[i][3]
if bla1[i][3] < bla2[i][3]: dif = bla2[i][3]
tt2 += t2[:bla2[i][1]] + '_'*dif
if i <= len(bla2) -1:
if bla1[i][3] >= bla2[i][3]: dif = bla1[i][3]
if bla1[i][3] < bla2[i][3]: dif = bla2[i][3]
if len(bla2) != 1:
if i == 0: tt2 += t2[bla2[i][1] + bla2[i][3]:bla2[i+1][1]]
if i != 0 and i != len(bla1)-1: tt2 += '_'*dif + t2[bla2[i][1] + bla2[i][3]:bla2[i+1][1]]
if i == len(bla2)-1: tt2 += '_'*dif + t2[bla2[i][1] + bla2[i][3]:len(t2)]
i = i+1
print('t2 = ' + tt2)
else:
break
print()
Solution:
Unfortunately I have been too busy to continue coding this and have resorted to sub-processing diffutils ... this is a wonderful alternative to a lot of painstaking coding!

What would I need to write if I wanted to make a timer cancel based on the increasing of the score? (Corona)

I'm trying to cancel a timer when the player chooses the correct character. I have come to the conclusion that the two possible ways to do this are to cancel the timer based on the increase of the score, or on the initialization of the addEventListener. I have tried to fiddle around with some possible options, but none of them worked. Any ideas on what to write to make the condition viable so that the timer will cancel based on such event happening? here is the code I have thus far:
function timeClock(event)
if event.time > 2000 then
storyboard.gotoScene( "restartEasy" )
randomImage.alpha = 0
else
timer.cancel( event.source )
end
end
function endGame(event)
if imageFile == "redbox.png" then
timer.performWithDelay( 2000, timeClock )
randomImage.alpha = 0
mydata.score = mydata.score + 1
scoreText.text = mydata.score
button1.x = math.random( 55, 300)
button1.y = math.random( 55, 300)
button2.x = math.random( 55, 300)
button2.y = math.random( 55, 300)
imageFile = imageFiles[math.random(2)]
randomImage = display.newImage(imageFile, centerX, screenTop + 20)
storyboard.gotoScene( "restartEasy" )
randomImage.alpha = 0
end
end
function endGame2(event)
if imageFile == "bluebox.png" then
timer.performWithDelay( 2000, timeClock )
randomImage.alpha = 0
mydata.score = mydata.score + 1
scoreText.text = mydata.score
button1.x = math.random( 55, 300)
button1.y = math.random( 55, 300)
button2.x = math.random( 55, 300)
button2.y = math.random( 55, 300)
imageFile = imageFiles[math.random(2)]
randomImage = display.newImage(imageFile, centerX, screenTop + 20)
else
storyboard.gotoScene( "restartEasy" )
randomImage.alpha = 0
end
end
button1:addEventListener("touch", endGame)
button2:addEventListener("touch", endGame2)
end
Name the timer a public variable for instance
timer1 = timer.performWithDelay(2000, timeClock)
Then in your function cancel the timer using
timer.cancel(timer1)

What is wrong with my function in Octave?

I just tried to create my first function in octave, it looks as follows:
function hui(x)
if(0 <= x && x <2)
retval = (1.5 * x + 2)
elseif(2<= x && x <4)
retval = (-x + 5)
elseif(4<= x && x < 6)
retval = (0.5 * x)
elseif(6<= x && x < 8)
retval = (x - 3)
elseif(8<= x && x <=10)
retval = (2 * x - 11)
endif
endfunction
but if I try to plot it using: x=0:0.1:10; plot(x, hui(x));
It shows a plot witch seems a little bit strange.
What did I wrong?
Thanks in advance
John
You'll have to pardon my rustiness with the package, but you need to change the code around a bit. Notably, the notation 0<=x is incorrect, and must be x>=0. Since hui is operating on a vector, I believe you need to take that into account when constructing your return value.
I'm sure there are more effective ways of vectorizing this, but basically, While stepping over the input vector, I added the latest value onto the return vector, and at the end lopping off the initial 0 that I had put in. I put in a sentinel value in case the input didn't fulfill one of the criteria (it was always taking the "else" path in your code, so putting something there could have alerted you to something being wrong).
function [retval] = hui(x)
retval = 0
for i=1:size(x,2)
if(x(i)>=0 && x(i) <2)
retval = [retval (1.5 * x(i) + 2)];
elseif( x(i)>=2 && x(i) <4)
retval = [retval (-1*x(i) + 5)];
elseif(x(i)>=4 && x(i) < 6)
retval = [retval (0.5 * x(i))];
elseif(x(i)>=6 && x(i) < 8)
retval = [retval (x(i) - 3)];
elseif(x(i)>=8 && x(i) <=10)
retval = [retval (2 * x(i) - 11)];
else
retval = -999;
endif
endfor
retval = retval(2:size(retval,2));
endfunction
x is a vector, so you either need to loop through it or vectorise your code to removing the need.
As you're using Octave, it's worth vectorising everything you possibly can. The easiest way I can think of doing this is:
x = 0:0.1:10;
y = x;
y(x >= 0 & x < 2) = x(x >= 0 & x < 2) * 1.5 + 2;
y(x >= 2 & x < 4) = x(x >= 2 & x < 4) * -1 + 5;
y(x >= 4 & x < 6) = x(x >= 4 & x < 6) * 0.5;
y(x >= 6 & x < 8) = x(x >= 6 & x < 8) - 3;
y(x >= 8 & x < 10) = x(x >= 8 & x < 10) * 2 - 11;
The y(x >= a & x < b) syntax is logical indexing. Alone, x >= a & x < b gives you a vector of logical values, but combined with another vector you get the values which meet the condition. Octave will also let you do assignments like this.

Resources