Compare 2 dynamically created matrix columns to get a difference - reporting-services

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)

Related

Get count of specific values in a single field for a given group of records

I have a set of records in SSRS (SQL Server 2019) with a "code" field that has a value from a list (M, S, W). I'm grouping on another field and need to get counts of this field's value within the group (i.e., how many rows in the group have S, how many M, etc.)
I'm probably describing this poorly but this is a simplified version of what I'm trying to do.
name | code
-----|-----
Bob | M
Bob | S
Bob | S
Bob | W
Joe | S
Joe | S
Grouping on name, I'd like to be able to see this:
Bob group M = 1, S = 2, W = 1
Joe group M = 0, S = 2, W = 0
I'm trying to write an expression to hide/show a group based on these values so I need to be able to test them. So far, I'm not able to figure out how to do this in SSRS. Can it be done?
FWIW, I've previously accomplished this in Crystal Reports by creating variables within the group and literally counting each value while printing the records and then examining them at the end of the group.
I would probably just sum 1 for each matching value. Assuming the expression sits within your rowgroup then you won't need the scope specified
=SUM(IIF(Fields!code.Value = "M", 1,0))
Obviously needs to be repeated for each code but that should work.
Another option is to simply use a Tablix report.
For example, set the 'Row Groups' to Name and 'Column Groups' to Color and use Count as the aggregate.

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:

SSRS 2012 MATRIX TOTAL DYNAMIC COLUMNS BY COLUMN HEADING

Simple Q:
In SSRS 2012: require a matrix with column totals derived from the dynamic column headings;
e.g.
Period 1 2 3 Total Total
Type Act Bud Act Bud Act Bud Act Bud
Total 10 9 10 9 10 9 30 27
is this possible in SSRS?
Can easily get the total of the sum of columns -(57) but not split by type.
Thank You.
data is grouped ;
Business Unit - Row Group
Account Type - Row Group
Month Period - Column Group
Amount_Type (Act, Bud) - Column Group
The Aggregate is Amount
Tks
Without knowing the structure of your dataset I think you can add two columns outside the columns groups at the right side and use these expression to calculate the respective total:
For Actual total:
=SUM(IIF(Fields!Type.Value = "Act",Fields!Amount.Value,0))
For Budget total:
=SUM(IIF(Fields!Type.Value = "Bud",Fields!Amount.Value,0))
UPDATE: Try setting the scope of the SUM function.
=SUM(IIF(Fields!Type.Value = "Act",Fields!Amount.Value,0),"DataSetName")
Replace DataSetName by the actual name of your dataset.
Let me know if this helps.

Add sum of columns to chart SSRS

I have searched all over and cannot seem to find a definitive answer for this issue! I have a simple chat here grouped on the 5 categories below detailing the Sums of their SqFt.
I want to add a Total Column to the graph ~(Total = 11M sqft). Can this only be done in SQL? It is a bit puzzling for me to do this because the query already sums the sqft for each row (as a nested query). I would need to Sum(sum(sqft)) in order to produce what I want, however, I dont believe this will work on the group level.
Sample Data set:
ID| Type| Sqft|
12| OF| 500
14| IN| 1294
99| OF| 12042
24| ME| 92043
15| IN| 13945
16| OW| 2650
Can this be done in the report builder?
Thanks!
You can add a Total row in your query by using GROUPING SETS operator. Once the total is in the dataset it is trivial to show the column in the chart.
Based on the data sample you posted you can use a similar query to the below:
SELECT
CASE
WHEN GROUPING_ID(Type) = 1 THEN 'TOTAL'
ELSE Type
END [Type],
SUM(Sqft) Sqft,
GROUPING_ID(Type) [Grouping]
FROM your_table
GROUP BY GROUPING SETS ((Type), ())
Check this Live Demo
If you are confused by the above query you can simply use the union operator to add a row to the end of your current dataset.
SELECT
ID,
[Type],
Sqft
FROM your_table
UNION ALL
SELECT
NULL,
'Total',
SUM(Sqft)
FROM your_table
Now just create your chart using the produced dataset.
Let me know if this helps.

Reporting Services How to get the Count() value from IIF

I have this table with a column named Open_Time which contain a datetime value. I would like to have another column named Total Ticket In June with the total count of ticket in June, so I've inserted the expression like below:
=Count((IIF(DatePart("m",Fields!Open_Time.Value,0,0) = "6",1,0)))
but there seems to be an error. To make myself clear, the table should look like this:
Assigned Name Ticket ID Open_Time Total Ticket in June
Ivan 001 3/28/2014 2
002 6/24/2014
003 6/11/2014
I would like to get value "2", which is the total number of ticket in June. Any idea? :)
You need to switch to a Sum aggregate instead of Count (based on your IIF, Count will count every row instead of giving you a total number of occurences):
=Sum(IIF(DatePart("m",Fields!Open_Time.Value,0,0) = 6, 1, 0))
From your table it's hard to tell your row groupings, but keep in mind that you'll only get the sum you expect if that expression is on a totals row (i.e. if you use that expression on the detail row it will simply list 1 or 0 for each date).
Try this:
=Count((IIF(DatePart(dateinterval.Month,Fields!Open_Time.Value,0,0) = 6,1,0)))