Conditional Aggregate Lookup - SSRS - reporting-services

I have a report with two datasets to summarise the number and value of incomplete orders by status. I have a "Back Order" column, which is using the 'Lookup' function to refer to a second database, based on a whether the Fields!IsBackorder.Value returns true. This works at line level, but I've run into issues at the aggregate level.
For the total count of orders, this forumula works:
=SUM(IIF(LOOKUP(Fields!SalesOrderID.Value, Fields!SalesOrderID.Value, Fields!IsBackorder.Value, "DstBackorders") = "TRUE",1,0))
However, for the total value of orders ("Fields!NetValue.Value"), this returns '#Error'
=SUM(IIF(LOOKUP(Fields!SalesOrderID.Value, Fields!SalesOrderID.Value, Fields!IsBackorder.Value, "DstBackorders") = "TRUE",Fields!NetValue.Value,0))
I've tried custom aggregate functions but I haven't found any that work. I'm not sure how I'm getting this error.
Any suggestions would be really helpful.
Thanks,
Report Screenshot

The syntax looks perfectly fine , also the lookup looks good , can you please check on the below things in your DataSet:
Is Fields!NetValue.Value in scope of the current DataSet.
Are we using the correct data type for Fields!NetValue.Value(Something which is aggregatable , like int , decimal etc.)

Related

Trouble creating nested SUM IIF expression in SSRS

I am new to SSRS and have a SUM(IIF question.
My data set contains four columns: Date, GroupID, PlanPaid, and NetworkIndicator.
Here is an example of the data set:
I am trying to SUM the [PlanPaid] amount when [NetworkIndicator] = "In Network".
However, I need this amount broken up by the [Date]. I tried accomplishing this by creating the expression:
=Sum(IIf(Fields!NetworkIndicator.Value = "In Network"
, Fields!PlanPaid.Value
, Nothing)
, "Claims_Rolling12")
But this expression returns the same amount (total) across all [Dates]. How do I break it up so that it is grouped by the correct [Date]?
Here is a photo of my Tablix and my current Groups: [Tablix and Groups]
And here is a photo of the output: [Output]
You haven't said where you want this sum to appear, so the answer here might not work. If it doesn't then edit your question to show what you expect the output to look like based on your sample data.
I'm assuming here that you want to add a new column to the report that shows "In Network total" by date.
The easiest way to do this is to add a row group that groups by date, then within this group you can use a simple expression, like the one you tried, but without specifying the scope.
=SUM(IIF(Fields!NetworkIndicator.Value = "In Network", Fields!PaidPlan.Value, Nothing))
This expression will only sum rows that are within the current scope, in this case the scope will be the row group you created to group by dates.
As IO said, if this is not helpful, edit your question and show what you expect your end result to look like, based on the sample data you supplied and then I can look at it again.

SSRS standalone formulas

I have been working on this for days without being able to solve yet. It's probably simple if you know what you're doing. I'm simply trying to make a standalone formula that is not in a tablix or anything, it's just in a textbox.
Here is an example of my Dataset called Dataset1:
What I am trying to get is a sum of the Actual Cost when the Category is Labor from Dataset1. My current expression is:
=Sum(iif(Fields!Category.Value="Labor", Fields!ActualCost.Value, 0), "Dataset1")
I refer to Dataset1 as my scope because otherwise, I get an error about using an aggregate expression without a scope.
The report runs but shows #Error in the textbox that has my expression in it. When I replace Fields!ActualCost.Value with a 1, I get the answer, 5, which is the correct number of rows with Labor as the Category. But it won't let me sum the numbers in the ActualCost column where Category is Labor.
Any ideas as to what I'm doing wrong? I feel like it's something to do with aggregating, but I'm not sure. Thanks!
It may have to do with the datatype of fields!ActualCost.Value. If that field is a decimal (regardless of how you have it formatted), try using cdec(0) instead of just 0 in your expression.

Multiple datasets Count with IIF in SSRS

I am trying to write an expression in SSRS which counts only specific data using IIF. I found the following solution:
=Sum(IIF(Fields!Program.Value = "FC", Fields!QuantityToShip.Value, 0))
The code above works but only when there is ONE dataset, while I have several.
Here is the code I wrote:
=Count(IIF(Fields!Mgroup.Value,"DataSet1"=303,1,0))
I get the aggregation error:
Textbox refers directly to the field ‘Mgroup’ without specifying a dataset aggregate
I added a sum:
=Count(IIF(Sum(Fields!Mgroup.Value,"DataSet1")=303,1,0))
Still getting the same error.
Why is that?
What can I put instead of Sum? All I need is to count how many groups named 303 I have.
The expression you used has some sintax errors. The Count function only aggregate from the scoped Dataset.
Try this:
=LookupSet(303,Fields!Mgroup.Value,Fields!Mgroup.Value,"DataSet1").Length
Let me know if this helps you.

SSRS Expression with parameter in WHERE clause

In SSRS, I'm trying to calculate the average number of months a client used a program. The programID is the parameter for the whole report. I'm trying to achieve this (not written with real syntax):
=Avg(Fields!length_of_stay.Value, 0))/30.0 WHERE programid = #ProgramID
Using this question, I came up the the following code which is producing an incorrect answer. I tested in SSMS to get the actual values to compare to SSRS results.
=Avg(IIF(Fields!programid.Value = Parameters!ProgramID.Value, Fields!Length_of_Stay.Value, 0))/30.0
The "/30" is used since the value is in days and I need months. I think the issue is using the parameter value chosen; this is my first report trying to calculate expressions with parameters.
Avg returns the average of all non-null numeric values. 0 is not null so it gets included in the average, distorting your result for every row with a different PragramId. Try using Nothing instead:
=Avg(IIF(Fields!programid.Value = Parameters!ProgramID.Value, Fields!Length_of_Stay.Value, Nothing))/30.0

