Sum the expression - reporting-services

I need to sum this expression
= IIf(Fields!SupplyStrategyTypeName.Value="Supply",0,
IIf(Fields!IP_DA_FIN.Value>225,Fields!IP_DA_FIN.Value-225,0))
I tried following the example of the previous question but I get an error.
Any help on this?

You didn't list what you tried or the error you got so here's what should work:
=SUM(IIf(Fields!SupplyStrategyTypeName.Value = "Supply", 0,
IIf(Fields!IP_DA_FIN.Value > 225, Fields!IP_DA_FIN.Value - 225, 0)) )
So if a row of data's SupplyStrategyTypeName is Supply or IP_DA_FIN is 225 or less, it would result in 0 otherwise it would be IP_DA_FIN - 225. Then the sum will sum all the results.
Don't be worried about using too many spaces - they make it more readable when you go back through your formulas to figure out what's wrong.

Related

SSRS - IIF error in Report Builder

I am having an issue with a divide by zero error that I have half way worked through.
Basically I need (EstimatedValuePlanned - EAC) / EstimatedValuePlanned
I have the following, however I am still getting #Error on some:
= IIF (Fields!EstimatedValuePlanned.Value = 0 , 0,
Fields!EstimatedValuePlanned.Value - Fields!EAC.Value)
/
SUM(Fields!EstimatedValuePlanned.Value)
I have changed the code around several times but I still either get Error or NaN
Thank you
IIF will always evaluate both parts of the function, so when SUM(Fields!EstimatedValuePlanned.Value) is zero you will get the error even though that's not what will be returned.
Try using a SWITCH statement. SWITCH stops once an expression returns True.
Something like this
= SWITCH(
SUM(Fields!EstimatedValuePlanned.Value) = 0 , 0,
True, (Fields!EstimatedValuePlanned.Value - Fields!EAC.Value) / SUM(Fields!EstimatedValuePlanned.Value)
)
The True expression simply acts like an else
UPDATE WITH SAMPLE
I created a new report added a dataset and set the dataset query to be the following (just to generate some dummy data).
DECLARE #t TABLE (EVP float, EAC float)
INSERT INTO #t VALUES
(1,2),
(2,3),
(5,4),
(0,2),
(1,0),
(0,0)
SELECT * FROM #t
I then added a table, set the first to columns to be EVP and EAC respectively and set the 3rd column to an expression as follows.
=SWITCH (
Fields!EVP.Value = 0, 0
, True, (Fields!EVP.Value - Fields!EAC.Value) / Fields!EVP.Value
)
The report design looks like this.
And when it's run this is the result....
Try replicating the above steps and see if you can get it to work like this first then review difference to your report.
I got it to work with:
=IIF (Fields!EstimatedValuePlanned.Value = 0 , 0,
Fields!EstimatedValuePlanned.Value - Fields!EAC.Value)
/
IIF(Fields!EstimatedValuePlanned.Value =
0,1,Fields!EstimatedValuePlanned.Value)
Thank you both

Multiple Or in an IIF function

Given this data: Complete Dataset
I only want to get the Total_AR of those TransactionYrMonth=ParameterPeriod so I produced it using this code:
=IIF(
IsNothing(Sum(Fields!Total_AR.Value)),0,
Sum(IIF(Cdate(Fields!TransactionYrMnth.Value)=Cdate(Parameters!Period.Value),
Fields!Total_AR.Value,0)
)
)
From there, I got this dataset:
Filtered Dataset
Now, what I need to do is to get the sum of the first 3 service months starting from January to March that is equal to the latest transaction month which is April.
So the sum should be equal to $204,329 + -$96,640 + -$259,008 = -$151,319
To make it possible, I tried to use a code like this:
=Sum(IIF(
Cdate(Fields!TransactionYrMnth.Value)=Cdate(Parameters!Period.Value) And (
Cdate(Fields!ServiceYrMnth.Value)=DateAdd(DateInterval.Month, -3, CDate(Parameters!Period.Value)) Or
Cdate(Fields!ServiceYrMnth.Value)=DateAdd(DateInterval.Month, -2, CDate(Parameters!Period.Value)) Or
Cdate(Fields!ServiceYrMnth.Value)=DateAdd(DateInterval.Month, -1, CDate(Parameters!Period.Value))), Fields!Total_AR.Value,0
))
I have no luck producing it. I even got an error saying
‘Textbox11.Paragraphs[0].TextRuns[0]’ contains an error: [BC30516] Overload resolution failed because no accessible 'IIf' accepts this number of arguments.
Anyone, please help?
I got a solution already using this code:
=Sum(IIF(
Cdate(Fields!TransactionYrMnth.Value)=Cdate(Parameters!Period.Value) And
CDate(Fields!ServiceYrMnth.Value) >= DateAdd(DateInterval.Month, -3, CDate(Parameters!Period.Value)) And
CDate(Fields!ServiceYrMnth.Value) <= DateAdd(DateInterval.Month, -1, CDate(Parameters!Period.Value)), Fields!Total_AR.Value, 0
)
)

