Sum results of expression containing CountDistinct in Report Builder - reporting-services

I have a report that requires counting the number of days an event occurred. I have created a Calculated Field that weeds out the dates:
=IIF(Fields!ID_ACW.Value > 0, Fields!CallDate.Value, Nothing)
By placing this in a CountDistinct expression, each row shows the correct number of distinct days this event occurred.
However, I need to add these results together for each row group's total. I have tried using
=Sum(CountDistinct(=IIF(Fields!ID_ACW.Value > 0, Fields!CallDate.Value, Nothing)))
and
=RunningValue(CountDistinct(=IIF(Fields!ID_ACW.Value > 0, Fields!CallDate.Value, Nothing)),Sum, "DataSet1")
, but neither gives me the correct total. Is there something I have missed or is it possible to add an expression to total each row's value?

I was able to get this to work by adding the row-group's value to the date in the Calculated Field like so:
=IIF(Fields!ID_ACW.Value > 0, Fields!CallDate.Value & " " & Fields!CallHour.Value, Nothing)
Now I can use
'''
CountDistinct(ACWDay)
'''
And get correct totals across all row-groups.
Is there a cleaner method to do this?

This is from memory so it might not be 100% but you should get the idea...
=Sum(
CountDistinct(=IIF(Fields!ID_ACW.Value > 0, Fields!CallDate.Value, Nothing), "YourRowGroupName")
)
Basically you need to do your CountDistinct within each rowgroup and then sum the results.

Related

SSRS IIF Returning #ERROR

I have an expression in my SSRS report that calculates a field Total across multiple groups:
=IIf(SUM(Fields!sales_Dollars.Value) > 0 ,(SUM(Fields!Total_Defective__.Value)/IIF(SUM(Fields!sales_Dollars.Value) <= 0, 1, SUM(Fields!sales_Dollars.Value))),0)
I had to alter it by adding groups within the statement:
=SUM(IIf(SUM(Fields!sales_Dollars.Value,"Item_Process_Group") > 0 ,(SUM(Fields!Total_Defective__.Value,"Item_Process_Group")/IIF(SUM(Fields!sales_Dollars.Value,"Item_Process_Group") <= 0, 1, SUM(Fields!sales_Dollars.Value,"Item_Process_Group"))),0))
The problem is that there is a 0 in the denominator of one of the rows in the calc that is not in the group specified in the clause. This causes an #error. (at least I believe this is the cause)
is there a way to slip in a check for zero in Fields!sales_Dollars.Value with no group? sort of like how it does it in the first code example.
I need the group names in the bottom portion to pull the correct values.
The first thing I like to do when I look at these is simplify them, in this case there is a redundancy to be removed. There is no way that
SUM(Fields!sales_Dollars.Value) > 0
and
SUM(Fields!sales_Dollars.Value) <= 0
Could be true at the same time, which renders your second Iif statement always evaluating to false if the first one is true, if the first one is false the second Iif is skipped and a value of 0 is returned. So, we can set it to the false value drop the second Iif to end up with:
=IIf(
SUM(Fields!sales_Dollars.Value) > 0
,(SUM(Fields!Total_Defective__.Value)/SUM(Fields!sales_Dollars.Value))
,0)
Now the question is do you want to do something different if sales dollar value is zero. If that's the case we replace the zero with what the equation would have been if the second Iif statement were ever evaluated:
=IIf(
SUM(Fields!sales_Dollars.Value) > 0
,(SUM(Fields!Total_Defective__.Value)/SUM(Fields!sales_Dollars.Value))
,SUM(Fields!Total_Defective__.Value))
Hopefully that fixes the syntax problem you were experiencing, with the first statement.

Calculated field in SSRS

I have tried to create a calculated field that will show the number of customer transactions processed within 15 minutes.
I have added the expression:
=count(fields!wait.Value<15)
However, when I run the query I'm getting the error message : 'expression used for the calculated field includes an aggregate RowNumber...'
Can you advise please on how to create a calculated field so I can capture the value I want?
I have tried = SUM(IIF(Fields!wait.Value < 15 , 1, 0)) to no avail.
With many thanks.
Calculated fields added to datasets can't have aggregate functions. The calculated field is essentially adding an extra column to your dataset. It sounds like you may want a variable? Used elsewhere in the report, your second expression would work, or the similar
=Count(IIf(Fields!wait.Value<15, 1, Nothing))
would work too.

SSRS Expression how to compare value from two different datasets?

So I've searched for this specifically but cannot find an example or answer anywhere.
I'm trying to compare a value of one dataset to the value of another dataset but it's not working.
Here's my expression:
=IIF((Fields!IC_ADV.Value + Fields!OC_ADV.Value) > 0, Max(Fields!AMOUNT.Value, "Data_bucket_info") > 15, nothing)
All I get is "True". If I remove the '> 15' then I get a value but it's not the right value. I've tried replacing Max with First, Min, Sum; without the '>15' I again get a value but not the right one and with the '>15' I get "True" or "False".
If I have Max in, I'm expecting to get the Max value for AMOUNT for all amount > 15; if I have Min in, I'm expecting to get the Min value for AMOUNT for all amount > 15.
So for example:
AMOUNT is in increments of 5; so 5, 10, 15, 20, 25....etc.
If (Fields!IC_ADV.Value + Fields!OC_ADV.Value) = 24 then what I want is for AMOUNT to display 25. If (Fields!IC_ADV.Value + Fields!OC_ADV.Value) = 3 then I want AMOUNT to display 5.
The warning I keep getting in SSRS is 'The Value expression for the textrun 'Textbox12.Paragraphs[0].TextRuns[0]' uses a numeric aggregate function on data that is not numeric. Numeric aggregate functions (Sum, Avg, StDev, Var, StDevP, and VarP) can only aggregate numeric data.'
But the AMOUNT field is a numeric value.
It is not lot clear from your description what you are trying to achieve. But for what I understand you want to match the sum of IC_ADV and OC_ADV with some value and depending on that you want to get the value. for that you can set the expression as below,
=IIF((Fields!IC_ADV.Value + Fields!OC_ADV.Value) = 24,Max(Fields!AMOUNT.Value, "Data_bucket_info"),IIF((Fields!IC_ADV.Value + Fields!OC_ADV.Value) = 3,Min(Fields!AMOUNT.Value, "Data_bucket_info"),Nothing)
But if you want to match match value of one dataset to another datasets value based on some common field then you should take a look at the LookUp. Here is more info.
In this scenario, I think it's better to make it in Math way. Try the expression below:
=((Fields!IC_ADV.Value + Fields!OC_ADV.Value)\5+1)*5

Report builder 3.0 count with where clause

For one of our reports I'm trying to get a count of rows that have the column Canceled with value 1. In the past I used the solution I found on Stackoverflow to use the Sum function with IIF, i.e.
=Sum(iif(Fields!Canceled.Value="True", 1, 0))
But now my source data has multiple rows for one booking_id so I need to add a distinct on that column. In SQL I could easily do
SELECT COUNT(DISTINCT(booking_id)) FROM Booking WHERE Canceled=1
But I can't figure out how to get the same behaviour in Report Builder 3.0. Any ideas?
Doing this in T-SQL, if possible, as suggested in the comments is not a bad idea, but you can do this in the report with an expression like:
=CountDistinct(IIf(Fields!Canceled.Value = 1, Fields!booking_id.Value, Nothing))
CountDistinct will ignore Nothing (i.e. Null) records, so you can apply a CountDistinct to an IIf expression that returns booking_id or Nothing based on Canceled.

SSRS Avg function returns result that is different than expected

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