Reusing an expression in SSRS field - reporting-services

I'm writing a report in SSRS. I have a report with some fairly long expressions although the calcuations are simple additions, subtractions and mults and divs. Is there a way to capture the results of an expression for use in a calculation in another field without having to repeat the whole original calculation? I already do part of the calculation in underlying views. Is it possible to do something similar to referencing a 'Field.xxx.Value'? I'm using 2008 R2 for now but will be moving to Sql Server 2012 soon.

Supposedly the result of the complex expression that you want to reuse is located in a Text Box named "amt3". Then you can use this to, say, Color it:
=iif(ReportItems("amt3").Value < 0, "Red", "Green")

Are you wanting to repeat a calculation or use a calculated value?
If you want to repeat a calculation you can use inbedded code. Just write some VB that takes in whatever parameters you want and have it output the results.

Related

How To Show Or Hide A Tablix Based On A Bit Field In SQL Server Reporting Services

I have a multi row dataset that I want to do a lookup on and take the bit/Boolean value of one of it's columns and apply it to the hidden property of a tablix.
Here is the expression for the hidden property on the tablix.
= IIF(Lookup(6, Fields!ServiceOfferingID.Value, Fields!UseCustomCalculator.Value, "JobBuyerDetails") = 0, True, False)
If the ServiceOfferingID is 6, get what is in the UseCustomCalculator column to show or hide the tablix.
I don't know if you can compare values like this, what may need to be a string or if they are all just variants.
I have tried converting the values to string with CStr(), putting quotes around the searched value (6 in this case) but have had no luck getting the tablix to show or hide correctly.
There doesn't seem to be a good way to debug this, check what the Lookup is returning etc.
How can I change my expression to get it to work?
What am I missing?
I am pretty new to SQL Server Reporting Services.
I am using SQL Server 2008 R2.
Debugging in the traditional sense isn't really an option. What I normally do is add a textbox and set the expression to the expression you want to test, in your case, set the expression to
=Lookup(6, Fields!ServiceOfferingID.Value, Fields!UseCustomCalculator.Value, "JobBuyerDetails")
and see what you get.
Once you've established what is being returned and corrected the comparison then you can simplify the hidden expression a little.
The hidden expression would just be something like (assuming lookup issues are resolved)
=Lookup(6, Fields!ServiceOfferingID.Value, Fields!UseCustomCalculator.Value, "JobBuyerDetails") = 0
There's no need to use an IIF here as the comparison will return return or false anyway.

ProperCase SSRS 2008 Within Footer

I am trying to create a footer that is using ProperCase within SSRS 2008
I have tried
=Code.ProperCase(LCase(Fields!aField1.Value, "DataSet1"))
to use the ProperCase field within the footer but it is stating that I do not have the text box linked to a DataSet.
Help would be much appreciated.
I don't think the problem lies with the ProperCase function. However, when inserting a value from a dataset, outside the context of the dataset, you must specify what record to use. For example, to use the first record from the dataset in your page footer, you should write your expression like this:
=Code.ProperCase(LCase(First(Fields!aField1.Value, "DataSet1")))
In other words, the reference to "DataSet1" makes no sense in the LCase-function, which is just a simple string manipulation function. To reference the dataset, you must use one of the Aggregate functions (in this case, First()), which takes as second argument the name of the dataset in question.
I assume you have written the ProperCase function yourself? There is a VBA function available in SSRS that allows you to change the case of a string and it's called StrConv which you could've used.
In your case you'd write: StrConv(First(Fields!aField1.Value, "DataSet1"), vbStrConv.ProperCase)
The other benefit of the StrConv function is that you can specify a localeID too if that is of any relevance.

Use of a Previous aggregate function in a TablixCell error in one tablix, not in another

