I am trying to piece together a nested "IIF" statement to produce one of three Values but my line is not working
=IIF(Fields!LeaseNumber.value = "", "Vacant",Fields!tenantName.value) AND (IIF(Fields!tenantName.Value ="",Fields!LeaseDBA.Value, Fields!tenantName.Value))
My expected outcome should be:
If No lease# - "Vacant"
if no tenantName - "LeaseDBA"
all other rows just give me - "tenantname"
All the other questions I reviewed didn't seem to match this type of IIF clause.
Any help really is appreciated.
I've actually recently just tried to expalain how the IIF expression works and how to nest them. https://stackoverflow.com/a/35289515/4579864
To check if a value is null or empty, you're better off using one of these functions:
You can use IsNothing() to check if there is a value. Applied to your example would result to this:
=IIF(IsNothing(Fields!LeaseNumber.value)
,"Vacant"
,IIF(IsNothing(Fields!tenantName.Value), Fields!LeaseDBA.Value, Fields!tenantName.Value))
You can calculate the length of your value with Len() and then check if it is larger than 0.
=IIF(Len(Fields!LeaseNumber.value) > 0
,IIF(Len(Fields!tenantName.Value) > 0, Fields!tenantName.Value, Fields!LeaseDBA.Value)
,"Vacant" )
If No lease# - "Vacant" (If there is no LeaseNumber then show "Vacant" -String only)
If no tenantName - "LeaseDBA" (If there is not tenantName then show Fields!LeaseDBA.Value
And for other rows just give me Fields!tenantName.Value
If this is you want then try below expression.
IIF(Trim(CStr(Fields!LeaseNumber.Value))="","Vacant",CStr(Fields!tenantName.Value)) & IIF(Trim(CStr(Fields!tenantName.Value))="",Fields!LeaseDBA.Value,CStr(Fields!tenantName.Value))
Related
I need some help with an Access expression. I keep getting a syntax error.
I'm trying to get a calculated field from the combination of another calculated field.
Note that the LaserCuttingRate field is also a Lookup field. So it's not a simple entry field. It becomes a selector of the 3 choices.
IIf([LaserCuttingRate]="200",([CuttingTime]*200/60),IIf([LaserCuttingRate]="400",([CuttingTime]*400/60),IIf([LaserCuttingRate]="100",([CuttingTime]*100/60)))
All help appreciated.
Cam
I think you're missing a final close bracket.
There are 6 open brackets ( but only 5 closing brackets )
UPDATE - second issue
Two extra issues
The checks on number either don't need, or shouldn't have double-quotation masks
The final IIF also needs a false clause at the end (in the example below, 0).
Try this - I have put it into an Access database scratch table and it worked. (If relevant, note that I had LaserCuttingRate and CuttingTime set as doubles, as well as the Result Type for the calculated column).
IIf([LaserCuttingRate]=200,([CuttingTime]*200/60),IIf([LaserCuttingRate]=400,([CuttingTime]*400/60),IIf([LaserCuttingRate]=100,([CuttingTime]*100/60),0)))
hope someone more experienced can give me answer/solution to my problem.
In a database I have two columns, one to control do we show data and the data to be shown is in the second column.
I have data and expression as below:
ShowPriceOnMatrix | RSP
-------------------------------
0 | 1.48
1 | 10.26 euro -> This one fails with #Error
1 | 4.59
0 | 7.12
=IIF(Fields!ShowPriceOnMatrix.Value = 1, CDbl(Fields!RSP.Value), "")
This expression sometimes gives me #Error.
The problem is that the RSP field was defined as nvarchar and I cant change it now so I'm trying to convert to number and show as. Some users when input price on the filed they also type a text ex. "12.45 euro" or similar. When the expression hits a value with text the whole IIF fails with an #Error even if the first column is stated not to show the price.
The problem with IIF is that it is not skipping the second parameter if the first one evaluates to false - and therefore produces #Error in my case.
I've tried to use
IsNumeric(Cdbl(Fields!RSP.Value))
and
IsError(Cdbl(Fields!RSP.Value))
but both of them give an #Error also. From SSRS explanation for those functions, they should return a Boolean value but somehow I'm getting #Error :)
Just convert the field to numeric using
VAL(Fields!RSP.Value)
Also, you might want to consider using SWITCH instead of IIF. SWITCH stops at the first expression which evaluates to True, whereas IIF will evaluate all expressions regardless of if they will be used or not.
In your case, with a single IIF, SWITCH is not relevant but in cases where nested IIFs are used or situations where you may get a divide by zero error, SWITCH is often easier to read/debug. So something like this where we have a hypothetical situation that we want to test for divide by zero and return zero or if the thing we are dividing by is negative we want to return -1, otherwise do the calc...
=IIF(Fields!MyDivisor.Value = 0, 0, IIF(Fields!MyDivisor.Value <0, -1, Fields!MyNumber.Value/ Fields!MyDivisor.Value))
would become
=SWITCH (
Fields!MyDivisor.Value =0, 0,
Fields!MyDivisor.Value <0, -1,
True, Fields!MyNumber.Value/ Fields!MyDivisor.Value
)
each expression/result pair is evaluated in order until it hits the first True expression, so if MyDivisor was zero it would return 0 and stop, if it was negative, it would return -1 and stop. The final True acts like an else (as True always returns True ! ) so it would only do the calculation if all other expressions returned false.
You can use this expression to extract your numeric values:
=System.Text.RegularExpressions.Regex.Replace(Fields!RSP.Value, "[^0-9,]", "")
But you have to take care of the . in 1.48. With , and the language DE it worked for me. With . it would be:
=System.Text.RegularExpressions.Regex.Replace(Fields!RSP.Value, "[^0-9.]", "")
I'm New to SSRS.
Im trying to create a report where i need to group by [DATA Flag] column which is working fine ,but once the data is grouped i need to set the DATA FLAG ="TotalCancellations" and there is another column CancellDays which i need to set it as <120 .
I tried
Option 1:-
So to achieve this i have added TWO filters one with
Expression : DATA FLAG
Operator =
and Value as TotalCancellations
and the other filter as follows
Expression : Cancelldays
Operator =
and Value as < 120
But its not working and giving empty result,i have records with Cancelldays <120
Option 2 :-
Right click on Group and in General Tab ,Group on Expression as below ]
Fields!DFlag.Value = "TotalCancellations" AND Fields!DFlag.Value <120
which didnt work :(
this is similar to writing having clause in SQL i believe but im not getting how to implement that here in SSRS.
i can add in SQL Query but its already a huge query with lot of unions so please suggest me if there is any way i can implement here in SSRS
Im using Matrix in SSRS 2008
Adjusting the syntax for your option 1 is probably the easiest solution. In the Group Properties, under the Filters section, enter these two filters:
Expression: [DFlag] (Text)
Operator: =
Value = TotalCancellations
Expression: =Sum(Fields!CancelDays.Value) [enter this in the expression builder] (Integer)
Operator: <
Value: 120
Putting all the filters in a single expression, like your option 2, can be useful if you need to filter by one criteria OR another.
Below are the filer expressions
and this is how group by is
The filter should be implemented at the tablix level, not at the group level.
I have been working on ssrs and tried to do the following statement. I am using data from 2 datasets and there is no way I can merge them into one (I tried that when I first had this error). Code:
=Sum(iif(Fields!Year.Value = Max(Fields!Year.Value) - 1 , Sum(Fields!PersonCount.Value, "RetCust"), 0))
This code gives the error: The Value expression for the text box has a nested aggregate that specifies a dataset scope. Inner aggregates cannot specify a dataset scope.
I have also tried a different variation of this code which is as follows:
=(iif(Fields!Year.Value = Max(Fields!Year.Value) - 1 , Sum(Fields!PersonCount.Value, "RetCust"), 0))
I just got rid of the sum at the beginning. But this time I had a different error: Report item expressions can only refer to fields within the current dataset scope or, if inside an aggregate, the specified dataset scope. Letters in the names of fields must use the correct case
Hope someone can help :)
Cheers
Your expression is a bit wrong.
=Sum(iif(Fields!Year.Value = Max(Fields!Year.Value) - 1 , Sum(Fields!PersonCount.Value, "RetCust"), 0))
Your Sum is outside and is covering the two fields which is not on the same dataset. You should assign Sum for each of them since they are from different dataset.
Try this:
=iif(Sum(Fields!Year.Value) = Max(Sum(Fields!Year.Value)) - 1 , Sum(Fields!PersonCount.Value, "RetCust"), 0)
Note: I haven't tested the Max(Sum(.... if that's correct.
Currently I am trying to average a set of numbers, but with certain conditions.
Is it possible to use an iif() within an avg() and return the correct result?
Furthermore, as of now my computations return a decimal returned to a power (8.9267....E -05).
I tried circumventing the AVG function by conditionally summing and then dividing by a conditional count but it gives me the same results.
Can someone explain why this is returned and offer help?
Currently I have:
=avg(iif((This_case) AND (That_case) AND (This_conditional)
, Fields!ResponseRate.Value
, 0))
Essentially I want the average ResponseRate if certain conditions are met.
The sum function works fine for the conditions but the average doesn't.
You can definitely use IIf within Avg and get the correct results.
Do you want to exclude the False values from the calculation entirely?
In your example you're still including them, just setting them to 0 and hence still including them in the calculation. This might explain your unexpected results.
If you want to exclude them entirely use Nothing instead of 0.
Edit after comment
You can nest an expression in another IIf statement and check for NULL values using IsNothing.
Say your condition average expression is:
=Avg(IIf(Fields!ID.Value > 5, Fields!value.Value, Nothing))
You can return 0 for NULL values with something like:
=IIf(IsNothing(Avg(IIf(Fields!ID.Value > 5, Fields!value.Value, Nothing)))
, 0.0
, Avg(IIf(Fields!ID.Value > 5, Fields!value.Value, Nothing)))
I'd like to add my two cents to this one, a little late, but also valuable. I was able to use the above code to Average out a data set if another record appears X number of times.
=AVG(IIF(Count(Fields!AcctNo.Value, "AcctNo1") = 2, Fields!Limit.Value, Nothing))
So, if the acctno field appears 1 time, avg the limit field for that row group. Ian's example was very helpful