SSRS Expression for color error

I've got this expression for my percent column to work out which colour to use based on several column values in my SSRS report.
=IIF(Fields!GrossMarginActual.Value = 0 AND Fields!GrossMarginPercentageActual.Value = 0, 0, Fields!VarianceGrossMargin.Value) AND
IIF(Round(Fields!VarianceGrossMargin.Value,2) < 0.00, "Red", "Black")
However I get the following
The color expression for the text run 'VarianceGrossMarginPercent.Paragraphs[0].TextRuns[0]' contains an error: [BC30205] End of Statement Expected.
I am guessing my syntax on my expression isn't quite right. I've wrapped extra brackets round it but that just gives me back errors. This correct syntax doesn't flag up any errors in the expression box.
Would appreciate a second pair of eyes to look over it.
Try this:
=IIF((Fields!GrossMarginActual.Value = 0 AND Fields!GrossMarginPercentageActual.Value = 0, "Red",
IIF(Round(Fields!VarianceGrossMargin.Value,2) < 0.00, "Red", "Black")))

How to Sum Visible Rows Only SSRS

I am trying to sum only the visible rows only in SSRS and I have read a few forums that say you need to put:
=Sum( iif( <The condition of Visibility.Hidden expression>, 0, Fields!A.Value))
However I am unsure on how this works, my visibility code is:
=iif(Fields!Sub.Value = "Ins", True, IIF(Fields!Sub.Value = "Ins - Int", True, False))
When I add this into the above 1st code i get #ERROR.
Appreciate any help.
Thanks
Try this:
=Sum(iif(
Fields!Sub.Value = "Ins",
0,
IIF(Fields!Sub.Value = "Ins - Int", 0, Fields!A.Value)
))
Note I am not using the same expression for hiding the rows, I am just using its logic.
Hopefully this is what you are looking for, let me know if you need further help.

How do I write a function that takes the average of a list of numbers

I want to avoid importing different modules as that is mostly what I have found while looking online. I am stuck with this bit of code and I don't really know how to fix it or improve on it. Here's what I've got so far.
def avg(lst):
'''lst is a list that contains lists of numbers; the
function prints, one per line, the average of each list'''
for i[0:-1] in lst:
return (sum(i[0:-1]))//len(i)
Again, I'm quite new and this for loops jargon is quite confusing to me, so if someone could help me get it so the output of, say, a list of grades would be different lines containing the averages. So if for lst I inserted grades = [[95,92,86,87], [66,54], [89,72,100], [33,0,0]], it would have 4 lines that all had the averages of those sublists. I also am to assume in the function that the sublists could have any amount of grades, but I can assume that the lists have non-zero values.
Edit1: # jramirez, could you explain what that is doing differently than mine possible? I don't doubt that it is better or that it will work but I still don't really understand how to recreate this myself... regardless, thank you.
I think this is what you want:
def grade_average(grades):
for grade in grades:
avg = 0
for num in grade:
avg += num
avg = avg / len(grade)
print ("Average for " + str(grade) + " is = " + str(avg))
if __name__ == '__main__':
grades = [[95,92,86,87],[66,54],[89,72,100],[33,0,0]]
grade_average(grades)
Result:
Average for [95, 92, 86, 87] is = 90.0
Average for [66, 54] is = 60.0
Average for [89, 72, 100] is = 87.0
Average for [33, 0, 0] is = 11.0
Problems with your code: the extraneous indexing of i; the use of // to truncate he averate (use round if you want to round it); and the use of return in the loop, so it would stop after the first average. Your docstring says 'print' but you return instead. This is actually a good thing. Functions should not print the result they calculate, as that make the answer inaccessible to further calculation. Here is how I would write this, as a generator function.
def averages(gradelists):
'''Yield average for each gradelist.'''
for glist in gradelists:
yield sum(glist) /len(glist)
print(list(averages(
[[95,92,86,87], [66,54], [89,72,100], [33,0,0]])))
[90.0, 60.0, 87.0, 11.0]
To return a list, change the body of the function to (beginner version)
ret = []
for glist in gradelists:
ret.append(sum(glist) /len(glist))
return ret
or (more advanced, using list comprehension)
return [sum(glist) /len(glist) for glist in gradelists]
However, I really recommend learning about iterators, generators, and generator functions (defined with yield).