change colour of highest total column value in rdl report - reporting-services

In rdl report i am showing total column as sum aggregation, i want to highlighte highest of that total.

This is a generic answer that makes lots of assumptions. As you question does not contain enough info for a definitive answer but hopefully this will give you enough that you resolve your problem.
To recreate this do the following (and then adapt to suit your specific situation)
Create a new report.
Add a datset and use the following query as the dataset query
-- create some sample data
DECLARE #t TABLE(Customer varchar(10), Product varchar(10), Quantity int)
INSERT INTO #t VALUES
('Dave', 'Hammer', 6), ('Dave', 'Saw', 6),('Dave', 'Hammer', 1),
('George', 'Drill', 3),('George', 'Hammer', 6),('George', 'Saw', 6),('George', 'Hammer', 1),
('Mary', 'Drill', 3),('Mary', 'Hammer', 6),
('Jane', 'Saw', 6),('Jane', 'Hammer', 1),('Jane', 'Drill', 3)
-- return the sample data plus a total per customer
SELECT *
, CustomerTotal = SUM(Quantity) OVER(PARTITION BY Customer)
FROM #t
This give use the following results
Next add a Matrix control to the report.
Drag the fields to the matrix as shown here
Customer to "Rows"; Product to "Columns"; Quantity to "Data"
You should now have a matrix with some row and column groups shown underneath.
next we need to add a total column so right-click the "Product" column group select "Add Total => After"
Finally, we need to test if the value in the total column matches the largest CustomerTotal in our dataset. If it does match then change the textbox color property to "Red".
We can use this expression in the textbox color to do this..
=IIF(
Sum(Fields!Quantity.Value) = MAX(Fields!CustomerTotal.Value, "DataSet1"),
"Red",
"Black")
What this does is take the total quantity in the current scope (the entire row) and compare it to the highest CustomerTotal with the scope "Dataset1" which is the entire dataset. If the two match, set the vlue to "Red", else set it to "Black"
With a bit of a tidy-up the final output looks like this.

Related

Getting Scope error on SUM(IIF()) of rows from other table

Currently getting a scope error using the code below, what we're trying to do is count the number of rows that match the conditions we have:
=
SUM(
IIF(
Fields!Defect_Category.Value = "Packaging"
& Fields!Defect_Category.Value = "Major"
& Fields!WorkOrderDisplayID.Value = Fields!Work_Order_Id.Value,
1, 0),
"dsDefects"
)
Work_Order_Id is the "key" of the dsGeneral dataset which is the current scope/dataset of the tablix where we're trying to implement this. Any way we can fix this?
My understanding is that the Scope parameter of SUM is referring to the dataset we're trying to get the sum of (or count of, in this case). When I specify "dsDefects" as the scope of SUM, I get the following error:
The Value expression for the text box 'Textbox101' refers to the field
'Work_Order_Id'. Report item expressions can only refer to fields
within the current dataset scope or, if inside an aggregate, the
specified dataset scope. Letters in the names of fields must use the
correct case.
However, if I remove the scope parameter value, I'm getting the following error:
The Value expression for the text box 'Textbox101' refers to the field
'Defect_Category'. Report item expressions can only refer to fields
within the current dataset scope or, if inside an aggregate, the
specified dataset scope. Letters in the names of fields must use the
correct case.
I think you will need something like this...
=
IIF(
Fields!Defect_Category.Value = "Packaging"
& Fields!Defect_Category.Value = "Major",
LOOKUPSET(Fields!WorkOrderDisplayID.Value, Fields!WorkOrderDisplayID.Value, Fields!WorkOrderDisplayID.Value, "dsDefects").Length,
0)
I'll give a simple example of counting matches from another dataset and that might help you put the two together to get an solution.
If I create two datasets with the following queries, called dsEmp and dsDev respectively
dsEmp
DECLARE #e table (empid int, empname varchar(10))
insert into #e values
(1, 'Bob'), (2, 'Dave')
SELECT * FROM #e
dsDev
declare #d table(empid int, device varchar(10))
insert into #d VALUES
(1, 'Phone'),
(1, 'Laptop'),
(1, 'Desktop'),
(2, 'Phone'),
(3, 'Tablet')
SELECT * FROM #d
Then in my report I add a table bound to dsEmp showing the empID and empName and then in the final column I use the following expression
=LookupSet(
Fields!empid.Value,
Fields!empid.Value,
Fields!empid.Value, "dsDev"
).Length
I get this final output
As lookupset returns a collection, the collection's length is, in fact, the number of items contained in the collection.

How to SUM the values from group in SSRS which are distinct

