Informatica- What is the result if the IIF condition is null? - informatica

If my Informatica code looks like this:
IIF(foo,1,0)
and foo = NULL, what would the output be?
Sorry if the answer to this is to obvious or is in a reference somewhere, but I couldn't find anything useful through googling or searching SO.

foo is a expression that should evaluate to either TRUE or FALSE, so here's how Integration Service interprets different integer values when they constitute the entire condition:
IIF( -7, 1, 0) -> 1
IIF( 0, 1, 0) -> 0
IIF(NULL, 1, 0) -> 0
IIF( 7, 1, 0) -> 1
All negative or positive integers yield TRUE.
The values 0 or NULL give FALSE.

If I were you, I'd attempt at rewriting this particular piece. Although its not incorrect, its best not to code like this. wrap the "foo" around a isnull() if you suspect it to be such. Further, use an nvl equivalent and check for those values. Conditional checks dealing with NULL are always expensive.

Related

Make =IF Function Output Numbers For "Scoring": Google Sheets

I'm am exploring methods of giving scores to different datapoints within a dataset. These points come from a mix of numbers and text string attributes looking for certain characteristics, e.g. if Col. A contains more than X number of "|", then give it a 1. If not, it gets a 0 for that category. I also have some that give the point when the value is >X.
I have been trying to do this with =IF, for example, =IF([sheet] = [Text], "1","0").
I can get it to give me 1 or 0, but I am unable to get a point total with sum.
I have tried changing the formatting of the text to both "number", "plain text", and have left it as automatic, but I can't get it to sum. Thoughts? Is there maybe a better way to do this?
FWIW - I'm trying to score based on about 12 factors.
Best,
Alex
The issue here might be that you're having the cell evaluate to either the string "0" or the string "1" rather than the number 0 or the number 1. That would explain why you're seeing the right things but the math isn't coming out right - the cell contents look like numbers, but they're really text, which the summation would then ignore.
One option would be to drop the quotation marks and write something like this:
=IF(condition, 1, 0)
This has the condition evaluate to 1 if it's true and 0 if it's false.
Alternatively, you could write something like this:
=(condition) * 1
This will take the boolean TRUE or FALSE returned by condition and convert it to either the numeric value 1 (true) or the numeric value 0 (false).

Power Query M - We cannot convert the value null to type Logical

In Power BI I have an M Query that tests for the value in a column equaling or not equaling to null.
When I add the statement for [Sale.Revenue] <> null I get an error however it works fine for the [UserRole.Name] = null it works fine. Tested just by removing the statement and adding it back.
We cannot convert the value null to type Logical.
This seems like it should work but just can't figure it out.
add_user_role_group = Table.AddColumn(
join_expand_sale,
"UserRole.Group1",
each (
if [UserRole.Name] = null and
[Sale.Revenue] <> null then
"Group1"
else if Text.Contains([UserRole.Name], "Manager") then
"Group2"
else
"Undefined"
)
)
I am sure it is something glaringly obvious :/ Thanks for your thoughts on this.
One of your rows has a null value for both UserRole.Name and Sale.Revenue. You need to check for that explicitly, and then add it to the "Undefined" group.
What happened is that the first condition fails because Sale.Revenue is null. The second condition calls Text.Contains, which returns null when [UserRole.Name] is null (Text.Contains returns a nullable logical value). null is not true or false, so you get the error.
After a such journey, finaly I found Text.Length !!
You can solve your problem like this:
if Text.Length([UserRole.Name]) = 0 and
Text.Length([Sale.Revenue]) > 0 then
I hope I have helped you.
Reference: Power Query M - Text.Length
Your issue is in the Text.Contains formula. You create an if statement that expects an expression that returns either true or false.
When the Text.Contains formula contains a null value, it returns 'null' as answer, and not true or false. You can adjust your code:
Text.Contains([UserRole.Name], "Manager")
To
Text.Contains([UserRole.Name]??"", "Manager")
The ?? is the COALESCE operator. In case it finds a null value, it now treats it as "". Instead of returning null it now returns true or false.
More on text functions in this article: https://gorilla.bi/power-query/text-functions/
Enjoy Power Query,
Rick

np.delete and np.s_. What's so special about np_s?

