Wrong SSRS Expression - reporting-services

I've got this expression in an SSRS form:
=IIf(Fields!Number_Of_Txns.Value > 2, (Fields!Avg_Interpurch_Interval.Value/(IIf(Fields!St_Dev_Interpurch_Interval.Value = 0, 10000, Fields!St_Dev_Interpurch_Interval.Value))), 0.2)
What it should do is: if the Number_Of_Txns is > 2 and the standard deviation is not = 0 then divide the interpurchase interval by the standard deviation on the other hand if the standard deviation = 0 or the Number_Of_Txns <= 2 than just return 0.2
Where is the error?

You should first check both conditions, so your expression should be:
=IIf(Fields!Number_Of_Txns.Value > 2 AND Fields!St_Dev_Interpurch_Interval.Value <> 0
,Fields!Avg_Interpurch_Interval.Value / Fields!St_Dev_Interpurch_Interval.Value
, 0.2)

Related

Access VBA IF (X and Y) > 0 then

I'm stumped on this one. I have some functioning VBA code in Access that looks like this.
If (intFrontLoaded And 2) > 0 Then boolFrontLoad(1) = True Else boolFrontLoad(1) = False
If (intFrontLoaded And 4) > 0 Then boolFrontLoad(2) = True Else boolFrontLoad(2) = False
If (intFrontLoaded And 8) > 0 Then boolFrontLoad(3) = True Else boolFrontLoad(3) = False
If (intFrontLoaded And 16) > 0 Then boolFrontLoad(4) = True Else boolFrontLoad(4) = False
If (intFrontLoaded And 32) > 0 Then boolFrontLoad(5) = True Else boolFrontLoad(5) = False
If (intFrontLoaded And 64) > 0 Then boolFrontLoad(6) = True Else boolFrontLoad(6) = False
I'm trying to figure out how the (intFrontLoaded And X) > 0) works.
I know what it does, I'm trying to figure out how, example:
If intFrontLoaded = 14 then boolFrontLoad(1), (2) and (3) will be true.
If intFrontLoaded = 28 then boolFrontLoad(2), (3) and (4) will be true.
I understand that 2+4+8 = 12 and 4+8+16 = 28, but how does (intFrontLoaded And X) > 0) do the calculation?
And in this context is a bitwise AND operator. The test is checking for a single flag bit. Let's use your example of intFrontLoaded = 14 with If (intFrontLoaded And 4) > 0 Then.
14 as bitflags is this: 0000 0000 0000 1110
4 is this: 0000 0000 0000 0010
The result of And is every bit that is the same. In the example above, it's only the "flag" bit, 4. So, the result of the And operation is 4.
Now plug that back into the expression:
If 4 > 0 Then
So, it executes the "true" condition. If you'll notice, all of the tests are powers of 2. This is because when they are represented as binary, they'll only be only a single bit.
Basically, intFrontLoaded is storing a single boolean value for each of the bits being tested. This was much more common in early computing when memory was at a premium and using all 16 bits to store a boolean was considered wasteful.
Note that you can simplify this to:
boolFrontLoad(1) = intFrontLoaded And 2
boolFrontLoad(2) = intFrontLoaded And 4
boolFrontLoad(3) = intFrontLoaded And 8
boolFrontLoad(4) = intFrontLoaded And 16
boolFrontLoad(5) = intFrontLoaded And 32
boolFrontLoad(6) = intFrontLoaded And 64
The And operator is a bitwise AND operation - it compares the bits in each operand and returns the value where both operands have bits in common.
That said, your code would be much clearer written as:
boolFrontLoad(1) = (intFrontLoaded And 2) > 0
boolFrontLoad(2) = (intFrontLoaded And 4) > 0
boolFrontLoad(3) = (intFrontLoaded And 8) > 0
boolFrontLoad(4) = (intFrontLoaded And 16) > 0
boolFrontLoad(5) = (intFrontLoaded And 32) > 0
boolFrontLoad(6) = (intFrontLoaded And 64) > 0
This is called a bitwise operation. The logical And is performed bit per bit between intFrontLoaded And X. When X is a power of 2, say 2^a, its binary representation is composed of zeros except a 1 on the (a+1)'th position (numbering the bits from the right to the left).
Therefore, intFrontLoaded And 4 checks whether the third bit in intFrontLoaded is set. if the result is non-zero, the IF will succeed.
In your code intFrontLoaded is used as a bit-set, that is, a set of flags where each bit represents a flag for some boolean condition.

How to replace Null fields with 0s

