How to fix #Error when dividing by 0 in SSRS - tsql
So I am aware this has been answered before,
here:
SSRS 2008 - Dealing with division by zero scenarios
and here:
https://sqldusty.com/2011/08/01/ssrs-expression-iif-statement-divide-by-zero-error/
and I'm sure these work for what I need. However, I'm struggling where to put the different parts of the Syntax as
A) I'm not very good at this but I am trying my best!
and
B) The code is awkwardly long
This is my code which produced #Error
=SUM(Fields!OperationalCharge.Value + Lookup(Fields!InvoiceDate.Value,Fields!InvoiceDate.Value, Fields!OperationalCharge.Value, "WaterBillingData") - Fields!OperationalChargePreviousMonth.Value + Lookup(Fields!InvoiceDate.Value,Fields!InvoiceDate.Value, Fields!OperationalChargePreviousMonth.Value, "WaterBillingData"))
/ SUM(Fields!OperationalChargePreviousMonth.Value + Lookup(Fields!InvoiceDate.Value,Fields!InvoiceDate.Value, Fields!OperationalChargePreviousMonth.Value, "WaterBillingData")) * 100
And this is what I have tried:
=Iif(SUM(Fields!OperationalCharge.Value + Lookup(Fields!InvoiceDate.Value,Fields!InvoiceDate.Value, Fields!OperationalCharge.Value, "WaterBillingData") - Fields!OperationalChargePreviousMonth.Value + Lookup(Fields!InvoiceDate.Value,Fields!InvoiceDate.Value, Fields!OperationalChargePreviousMonth.Value, "WaterBillingData")) = 0, 0,
SUM(Fields!OperationalCharge.Value + Lookup(Fields!InvoiceDate.Value,Fields!InvoiceDate.Value, Fields!OperationalCharge.Value, "WaterBillingData") - Fields!OperationalChargePreviousMonth.Value + Lookup(Fields!InvoiceDate.Value,Fields!InvoiceDate.Value, Fields!OperationalChargePreviousMonth.Value, "WaterBillingData")) / SUM(Fields!OperationalChargePreviousMonth.Value + Lookup(Fields!InvoiceDate.Value,Fields!InvoiceDate.Value, Fields!OperationalChargePreviousMonth.Value, "WaterBillingData")) * 100 ) = 0, 1,
Iif(SUM(Fields!OperationalCharge.Value + Lookup(Fields!InvoiceDate.Value,Fields!InvoiceDate.Value, Fields!OperationalCharge.Value, "WaterBillingData") - Fields!OperationalChargePreviousMonth.Value + Lookup(Fields!InvoiceDate.Value,Fields!InvoiceDate.Value, Fields!OperationalChargePreviousMonth.Value, "WaterBillingData"))
However, this then gives me the error:
The Value expression for the Textrun 'Col.name.Paragraphs[0].TextRuns[0]' Contains an error: [BC30205] End of statement expected
I'm half thinking I've actually got this right, however as I've changed it, it doesn't like something in the update from other things I've read while trying to resolve this.
Ideally I want any 0/0 to show N/a or -. I'm aware the above code I tried doesn't do this, but when I tried to how this as well, I also came across the same error
Any help would be gratefully received!
Cheers,
Wil
Your issue may be in this IIF statement.
Iif(SUM(Fields!OperationalCharge.Value + Lookup(Fields!InvoiceDate.Value,Fields!InvoiceDate.Value, Fields!OperationalCharge.Value, "WaterBillingData") - Fields!OperationalChargePreviousMonth.Value + Lookup(Fields!InvoiceDate.Value,Fields!InvoiceDate.Value, Fields!OperationalChargePreviousMonth.Value, "WaterBillingData"))
It appears you have the Boolean test but not the True or False parts of the statement. It states IIF theSUM. Not what it equals and nothing for the True or False parts.
First thing I'll mention before I even looked at the expression - you need to be trying to calculate some of this stuff at the query level. If you can join these tables instead of using all these lookups, you will save yourself loads of time and effort dealing with this sort of issue. If that is not a possible solution, there is definitely something missing in the expression.
The first conditional SUM looks okay from what I can tell, as does the numerator from the false condition. The denominator is where your issues arise. It looks to me like you simply misplaced the IIF. On top of that, you have a misplaced parenthesis. You'll need to move the IIF to right after the division sign, remove the parenthesis from after * 100 and move it to the end of the expression. Basically, the end of your expression should look like the following.
/ IIF(SUM(Fields!OperationalChargePreviousMonth.Value
+ Lookup(Fields!InvoiceDate.Value,Fields!InvoiceDate.Value, Fields!OperationalChargePreviousMonth.Value, "WaterBillingData")) * 100 = 0,
1,
SUM(Fields!OperationalCharge.Value
+ Lookup(Fields!InvoiceDate.Value,Fields!InvoiceDate.Value, Fields!OperationalCharge.Value, "WaterBillingData")
- Fields!OperationalChargePreviousMonth.Value
+ Lookup(Fields!InvoiceDate.Value,Fields!InvoiceDate.Value, Fields!OperationalChargePreviousMonth.Value, "WaterBillingData")))
FYI, a program like Notepad++ can be very useful in diagnosing these sorts of issues. I used it to format the expression and use it's useful syntax highlighting to match parethesis to its terminating partner.
Related
gtsummary's tbl_regression() returning odd result
I'm using gtsummary::tbl_regression to format the output of the following code: dat.mi <- import(here("temp", "adTurn_mi.rds")) fit1 <- glm(quit ~ sex + age + race + education + experience + tenure_sr + salary + anymc + catcap + medi + nonpro + rural, family = 'binomial', weights = svy_weight, data = complete(dat.mi) ) |> tbl_regression(exponentiate = TRUE) dat.mi is MICE data (unfortunately I can't provide a truly reproducible example as the data is confidential), but is completed in the call to the glm() function. tbl_regression formats everything beautifully, with the exception of one line, and I cannot figure out what that one line is doing or how to fix it. Instead of returning an exponentiated coeficient, it returns a string of numbers separated by commas (please see the Masters degree line in the attached image). Is this a known issue with a similarly known and easy fix? Or am I just really good at breaking stuff?
limit random complex number to a given range
I can get the real part of a random number to stay withing a given range but the complex part of the number doesn't stay within the range I set. see matlab / octave code below. xmin=-.5 xmax=1 n=3 x=xmin+rand(1,n)*(xmax-xmin)+(rand(1,n)-(xmax-xmin))*1i x=x(:) The real part works but the complex part isn't limited to -0.5 to 1 0.2419028288441536 - 0.6579427654754871i 0.2712527227134944 - 1.451964497492678i 0.3245051849394858 - 1.107556052779179i
You have two mistakes: x=xmin+rand(1,n)*(xmax-xmin)+(xmin + rand(1,n)*(xmax-xmin))*1i You should add xmin to the sum and change - to * in the second part.
I've added some spaces to your code so the difference more obvious: x = xmin+rand(1,n)*(xmax-xmin) + ( rand(1,n)-(xmax-xmin) )*1i ^^^ correct ^^^ not correct: missing `xmin+` (and as OmG noted, also a `-` instead of a `*`) One good way to reduce the number of bugs is by avoiding code duplication. You could for example write: rand_sequence = #(m,xmin,xmax) xmin+rand(1,n)*(xmax-xmin); x = rand_sequence(n,xmin,xmax) + 1i*rand_sequence(n,xmin,xmax) (This looks like more code, but the more complicated code logic is not duplicated.) Or like this: x = xmin + (rand(1,n)+1i*rand(1,n)) * (xmax-xmin);
Avoid rounding to 0 when a result is very little
I've searched for this but i didn't find anything, i hope this is not a doubled question. I'm doing a formula in TSQL like this: #Temp = SQRT((((#Base1 - 1) * (#StDev1 * #StDev1)) + ((#AvgBase - 1) * (#AvgStDev * #AvgStDev))) * ((1 / #Base1) + (1 / #AvgBase)) / (#Base1 + #AvgBase - 2)) But it always returns me a 0. #Base1 and #AvgBase are int, the rest of parameters are float, but i've also tried with decimal(15,15). I tried also changing the self multiplication with the function POWER() but the only problem i can't solve is this part: (1 / #Base1) + (1 / #AvgBase), because #Base1 and #AvgBase are so big, and the result of the calc is 0,0001... and some more numbers. How can i force the engine to not round the result to 0? Thanks EDIT: I solved it changing the #AvgBase and the #Base1 to float type. I guess that the result 1/#param, with #param -> int gives you the rounded result and when you go for casting it or whatever, you are working on a rounded result anyway.
have you tried to create a #INVBase1 = (1/#Base1) ? will this also be rounded to 0? what happens when you play around with the data format of this new variable? alternatively have you tried / ((#Base1) + (#AvgBase)) instead of * ((1 / #Base1) + (1 / #AvgBase))
Is there a function similar to Math.Max for Entity Framework?
I have an entity framework query as follows; From T In Db.MyTable Where (T.Col1 - T.Col2) + T.Col3 - T.Col4 > 0 _ Select T I now need to make sure that the bracketed part '(T.Col1 - T.Col2)' does not go below zero. In .Net, I'd code it as follows (but obviously EF does not like Math.Max). From T In Db.MyTable Where Math.Max(T.Col1 - T.Col2,0) + T.Col3 - T.Col4 > 0 _ Select T Is there an easy way to do this? I am using EF 2.0 (not the latest, just released version). Thanks in advance
Max isn't supported, but Abs is; will that do? Otherwise you'll have to use a ternary expression. In C#, I'd do: from t in Db.MyTable let m = t.Col1 >= t.Col2 ? t.Col1 - t.Col2 : 0 where m + t.Col3 - t.Col4 > 0 However, this will be inefficient at the DB level unless you have an expression index. So I'd suggest a computed column instead.
Pythonic way to write a for loop that doesn't use the loop index [duplicate]
This question already has answers here: Is it possible to implement a Python for range loop without an iterator variable? (15 answers) Closed 7 months ago. This is to do with the following code, which uses a for loop to generate a series of random offsets for use elsewhere in the program. The index of this for loop is unused, and this is resulting in the 'offending' code being highlighted as a warning by Eclipse / PyDev def RandomSample(count): pattern = [] for i in range(count): pattern.append( (random() - 0.5, random() - 0.5) ) return pattern So I either need a better way to write this loop that doesn't need a loop index, or a way to tell PyDev to ignore this particular instance of an unused variable. Does anyone have any suggestions?
Just for reference for ignoring variables in PyDev By default pydev will ignore following variables ['_', 'empty', 'unused', 'dummy'] You can add more by passing supression parameters -E, --unusednames ignore unused locals/arguments if name is one of these values Ref: http://eclipse-pydev.sourcearchive.com/documentation/1.0.3/PyCheckerLauncher_8java-source.html
How about itertools.repeat: import itertools count = 5 def make_pat(): return (random() - 0.5, random() - 0.5) list(x() for x in itertools.repeat(make_pat, count)) Sample output: [(-0.056940506273799985, 0.27886450895662607), (-0.48772848046066863, 0.24359038079935535), (0.1523758626306998, 0.34423337290256517), (-0.018504578280469697, 0.33002406492294756), (0.052096928160727196, -0.49089780124549254)]
randomSample = [(random() - 0.5, random() - 0.5) for _ in range(count)] Sample output, for count=10 and assuming that you mean the Standard Library random() function: [(-0.07, -0.40), (0.39, 0.18), (0.13, 0.29), (-0.11, -0.15),\ (-0.49, 0.42), (-0.20, 0.21), (-0.44, 0.36), (0.22, -0.08),\ (0.21, 0.31), (0.33, 0.02)] If you really need to make it a function, then you can abbreviate by using a lambda: f = lambda count: [(random() - 0.5, random() - 0.5) for _ in range(count)] This way you can call it like: >>> f(1) f(1) [(0.03, -0.09)] >>> f(2) f(2) [(-0.13, 0.38), (0.10, -0.04)] >>> f(5) f(5) [(-0.38, -0.14), (0.31, -0.16), (-0.34, -0.46), (-0.45, 0.28), (-0.01, -0.18)] >>> f(10) f(10) [(0.01, -0.24), (0.39, -0.11), (-0.06, 0.09), (0.42, -0.26), (0.24, -0.44) , (-0.29, -0.30), (-0.27, 0.45), (0.10, -0.41), (0.36, -0.07), (0.00, -0.42)] >>> you get the idea...
Late to the party, but here's a potential idea: def RandomSample(count): f = lambda: random() - 0.5 r = range if count < 100 else xrange # or some other number return [(f(), f()) for _ in r(count)] Strictly speaking, this is more or less the same as the other answers, but it does two things that look kind of nice to me. First, it removes that duplicate code you have from writing random() - 0.5 twice by putting that into a lambda. Second, for a certain size range, it chooses to use xrange() instead of range() so as not to unnecessarily generate a giant list of numbers you're going to throw away. You may want to adjust the exact number, because I haven't played with it at all, I just thought it might be a potential efficiency concern.
There should be a way to suppress code analysis errors in PyDev, like this: http://pydev.org/manual_adv_assistants.html Also, PyDev will ignore unused variables that begin with an underscore, as shown here: http://pydev.org/manual_adv_code_analysis.html
Try this: while count > 0: pattern.append((random() - 0.5, random() - 0.5)) count -= 1
import itertools, random def RandomSample2D(npoints, get_random=lambda: random.uniform(-.5, .5)): return ((r(), r()) for r in itertools.repeat(get_random, npoints)) uses random.uniform() explicitly returns an iterator instead of list