I don't really understand why regular indexing can't be used for np.delete. What makes np.s_ so special?
For example with this code, used to delete the some of the rows of this array..
inlet_names = np.delete(inlet_names, np.s_[1:9], axis = 0)
Why can't I simply use regular indexing and do..
inlet_names = np.delete(inlet_names, [1:9], axis = 0)
or
inlet_names = np.delete(inlet_names, inlet_names[1:9], axis = 0)
From what I can gather, np.s_ is the same as np.index_exp except it doesn't return a tuple, but both can be used anywhere in Python code.
Then when I look into the np.delete function, it indicates that you can use something like [1,2,3] to delete those specific indexes along the entire array. So whats preventing me from using something similar to delete certain rows or columns from the array?
I'm simply assuming that this type of indexing is read as something else in np.delete so you need to use np.s_ in order to specify, but I can't get to the bottom of what exactly it would be reading it as because when I try the second piece of code it simply returns "invalid syntax". Which is weird because this code works...
inlet_names = np.delete(inlet_names, [1,2,3,4,5,6,7,8,9], axis = 0)
So I guess the answer could possibly be that np.delete only accepts a list of the indexes that you would like to delete. And that np._s returns a list of the indexes that you specify for the slice.
Just could use some clarification and some corrections on anything I just said about the functions that may be wrong, because a lot of this is just my take, the documents don't exactly explain everything that I was trying to understand. I think I'm just overthinking this, but I would like to actually understand it, if someone could explain it.
np.delete is not doing anything unique or special. It just returns a copy of the original array with some items missing. Most of the code just interprets the inputs in preparation to make this copy.
What you are asking about is the obj parameter
obj : slice, int or array of ints
In simple terms, np.s_ lets you supply a slice using the familiar : syntax. The x:y notation cannot be used as a function parameter.
Let's try your alternatives (you allude to these in results and errors, but they are buried in the text):
In [213]: x=np.arange(10)*2 # some distinctive values
In [214]: np.delete(x, np.s_[3:6])
Out[214]: array([ 0, 2, 4, 12, 14, 16, 18])
So delete with s_ removes a range of values, namely 6 8 10, the 3rd through 5th ones.
In [215]: np.delete(x, [3:6])
File "<ipython-input-215-0a5bf5cc05ba>", line 1
np.delete(x, [3:6])
^
SyntaxError: invalid syntax
Why the error? Because [3:4] is an indexing expression. np.delete is a function. Even s_[[3:4]] has problems. np.delete(x, 3:6) is also bad, because Python only accepts the : syntax in an indexing context, where it automatically translates it into a slice object. Note that is is a syntax error, something that the interpreter catches before doing any calculations or function calls.
In [216]: np.delete(x, slice(3,6))
Out[216]: array([ 0, 2, 4, 12, 14, 16, 18])
A slice works instead of s_; in fact that is what s_ produces
In [233]: np.delete(x, [3,4,5])
Out[233]: array([ 0, 2, 4, 12, 14, 16, 18])
A list also works, though it works in different way (see below).
In [217]: np.delete(x, x[3:6])
Out[217]: array([ 0, 2, 4, 6, 8, 10, 14, 18])
This works, but produces are different result, because x[3:6] is not the same as range(3,6). Also the np.delete does not work like the list delete. It deletes by index, not by matching value.
np.index_exp fails for the same reason that np.delete(x, (slice(3,6),)) does. 1, [1], (1,) are all valid and remove one item. Even '1', the string, works. delete parses this argument, and at this level, expects something that can be turned into an integer. obj.astype(intp). (slice(None),) is not a slice, it is a 1 item tuple. So it's handled in a different spot in the delete code. This is TypeError produced by something that delete calls, very different from the SyntaxError. In theory delete could extract the slice from the tuple and proceed as in the s_ case, but the developers did not choose to consider this variation.
A quick study of the code shows that np.delete uses 2 distinct copying methods - by slice and by boolean mask. If the obj is a slice, as in our example, it does (for 1d array):
out = np.empty(7)
out[0:3] = x[0:3]
out[3:7] = x[6:10]
But with [3,4,5] (instead of the slice) it does:
keep = np.ones((10,), dtype=bool)
keep[[3,4,5]] = False
return x[keep]
Same result, but with a different construction method. x[np.array([1,1,1,0,0,0,1,1,1,1],bool)] does the same thing.
In fact boolean indexing or masking like this is more common than np.delete, and generally just as powerful.
From the lib/index_tricks.py source file:
index_exp = IndexExpression(maketuple=True)
s_ = IndexExpression(maketuple=False)
They are slighly different versions of the same thing. And both are just convenience functions.
In [196]: np.s_[1:4]
Out[196]: slice(1, 4, None)
In [197]: np.index_exp[1:4]
Out[197]: (slice(1, 4, None),)
In [198]: np.s_[1:4, 5:10]
Out[198]: (slice(1, 4, None), slice(5, 10, None))
In [199]: np.index_exp[1:4, 5:10]
Out[199]: (slice(1, 4, None), slice(5, 10, None))
The maketuple business applies only when there is a single item, a slice or index.

IF statement in Excel with date1 and endtime variables