I have a field in a matrix that has some nulls. I would like to replace all the nulls with 0s. This is the expression I currently have in the field:
=Sum(IIf(Code.isVM(Fields!deviceType.Value), 0, 1))
I'm guessing I have to have an IsNothing expression in there but I can't figure out how to add this with this existing expression.
How about:
=Iif(
Sum(IIf(Code.isVM(Fields!deviceType.Value), 0, 1)) Is Nothing,
0,
Sum(IIf(Code.isVM(Fields!deviceType.Value), 0, 1))
)
I recall testing for length greater than 0 worked well.
=IIf(Len(Fields!deviceType.Value) > 0, Fields!deviceType.Value, 0)

#Error in a greater than iif sum statement

I'm trying to write some code that will prevent a #Error.
I don't know where my issue is.
=Sum(IIF(Fields!CurrentNumDaysOD.Value >= 1 And Fields!CurrentNumDaysOD.Value <= 10,
Fields!CurrentBalance.Value, "0")) / Sum(IIF(Fields!CurrentNumDaysOD.Value > 0,
Fields!CurrentBalance.Value, "0"))
I have also attempted this:
=Sum(IIF(Fields!CurrentNumDaysOD.Value >= 1 And Fields!CurrentNumDaysOD.Value <= 10,
Fields!CurrentBalance.Value, Nothing)) / Sum(IIF(Fields!CurrentNumDaysOD.Value > 0,
Fields!CurrentBalance.Value, Nothing))
If the result of the second half of your statement (after the /) is 0, then you will be dividing by 0, which will cause an error.

AS3 convert a positive number to 1 and a negative number to -1

There is a simple trick to convert a number to 1 or -1.
Just raise it to the power of 0.
So:
4^0 = 1
-4^0 = -1
However, in AS3:
Math.pow( 4, 0); // = 1
Math.pow(-4, 0); // = 1
Is there a way to get the right answer without an if else?
This could be done bitwise.
Given the number n (avg time: 0.0065ms):
1 + 2 * (n >> 31);
Or slightly slower (avg time: 0.0095ms):
(n < 0 && -1) || 1;
However, Marty's solution is the fastest (avg time: 0.0055ms)
n < 0 ? -1 : 1;
Not sure if without an if/else includes the ternary operator in your eyes, but if not:
// Where x is your input.
var r:int = x < 0 ? -1 : 1;
Will be more efficient than Math.pow() anyway.

Why is this IIF function giving an #Error?

One of my tables has a field named Cost and a field named Extra Cost. To come up with the Total Cost, I add them together with the following field, which works just fine:
Total Cost: (Val(nz([Cost],"")))/100 + (Val(nz([Extra Cost],"")))/100
(I divide by 100 because Cost and Extra Cost are stored without a decimal point)
Now it's possible that a record will have Cost = 0, and Extra Cost > 0. But if Cost = 0, I want Total Cost to also = 0. I came up with the following, but it results in #Error if Cost = 0. It works fine if Cost > 0:
Total Cost: IIf([Cost]>0,((Val(nz([Cost],"")))/100+(Val(nz([Extra Cost],""))))/100,0)
Basically I'm looking for:
If Cost = 0, Then Total Cost = 0
Else
If Cost > 0, Then Total Cost = Cost + Extra Cost
What is wrong with the 'true' portion?
Here's a few examples of the data:
Cost Extra Cost
100 2.5
250 1.5
150 2.5
null 2.75
Based on your description, I think you can divide by 100 after you add the 2 values instead of dividing each of them by 100 before you add them. That shouldn't affect the logic, but should give you a simpler IIf expression ... which will hopefully be easier to diagnose.
IIf
(
Val(Nz([Cost], "0")) > 0,
(Val([Cost]) + Val(Nz([Extra Cost], "0"))) / 100,
0
)
Using your sample data in Access 2007, I get this result set from the following query:
Cost Extra Cost Total Cost
100 2.5 1.025
250 1.5 2.515
150 2.5 1.525
2.75 0
SELECT
y.Cost,
y.[Extra Cost],
IIf
(
Val(Nz([Cost], "0")) > 0,
(Val([Cost]) + Val(Nz([Extra Cost], "0"))) / 100,
0
) AS [Total Cost]
FROM YourTable AS y;
If the issue is that [Total Cost] requires a text value, you can use CStr() to cast the IIf numerical value to string.
CStr(
IIf
(
Val(Nz([Cost], "0")) > 0,
(Val([Cost]) + Val(Nz([Extra Cost], "0"))) / 100,
0
)
)