Simple IIF statement to determine a sum - reporting-services

I am a rookie to SSRS and am having difficulty obtaining a Sum.
I want the cell to Sum the distinct values of "UnitNumber" which is what the report is using to generate each row of my table. The reasoning behind this is that behind the complex report, Unit numbers are distinct and provide me with distinct SqFt Values. For example:
Unit # | Sqft|
Unit 001 | 472 |
Unit 002 | 600 |
Unit 004 | 1203|
The below does not work:
Sum(IIF(Fields!Unitnumber.Value,1,Fields!SqFt.Value)
I either get "Contains an error: cannot be converted to String" or, #Error in the cell. I cannot solely use Sum(SqFt) because it dumps an aggregate of the whole dataset query (every single row summed up) Any ideas?

It seems that your IIF missing main condition. It can be something like below or do as per your requirement.
Sum(IIF(Fields!Unitnumber.Value=1,1,Fields!SqFt.Value)
Also, I would suggest you to make your expression like below,
=SUM(IIF(Fields!Unitnumber.Value = 'putyourcondition', Fields!SqFt.Value, 0))

Related

lines break after X records in single ssrs page

I'm getting more than 200 records (for example, however, the number of records are dynamic) in a single table column in SSRS. I would like to do a line break after 25 records.
ReportTable result (Just an example):
Value
-----
jklsa
dfv
b
grt
trj
h
muik
rg
kuu
wd
gb
nm
wef
Final Rrport table desired output:
Values
-----------------
jklsa |muik | wef
dfv |rg |
b |kuu |
grt |wd |
trj |gb |
h |nm |
That is, after introducing line break, the records has to be moved to the "right side".
So, in this case instead of single column with 200 records, It should be 8 columns [column name can be same] each having 25 records.
This is what I have tried :
=IIF(CountRows(Fields!Request_Number.Value) > 25,vbCrLf,Fields!Request_Number.Value)
and
=IIF(CountRows(Fields!Request_Number.Value) > 25,Fields!Request_Number.Value, "") & vbCrLf
However, it throws error:
"The value of expression has a scope parameter that is not valid for
an aggregate function"
Hope I have explained this correctly.
What am I doing wrong? how to get the correct result?
Thanks
CountRows(Fields!Request_Number.Value)
wont give you correct count for your Rows on SSRS.
Instead use
RowNumber("DataSet1")
Note:"DataSet1" is your Dataset name.
By the way what exactly you wish to achieve. As you will be using Table or matrix do you wish to add pageBreak because linebreak works with Textbox and not with entire table.
Link for Ref:
https://learn.microsoft.com/en-us/previous-versions/sql/sql-server-2008-r2/ms159225(v=sql.105)

Lookup on 2 datasets in SSRS

I am having a problem using Lookups in a ssrs report. The report takes in 3 datasets that I do not have the opportunity to edit or merge (three different cubes)
It is a sales report that should be grouped by Sales Manager and show the potential for sales and a "discounted potential" of sales. The problem I face is that I have to loop my table on DataSet1 since it is the only on that holds Sales Mangers.
Using LookupSet and LookupSum it is easy enough to get the potential from DataSet2. Something like:
=Code.LookupSum(LookupSet(Fields!Country.Value, Fields!Country.Value, Fields!Potential.Value, "DataSet2"))
The problem arises when I try to calculate the Discounted Potential form DataSet3
Namely because I need to perform the lookup based on a value that is not in DataSet1! Is this somehow possible?
The datasets and the desired report look like this
You should be able to change the dataset of the tablix to DataSet2 to get the desired results. Dataset2 is the only dataset that directly relates to both of the other datasets, and since nested Lookups are not allowed, and also since you cannot modify your datasets, this is necessary for this situation.
I'm not sure exactly what calculation you are using to end up with your "Discount" column, I couldn't figure out any formula that worked with all of your sample data shown. For my test I just took the Sum to make sure it was working, but you should be able to modify that to fit your needs.
I set up a tablix like so:
+---------------------------------------------------+
| Manager | Country | Potential | Discount |
| <Expr1> | [Country] | [Sum(Potential)] | <Expr2> |
+---------------------------------------------------+
With 2 row groups, the first grouping on Expr1, and the child group grouping on Country, where Expr1 is =Lookup(Fields!Country.Value, Fields!Country.Value, Fields!Manager.Value, "DataSet1") and Expr2 is =Sum(Code.SumLookup(LookupSet(Fields!Customer.Value, Fields!Customer.Value, Fields!Discount.Value, "DataSet3"))). I sorted the parent row group by Country to keep the sorting the same as you had it in your screenshot. Again though, you would likely have to modify Expr2 to fit your needs. If the discount is a percentage for that particular customer, then the following code should work for that, but the results don't match your screenshot so I'm not sure if this is what you are looking for:
=Sum(Fields!Potential.Value - (Fields!Potential.Value * Code.SumLookup(LookupSet(Fields!Customer.Value, Fields!Customer.Value, Fields!Discount.Value, "DataSet3")) / 100))
Results from using the modified Expr2 if Discount is a percentage:

Compare 2 dynamically created matrix columns to get a difference

I have a matrix set up with a Row Group called "company", a Column Group called "Year" and a value field that is a sum of charges through the year called revenue. The column Group will end up having 2 columns in it that are provided through user input. The end results is a matrix that will look something like this:
Year 1 | Year 2
Company: $500 $250
Company2: $750 $250
What I would like to do is add a column to the matrix that calculates the change from year 1 to year 2. Is there a way to do this within the matrix such as adding a new column with an expression that compares the 2 entries in the row or will I need to manipulate the SQL code to create a column that does this within the code? To that end here is a view of the code for the dataset if that is the way I need to go:
SELECT
company.cmp_id
,company.Company
,ChargeDetails.[Bill To ID]
,ChargeDetails.[Delivery Year]
,ChargeDetails.Revenue
FROM
ChargeDetails
LEFT OUTER JOIN company
ON ChargeDetails.[Bill To ID] = company.cmp_id
WHERE
ChargeDetails.[Delivery Year] = #DeliveryYear
OR
ChargeDetails.[Delivery Year] = #ComparisonYear2
ORDER BY ChargeDetails.[Delivery Year] DESC,ChargeDetails.Revenue DESC;`
Try to add a column outside the Year group in header type Change or what you want to put, in the below cell use this expression:
=Sum(iif(Max(Fields!DeliveryYear.Value)=Fields!DeliveryYear.Value,Fields!Revenue.Value,0))-
Sum(iif(Min(Fields!DeliveryYear.Value)=Fields!DeliveryYear.Value,Fields!Revenue.Value,0))
Note I am substracting the min year (1) revenue sum to the max
year (2) revenue sum.
In your example it will produce:
+-----------+--------+--------+--------+
| Company | Year 1 | Year 2 | Change |
+-----------+--------+--------+--------+
| Company A | 500 | 250 | -250 |
+-----------+--------+--------+--------+
You can define change in many ways maybe in percentage or any other measure, this is only an example, learn what the above expression is doing in order to calculate the measure you need.
EDIT 1:
Add the matrix with this data arrangement.
The matrix looks like this one in preview:
Be sure you added the Change column outside the DeliveryYear group as shown in above image. Also check the fields names correspond to yours in the expression.
EDIT 2:
If Revenue has null values you can try this:
Try replace the null values at query level using T-SQL ISNULL() Function, change your query in this part as follows
,ISNULL(ChargeDetails.Revenue,0)
However if DeliveryYear has null values you may want to ignore that records, so try to exclude it in the where clause.
Let me know if you need further help.
I ended up using the following as the expression in the change column.
=sum(iif(Fields!Delivery_Year.Value=Parameters!DeliveryYear.Value,1,0)*Fields!Revenue.Value) - sum(iif(Fields!Delivery_Year.Value=Parameters!ComparisonYear2.Value,1,0)* Fields!Revenue.Value)

Using runningtotal() inside a Max() in ssrs

I am building a report using a matrix which have 3 row group and 2 column group. Now I wanted to calculate the sum total of a column based on a row group. I searched for a solution but didn't got one.
The only solution that I could think of was to use a RunningValue Function
Now my running value column output is like
| Original Values || Running Value |
|-----------------||---------------|
| 6676 || 6777 |
|.................||---------------|
| 6859 || 13625 |
|.................||---------------|
| 5320 || 66830 |
Now I want the result 66830 so that i can use it to compute other values
so can anyone please tell me
How to use RunningTotal function inside MAX function
what I am doing is
=MAX(runningvalue(Fields!NET.Value,sum,"matrix1_MIX"))
but it gives me an error
An error occurred during local report processing.
The defination of the report'Report1' is invalid.
The Value expression for the textrun 'Textbox106.paragraphs[0]TextRuns[0]' contains an
aggregate function (or RunningVale or RowNumber functions) in the argument to another
aggregate functiion (or RunningValue). Aggregate functions cannot be nested inside other
aggregate functions.
Is there a workaround to this problem in ssrs.
Thanks in advance
The maximum of a RunningValue by sum is the same as the regular SUM of the same value, taken in the scope of the entire group. Try something like this:
=Sum(Fields!NET.Value, "matrix1_MIX")
Think carefully about what you use for the second argument, that's key to getting the figure you need.

Column static grouping in Matrix

I'm trying to create a technical indicator in Microsoft SSRS. I have a dataset that has a table that tells me whether or not my product has passed various stages in testing and I want to take one of those stages and break it into two sub columns statically. So for instance I have temperature and want to break it out into min and max operating temp so the columns will look like:
| Weight | Height | Temperature | Length |
| min | max |
However I can't find any way to statically define the column breakdowns for Temperature, and one of my main problems is I need to pull the min max data from a different database than the rest of the information and therefore need to make it it's own dataset. Do you have any ideas on what I could group on or how to get this to split up?
I ended up figuring how to do this by creating a new row and then stretching the group over two columns. Then I just hid the unwanted columns with an if statement.