I have a report where I want to calculate the Grand total but ignore the values of a group which have same value. Below are my tables and the data. The Package No column is grouped and will be unique always, but every unique Package may or may not have same dimensions. I want to SUM the dimension (Length, Width and Height) and it Package No has multiple items in it then have to consider only the first row for summing up the values as the dimensions will remain the same for all the items within the same Package. Can anyone please help me to achieve this result?
Assuming the data that gets returned from your dataset query looks like the sample you show in the image then you should be able to do something like...
=SUM(IIF(Fields!RowNum.Value = 1, Fields!Length.Value, 0))
EDIT after OP update
If the RowNum column is not in your dataset then you will need to add it.
If you dataset query is just a script then it should be as easy as something like this...
SELECT *
, RownN = ROW_NUMBER() OVER(PARTITION BY PackageNo ORDER BY Item)
FROM myTable
If the dataset is the result of a stored proc then you might have to change the dataset query to something like
CREATE TABLE #t (PackageNo varchar(50), Length int, Width int, Height int, Item varchar(50))
INSERT INTO #t
EXEC myStoredProc
SELECT *
, RownN = ROW_NUMBER() OVER(PARTITION BY PackageNo ORDER BY Item)
FROM #t
Once one of the above options are been taken then you should be able to simply apply the =SUM(IIF(Fields!RowN.Value = 1, Fields!Length.Value, 0))
expression in your report totals.

SSRS Matrix conditional formatting

I have spent some time but can't seem to be able to accomplish this. I have this very simple matrix report and would like to highlight the Amount column if it is different from previous date. Please see below for my report designer screen and my desired output screen with 04/15/2020 highlighted since the amount is different from 04/14/2020. (sorry I circled it instead highlighting it).
Thank you in advance for any suggestions.
You can do this directly in SSRS but its messy and will rarely give perfect results if you are using a dynamic number of columns which is almost always the case.
You end up having to use a bit of VBA code to track the last value but if you use that in a background color expression for example it will mess things up.
There are plenty of questions just like this and most either unanswered or rely on you knowing what the date values are in advance.
NOTE: I have used a windowed function here, I think this is available in SQL 2008 but cannot be certain.
So to start I created some test data
Then I summarize this into a temp table (assuming you need to do summarize by project and date?), its here I use the windowed function to get a row number. We need this in case there are gaps in the dates.
Finally I join the temp table back to itself offsetting by 1 Row (using the row number)
Here's the full code I used in my dataset.
-- create some sample data
DECLARE #t TABLE(dt date, project varchar(10), amount float)
INSERT INTO #t VALUES
('2020-04-01', 'A', 10),('2020-04-01', 'A', 10),('2020-04-01', 'B', 10),('2020-04-01', 'C', 10),('2020-04-01', 'C', 10),
('2020-04-02', 'A', 20),('2020-04-02', 'A', 20),('2020-04-02', 'B', 10),('2020-04-02', 'C', 20),('2020-04-02', 'C', 20),
('2020-04-04', 'A', 25),('2020-04-04', 'A', 15),('2020-04-04', 'B', 10),('2020-04-04', 'C', 25),('2020-04-04', 'C', 25)
-- summarise and add a row number
SELECT project, dt, SUM(amount) as amount , ROW_NUMBER() OVER(PARTITION BY project ORDER BY dt) as RowN
into #x
FROM #t
GROUP BY project, dt
-- join #x to itself offseting by 1 row and calc diff vs previous amount
SELECT
cur.*
, cur.amount - ISNULL(prv.amount, cur.amount) as diff -- if there is no previous amount compare to current amount to difference is zero
FROM #x cur
LEFT JOIN #x prv
ON cur.project = prv.project
and cur.RowN = prv.RowN + 1
This gives us the following results...
Now allwe have to do is use this in our matrix and set the BackgroundColor property of the textbox to something like
=IIF(Fields!diff.Value = 0, Nothing, "#ff8c8c")
This gives us this as the final output.

How to use an AND LIKE parameter in an SSRS report with an additional OR option?

I have a Dataset Query in SSRS as follows
SELECT * FROM TableName
WHERE Account_Code LIKE #Department+'%'
This is so that the SSRS report is able to provide a drop-down of specified available departments based on the first letter of the Account Code(s) in the Account_Code table.
I have set the #Department parameter in SSRS Report Builder with specified available values as shown below...
However, I need Department 3 to be based on two criteria (i.e. with an Account_Code beginning with 'C' or 'D'). Entering the expression 'C OR D' in parameter properties does not work. Do I need to re-think the dataset query itself or can this be done in SSRS parameters with just my LIKE #Department clause?
You could set the third value to [c-d]. See the example below.
DECLARE #Department varchar(10)
SET #Department = '[c-D]'
CREATE TABLE #Test (Account_Code char(2))
INSERT INTO #Test(Account_Code) VALUES
('CA'),
('CB'),
('DA'),
('DB'),
('AZ'),
('AY'),
('BZ'),
('BY')
SELECT *
FROM #Test
WHERE Account_Code LIKE #Department + '%'
DROP TABLE #Test

MySQL Sum values in column based on type column

I have two columns called value and type. I want to sum all the values in my table together, but when the type is 'alpha' I want to add them and when the type is 'beta' I want to subtract them. Assuming that type is a varchar and value is a numeric type, how do I do this? Is there a built in function for this or do I need to define my own?
This seems to work. Note that I used column names theValue and theType instead of value and type.
SELECT SUM(IF(theType = 'alpha', theValue, 0)) - SUM(IF(theType = 'beta', theValue, 0)) AS theNetResult
FROM mytable;
DEMO
This is another way to do the same
SELECT SUM(IF(theType = 'alpha', theValue,(theValue*-1))) AS NetResult
FROM mytable;