I have an SSRS report that works just fine with this code for checking the previous cell in a tablix:
=IIF(SUM(Fields!Transactions.Value) >
Previous(SUM(Fields!Transactions.Value),"matrix1_Month"), "Green", "Red")
This works in the report in the first tablix, matrix1, just fine.
I copied that tablix, used a different Different dataset, called it xForeignCardholders, and used this code:
=IIF(Fields!Transactions.Value >
Previous(Fields!Transactions.Value),"xForeignCardholders_Month", "Green", "Red")
It is the same except for the name of the tablix.
Now the report gives me the error
"Use of a Previous aggregate function in a TablixCell ..."
Why would it work in the first instance, and not in the second? The report is just one datacell, with no aggregation. In a previous version of this report, three different tablixes (tablii?) all used the same kind of code without error.
I can fix this in SQL, but it would really add a lot of complexity to something that should be really straightforward.
My situation while similar, is not quite identical - so may not be what you're running into, but I've gotten that exact error, and it was because my grouping had changed - that is, each grouping has a unique name, so if you're copying expressions from one table to another, you have to make sure you've updated the group name in your 'Previous' expression. I was using a field called "Year" in both tables, but it appears you can't repeat a group from one table in another. So when I copied my cell (and the expression therein), the destination grouping was "Year1" or "Year2" etc. The expression had to be updated accordingly.
In your case, make sure that the each expression has the right group name, in your example, "xForeignCardholders_Month". You may find that the group name is actually "xForeignCardholders_Month1" or something similar.
I also found that when I would copy/paste the <> from one cell to another, it drops most of the expression. You have to edit the original expression, copy it, and edit the expression in the new cell, and paste it. This only seems to happen when using a small report code script calculate YoY delta, and everything after "code.GetDeltaPercentage" was being dropped from my expression when I copied. Just something to look out for.
Update: I just discovered that sometimes, after a copy/paste of some cells, after verifying the expressions were exactly the same, I would still get this error. Delete the contents of the cells, choose the Sum(Field.value) from the selector icon in the cell, and then edit the expression on the adjacent cell and plug in my 'Previous' expression, and it would work. If there's a good reason for this behavior, it's beyond me. Seems like another VS Bug. :|
I'm self taught, so I may have some terminology wrong, but hopefully that gets the idea across anyway.
I think the problem is that in the first instance you use an agregate function, whien in the second you use the value itself, without the aggregation, compare:
IIF(SUM(Fields!Transactions.Value) > Previous(SUM(Fields!Transactions.Value),"matrix1_Month"), "Green", "Red")
and
=IIF(Fields!Transactions.Value > Previous(Fields!Transactions.Value), "xForeignCardholders_Month", "Green", "Red")
Best regards,
~Alexey

Adding Fields into an expression in an SSRS 2008 report

Thank you in advance for taking your time to answer my question.
I am having trouble with expressions in the SSRS reporting system.
The Field I am adding required fields from the dataset I provided in the report, however when I try to preview the report I get the Following message:
"A Value expression used for the report parameter ‘Policies_Total’
refers to a field. Fields cannot be used in report parameter
expressions."
This is my expression:
=IIF(Sum(Fields!policy_id.Value, "DataSet1") Is Null, 0, Count(Sum(Fields!policy_id.Value, "DataSet1")))
That was suppoed to be converted from Crystal reports which has the following expression:
If IsNull ({usp_rep_agent_cases;1.policy_id}) then
0
Else
Count ({usp_rep_agent_cases;1.policy_id})
Any help is much appreciated.
Thank you
I think it may be as simple as understand that a 'parameter' should be passed into SSRS before fields are created for the most part. If this parameter is DEPENDENT on the value of something else first being chosen you cannot list it first as the field is not yet populated to my knowledge. It appears you are trying to use an expression to count something from a field from a dataset when you just make a dataset and reference that field directly. So instead of trying an expression you may choose a few other options instead:
Choose on the left pane of your parameter 'Available Values' selected 'Get values from a query'. You may use your query which is a 'dataset', value is self explanatory, label is what the end user will see display.
The option 'Allow null' value will accept a null value
You may run into situations where multiple datasets may need to be used, multiple selects or querying objects. In my experience with SSRS it gets mad at times when you try to reference a dataset used to display data with a dataset used to determine an event or action. SSRS also gets relativity slower the more Expressions you do so doing a whole report with nothing but expressions versus taking the power of the built ins of the RDL language is not really worth it IMHO.
For SSRS expressions you need to use IsNothing for NULL checking, something like:
=IIF(
IsNothing(Sum(Fields!policy_id.Value, "DataSet1"))
, 0
, Count(Sum(Fields!policy_id.Value, "DataSet1"))
)
In fact the whole expression seems a bit odd; what are you specifically trying to achieve with your expression? Are you just trying to count non-null values?
=Sum(IIf(IsNothing(Fields!policy_id.Value), 1, 0), "DataSet1")
Also, your error seems to be saying that a parameter is referencing a field when this isn't allowed, which may not be solved by changing syntax; I think more information about what you're trying to achieve is required here.

stuck in microsoft report

My report is containing some null value.
Report is generated through query
In my report I am calculating sum of every field for that I had written =iif((field name) is null,0,sum(field name))
Through this function I am getting result as 0 only if field contains some value as well
I agree with Remou, you should be using the footers for totals. However if you wanted to carry on with the way you are doing things then try replacing the IIF with NZ(MyField,0)
You should put that IIf statement in the query for that (or those) fields that you want to avoid nulls on. That way you can just put the Sum function in the details section of the report with impunity (assuming details section here).