Remove NaN in Nested iif in SSRS expression - reporting-services

New to SQL-SSRS
How do I add functionality to the below code to change returned NaN values to 0 or blank? What I think is happening is in situations that the sum of Agreed and Disagreed = 0 I am getting this Nan. I think this would be easier for me if I wasnt dealing with the Agreed and Disagreed categories. Again, new to SQL programming. Any help would be greatly appreciated.
=(Sum(iif(Fields!Response.Value = "Agreed",Fields!Days.Value,0)) + Sum(iif(Fields!Response.Value = "Disagreed",Fields!Days.Value,0)))/(Sum(iif(Fields!Response.Value = "Agreed",Fields!Fq.Value,0)) + Sum(iif(Fields!Response.Value = "Disagreed",Fields!Fq.Value,0)))

Try something like below,
=Replace((Sum(IIF(Fields!Response.Value = "Agreed",Fields!Days.Value,0)) +
Sum(IIF(Fields!Response.Value = "Disagreed",Fields!Days.Value,0))),"NaN","0")
/ Replace((Sum(IIF(Fields!Response.Value = "Agreed",Fields!Fq.Value,0))
+ Sum(IIF(Fields!Response.Value = "Disagreed",Fields!Fq.Value,0))),"NaN","0")
Also, look at this

Rather than replacing the text returned from dividing by zero, you should handle the DIV 0 issue in the expression:
=IIF(
SUM(IIF(Fields!Response.Value = "Agreed" OR Fields!Response.Value = "Disagreed", Fields!Fq.Value, 0)) = 0, 0,
SUM(IIF(Fields!Response.Value = "Agreed" OR Fields!Response.Value = "Disagreed", Fields!Days.Value, 0)))
/IIF(
SUM(IIF(Fields!Response.Value = "Agreed" OR Fields!Response.Value = "Disagreed", Fields!Fq.Value, 0)) = 0, 1,
SUM(IIF(Fields!Response.Value = "Agreed" OR Fields!Response.Value = "Disagreed", Fields!Fq.Value, 0)))
If the SUM is zero, the calculation is 0 / 1 otherwise it's the SUM(Days) / SUM(Fq) where the Response is Agreed or Disagreed.
And rather than two sum, just use an OR in the one.