Datediff & Group By not working?

I'm trying to build a query (QueryB) for it to be referenced in my MS Access control. I know I got the source expression syntax right, I have a very similar working control with QueryA.
I only changed the field and query names. However I keep getting the infamous #Name? error with QueryB. The difference between QueryA and QueryB is the SQL code. QueryA has a GROUP BY and SUM() and QueryB only has DATEDIFF(). I have tried adding the GROUP BY to QueryB, but kept getting [...execute query does not include the specified expression as part of aggregate function].
Query B:
SELECT IIF(DATEDIFF("d",Date_X,Date_Y)>100),
ROUND(IIF(DATEDIFF("d",Date_X,Date_Y)/30,2),
DATEDIFF("d",Date_X,Date_Y)
AS DATEDIFF_X_Y
FROM LAB_DATES GROUP BY LAB_DATES.ID;
This is in MS Access SQL.
ControlB source referencing QueryB in MS Access:
=DLookUp("[DATE_DIFF_X_Y]",
"[QueryB]",
"[LAB_DATES.ID] = " & [Forms]![Lab Results Form]![Textbox_DATE_ID])
When taking out the GROUP BY, this query runs fine but I get the #Name? error in the control. All data is from ODBC MySQL. Access is the front end.
Edit: I can just drop GROUP BY. But I will get the #Name? error. My goal is to display the date difference between Date_X and Date_Y.
[...execute query does not include the specified expression as part
of aggregate function]
That error message already indicates that using GROUP BY does not work with aggregate Functions.
DATEDIFF() is an aggregate function and does not work with a GROUP BY.
The reason for that is that GROUP BY reduces your dataresult and only displays one line per different entry of the column you are using.
What is your aim for that query anyways? I am sure there is a different solumtion for that problem. GROUP BY DATE.ID sounds like you are making a GROUP BY on the primary key which has no effect, because primary keys are unique per definition.
I think your problem is within your DLookup rather than your query. You do not need the GROUP BY at all, this will cause the query to error, you can simply make QueryB:
SELECT IIF(DATEDIFF("d",Date_X,Date_Y)>100),
ROUND(IIF(DATEDIFF("d",Date_X,Date_Y)/30,2),
DATEDIFF("d",Date_X,Date_Y) AS DATEDIFF_X_Y
FROM LAB_DATES;
The problem is that in your DLookup you are using:
[LAB_DATES.ID]
Where you actually want
[LAB_DATES].[ID]
i.e. In it's current form you are looking for a column called LAB_DATES.ID rather than a Column called ID in the object LAB_DATES. Changing your DLookup to this:
=DLookUp("[DATE_DIFF_X_Y]",
"[QueryB]",
"[LAB_DATES].[ID] = " & [Forms]![Lab Results Form]![Textbox_DATE_ID])
Should do the trick.