I have got an excel which has an formula and I am not able to understand how its working.
Here is the formula
=IF(C6=date1,0,IF(D6-endtime<=0,0,IF(D6-endtime>0,(D6-endtime)*1440)))
Can someone spends few minutes to post an explanation, how it works
Thanks,
Try looking at it like this:
=IF(C6=date1,
0,
IF(D6-endtime<=0,
0,
IF(D6-endtime>0,
(D6-endtime)*1440
#no "else statement" here!
)
)
)
So the first thing checked is if the cell C6 equals date1, which I think is a named range. If they are equal, then the whole equation resolves to the next line, 0
If they are not equal then D6-endtime is evaluated, if it is less than or equal to zero, then the equation resolves to zero.
If D6-endtime is greater than 0, then the next test is true and the whole equation resolves to (D6-endtime)*1440. There is no else in this last test because equation assumes D6-endtime will always be numeric.
Here's how I understand IF statements work in excel
=if(logical test,value if test true,value if test false)
For logical test, you have to use something that resolves to TRUE or FALSE, or you can specify TRUE or FALSE directly(but then you don't need an IF statement)
value if test true, if the test resolves to TRUE(like 1=1), then the cell will display this value, and supply this value to other functions
value if test false, if the test resolves to FALSE(like 1=0), then the cell will display this value, and supply this value to other functions.
You can omit value if TRUE/FALSE, and excel will return TRUE or FALSE after evaluation of the statement.

what is wrong with my if -condition?

So I've been trying to figure out what is wrong with my if-condition, but I am getting nowhere. I am still new to R, so maybe I am not understanding some very basic concept here?
I have a dataframe (dc) to which I appended a column with logical "FALSE". Now I want to change each FALSE into a TRUE based on the values in two columns of dc (dc$Probe and dc$Resp) that I specified using regexpr().
What it does so far is that, for both if-conditions, it changes each FALSE into TRUE regardless of the values in column 5 of dc. When I run the if-conditions seperately, I can see that they seem to be working fine on the OR-part of the condition, meaning the code only generates TRUE when the strings in dc$Probe match one of the strings specified in the OR-part. However, the AND-part seems to be ignored? Thus, when I run the complete code, I get a column with only TRUE, which is not what I want.
Edit: I should get a TRUE only if the string in Probe ends in a certain pattern (as specified in either of the two if conditions I wrote) and if the corresponding value in Resp is a "100" for the patterns specified in my first condition or a "200" for the patterns specified in my second condition. Thus, for strings ending in (sg|s|w1|w3|s1|s2), Resp must be "100" to get a TRUE and for strings ending in (\d\dg|\d\d), Resp must be "200" to get a TRUE. All other cases should be FALSE. For example, if a string ends in s1 and the corresponding value in Resp is 200, the code should return FALSE.
Edit: Some example data:
>dc<-data.frame(Subject=rep("SN",6), item.c=(1:6), Stim=c("XYZc02s03","XYZc01s30","XYZc02s29", "XYZc01s38", "XYZc02s11", "XYZc06w21"), Probe=c("XYzf02s03","XYZf01s30g","XYZf02s29w1","XYZf01s38sg","XYZf02s11s","XYZv06w21s1"), Resp=c(200, 100, 100, 100, 100, 200))
This is my code:
>dc$Resp<-as.character(dc$Resp) #column 5 in dc
dc$Probe<-as.character(dc$Probe)
dc$correct_response <- FALSE
for (i in 1:nrow(dc)) {
if (regexpr("^.*sg$", dc$Probe[i])==1 || regexpr("^.*s$", dc$Probe[i])==1 || regexpr("^.*w1$", dc$Probe[i])==1 || regexpr("^.*w3$", dc$Probe[i])==1 || regexpr("^.*s1$", dc$Probe[i])==1 || regexpr("^.*s2$", dc$Probe[i])==1 && dc[i,5]=="100") {(dc$correct_response[i]<- TRUE)}
if (regexpr("^.*\\d\\dg$", dc$Probe[i])==1 || regexpr("^.*\\d\\d$", dc$Probe[i])==1 && dc[i,5]=="200") {(dc$correct_response[i]<- TRUE)}
}
Is there something wrong with the regular expressions I am using? I checked them with glob2rx() and it seems like they are ok...Is my use of "OR" (||) or/and "AND" (&&) incorrect? How do I implement the AND-part properly? I have also tried the following code for the AND-part, but it didn't change anything:
regexpr("200", dc$Resp[i])==1
I read the R-help on regular expressions and control flow, but I still don't see what I am doing wrong. Consulting other webpages on logical expressions did not help me either.
Please help!
Im wondering if it can all be reduced to the following:
dc<- read.table(header=T,text="Subject item.c Stim Probe Resp
SN 1 XYZc02s03 XYzf02s03 200
SN 2 XYZc01s30 XYZf01s30g 100
SN 3 XYZc02s29 XYZf02s29w1 100
SN 4 XYZc01s38 XYZf01s38sg 100
SN 5 XYZc02s11 XYZf02s11s 100
SN 6 XYZc06w21 XYZv06w21s1 200")
cond1<-regexpr("^.*(sg|s|w1|w3|s1|s2)$", dc$Probe)==1 & dc$Resp==100
cond2<-regexpr("^.*(\\d\\dg|\\d\\d)$", dc$Probe)==1 & dc$Resp==200
dc$correct_response<-cond1|cond2
For one thing, you are missing a logical operator between the 2nd and 3rd clauses of your first if statement.