Public Function Divider (ByVal Dividend As Double, ByVal Divisor As Double)
If IsNothing(Divisor) Or Divisor = 0
Return 0
Else
Return Dividend/Divisor
End If
End Function
this function enables you to write a custom code which fixes all problems with Nan .
this can be called by below
=Code.Divider(Fields!FieldA.Value, Fields!FieldB.Value)
your code is as below
=Code.Divider(((Sum(iif(Fields!Response.Value = "Agreed",Fields!Days.Value,0)) + Sum(iif(Fields!Response.Value = "Disagreed",Fields!Days.Value,0))), ((Sum(iif(Fields!Response.Value = "Agreed",Fields!Fq.Value,0)) + Sum(iif(Fields!Response.Value = "Disagreed",Fields!Fq.Value,0)))))

Related

SSRS multiple iif statements rounds instead of keeping 2 decimal calculation

I have the following statement in a calculated field that will only return whole numbers instead of a 2 decimal dollar amount. If I only use one of the 3 iif conditions it works fine but as soon as I add the second or all 3 it will not return a decimal value. Ive also tried all 3 iif conditions separaately and they all work. It appears to be just the OR's causing the issue???
=
(iif ((Fields!loc.Value = "C08") OR (Fields!loc.Value = "C09"), Fields!price1.Value, Fields!price1.Value - Fields!price2.Value) )
OR
(iif ((Fields!loc.Value = "C33") OR (Fields!loc.Value = "C32") OR (Fields!loc.Value = "C10") OR (Fields!loc.Value = "C30"), 0, Fields!price1.Value - Fields!price2.Value) )
OR
(iif (Fields!price1.Value = 0, 0, Fields!price1.Value - Fields!price2.Value))
You can't write and IIF like that, you would need to nest you IIFs which would get a bit messy.
You might find it easier to use a SWITCH statement which is a bit easier to read.
I might not have understood your logic properly but it should be close enough. Note that the first expression that returns true is what will get returned so in thsi case I have assume that if price1 is 0 then the returned value will always be zero, if this is incorrect, just re-order the expression pairs
=SWITCH(
Fields!price1.Value = 0, 0,
Fields!loc.Value = "C08" OR Fields!loc.Value = "C09", Fields!price1.Value,
Fields!loc.Value = "C33" OR Fields!loc.Value = "C32" OR Fields!loc.Value = "C10" OR Fields!loc.Value = "C30", 0,
True, Fields!price1.Value - Fields!price2.Value
)
This reads... "If price1 is zero then return 0" else "if loc is C08 or C09 return price1" else "if loc is C33, C32, C10 or C30 then return 0". Finally, if no previous test return true, then return price1-price2 .
The final True acts like and else
This could be simplified but I wanted to leave it as close to your original attempt as I could.

How is the best way to integrate using loops

I'm using Mathematica, and I want to integrate a function f[wr] in wr using some method that works with a Table/Do/For in the variable wr.
I tried to use something like the Riemann's sum, to evaluate the function in some point wr_0 times the spacing (in this case 0.01). The problem is that wr depends on "[Micro]" and for each value of Micro, the integral in wr would change its limits (from [Micro] - 50*kt to [Micro] + 50*kt).
kb = 8.61*10^-5;
Te = 300;
kt = kb*Te;
sum = 0;
sum1 = 0;
data = Table[
{
Table[
{
sum = sum + (f[wr]*0.01);
sum1 = sum1 + (f[wr]*(wr - \[Micro])*0.01);
}
, {wr, \[Micro] - 50*kt, \[Micro] + 50*kt, 0.01}];
\[Micro], sum1/(Te*sum)
}
, {\[Micro], -0.4, 0.4, 0.1}];
I think this code, is not doing what I want. That is, for each value of Micro, calculate the integral (sum1 and sum) for a range of wr's and store, then for other value of Micro, do it all again, at the end i want the output of the quotient of these integrals. How could I implement that?

Substituting multiple inputs for a function with one function?

I am trying to see if I can simplify input by using a function that produces more than one output for use with another function. Is there any way I can do this? Do I HAVE to make a function to return single variables for each input?
--here is a snippet of what im trying to do (for a game)
--Result is the same for game environment and lua demo.
en = {
box ={x=1,y=2,w=3}
}
sw = {
box = {x=1,y=2,w=3}
}
function en.getbox()
return en.box.x,en.box.y,en.box.w,en.box.w
end
function sw.getbox()
return sw.box.x,sw.box.y,sw.box.w,sw.box.w
end
function sw.getvis()
return true
end
function CheckCollision(x1,y1,w1,h1, x2,y2,w2,h2)
return x1 < x2+w2 and
x2 < x1+w1 and
y1 < y2+h2 and
y2 < y1+h1
end
if CheckCollision(en.getbox(),sw.getbox()) == true then
if sw.getvis() == true then
en.alive = false
end
end
print(tostring(en.alive))
I am expecting the enemy (en) to die (en.alive = false) but I am getting the error: input:25: attempt to perform arithmetic on a nil value (local 'w2')
You can find an explaination for the issue you are seeing here: Programming in Lua: 5.1 – Multiple Results
I suggest you read the whole page but here is a relevant section
A function call that is not the last element in the list always produces one result:
x,y = foo2(), 20 -- x='a', y=20
x,y = foo0(), 20, 30 -- x=nil, y=20, 30 is discarded
I suggest the following changes to get your code working. wrap your output from getbox into a table with clear keys that make it easy to understand.
function en.getbox()
return {
x = en.box.x,
y = en.box.y,
w = en.box.w,
h = en.box.w
}
end
function sw.getbox()
return {
x = sw.box.x,
y = sw.box.y,
w = sw.box.w,
h = sw.box.w
}
end
function CheckCollision(o1, o2)
return o1.x < o2.x + o2.w and
o2.x < o1.x + o1.w and
o1.y < o2.y + o2.h and
o2.y < o1.y + o1.h
end
Alternatively you can wrap the output of getbox "on the fly" like:
function CheckCollision(o1, o2)
return o1[1] < o2[1] + o2[3] and
o2[1] < o1[1] + o1[3] and
o1[2] < o2[2] + o2[4] and
o2[2] < o1[2] + o1[4]
end
if CheckCollision({en.getbox()}, {sw.getbox()}) == true then
if sw.getvis() == true then
en.alive = false
end
end
I do strongly encourage the first option over the last. The last option leads to code that is harder to follow and should be accompanied by clear comments explaining it.

SSRS #ERROR Issue

I am getting an #ERROR in SSRS. I believe it's happening when it tries to divide 0 by 0.
How can I change this to handle that scenario.
=Fields!Total_Incidents.Value/Fields!Units_Sold.Value
You can add a function in custom code of the report, ex:
function divide(a as decimal, b as decimal) as decimal
if (b > 0.0) then
divide = a/b
else
divide = 0
end if
end function
Then, call this function in your textbox expression
This was how I ended up doing it
=IIf(Fields!Units_Sold.Value = 0, 0, Fields!Total_Incidents.Value / IIf(Fields!Units_Sold.Value = 0, 1, Fields!Units_Sold.Value))
Check denominator is zero or not,
=IFF(Fields!Units_Sold.Value=0,0,Fields!Total_Incidents.Value/Fields!Units_Sold.Value)

Tips for function inside while loop and i=i+1, Matlab

I have a problem with a function in matlab. This specific function is for filtering light signals. As you can see below I added the coding I’ve used in the function and in the while loop itself. The code is written for a NXT Lego robot.
Is there any tip how to get the count variable ( i = i + 1 ) to work in the function, so we can plot Light(i)? Because we’re getting a bunch of error messages when we try different codes to make it work.
function [light] = filter_func( i)
lightI(i) = GetLight(SENSOR_3);
if i==1
light(i)=lightI(i)
elseif i==2
light(i) = 0.55*lightI(i) + 0.45*lightI(i-1)
else
light(i) = 0.4*lightI(i) + 0.3*lightI(i-1) + 0.3*lightI(i-2);
end
end
i=1
while true
lightI(i) = GetLight(SENSOR_3); % Get’s a lightvalue between 0 and 1024.
if i>2
light =filter_func(i)
light=round(light);
else
light(i) = GetLight(SENSOR_3);;
end
i=1+i
plot(light(end-90:end), 'r-');
title('Lightvalue')
axis([0 100 0 1023]) ;
end
You probably mainly get errors because you are not allowed to mix script and functions like this in MATLAB (like you are in Python).
Your filter function is only used when i>2 so why are you doing the first 2 tests? It seems like you want lightI as a global variable, but that is not what you have done. The lightI inside the function is not the same as the one in the while loop.
Since your while loop runs forever, maybe you don't need to worry about updating the plot the first two times. In that case you can do this:
filter = [0.4 0.3 0.3]';
latest_filtered_light = nan(90,1);
lightI = [];
p = plot(latest_filtered_light, 'r-');
title('Lightvalue')
axis([0 100 0 1023]) ;
while True
lightI(end+1,1) = rand*1024; % Get’s a lightvalue between 0 and 1024.
if i>=3
new_val = lightI(end-2:end,1)'*filter;
latest_filtered_light = [latest_filtered_light(2:end);...
new_val];
set(p, 'ydata', latest_filtered_light)
drawnow
end
end
I think it is an important point to not call plot every time - at least if you are the least concerned about performance.