How to Combine Fields from Two Datasets SSRS? - reporting-services

I am working on a report to show the total number of hours an employee spent working. Our company tracks labor hours by Service Request and Work Order so I need to bring totals for each into the report.
I created two datasets- one for Work Orders and one for Service Requests. Ideally, I would like to combine the total number of Work Order hours with the total number of Service Request hours and present that number listed by employeeID since both datasets have the employeeID field.
I thought it would be as simple as:
=(SUM(Fields!TOTALHOURS_WO.Value, "DataSet1") + SUM(Fields!TOTALHOURS_SR.Value, "DataSet2"))
I don't get an error, however, I am getting a number which repeats for each employee so I know I'm doing something wrong.
Any help is greatly appreciated.

Mal,
As #StevenWhite mentioned, LOOKUP is probably the function you are looking for.
Here is an example for you. For the example datasets:
EmployeeID | TOTALHOURS_WO
-----------------------------------
123 | 12
456 | 3
EmployeeNum| TOTALHOURS_SR
-----------------------------------
123 | 2
456 | 5
You will note that each table in a SSRS report needs a DataSet assigned to it. I will assume your table is using our first DataSet, which we will name "DataSet1". The second dataset above will be "DataSet2".
For your total hours you will use an expression. It should look something like this:
=TOTALHOURS_WO + LOOKUP(Fields!EmployeeID.Value, Fields!EmployeeNum.Value, Fields!TOTALHOURS_SR.Value, "DataSet2")
So you will be adding the TOTALHOURS_WO from your local dataset to the result from the LOOKUP function. What lookup is doing is taking the first field from your local dataset, finding a match in the dataset provided to the function (as a string), and returning the field from the row it matched to. The last parameter is the dataset to search.
Just in case you get an error... it's always a good idea to cast data to the type you want to work with in case it comes in wrong. So...
=CINT(TOTALHOURS_WO) + CINT(LOOKUP(Fields!EmployeeID.Value, Fields!EmployeeNum.Value, Fields!TOTALHOURS_SR.Value, "DataSet2"))
This assumes you have a one to one match on employee ID. If you have to SUM both fields you can try this:
=SUM(CINT(TOTALHOURS_WO)) + SUM(LOOKUPSET(Fields!EmployeeID.Value, Fields!EmployeeNum.Value, CINT(Fields!TOTALHOURS_SR.Value), "DataSet2"))
SUM for TOTALHOURS_WO will give you the SUM in your current table group (so make sure you are grouping by staff ID in the table). It will then add it to the SUM of LOOKUPSET. LOOKUPSET works the same as lookup but returns an array of matches instead of the first.
Hope this helps.

Related

SumIIF Access Query

I am struggling to get the desired results i need using an Access query, and was wondering if what i was looking to do was actually achievable in one query, or whether i would need two queries and then export to Excel and interrogate the results there.
I've a table with a number of columns, i am specifically looking at three columns
Row Type - this will either be populated with A or U
Account Number - there will be potentially multiple instances of account number within the table. Although only once against row type "A", and multiple on row type "U"
Value - a currency field. At Account number level, the sum of "U" row, should equal the "A" value
I am looking to produce a query that will list three things.
[Account Number]
Sum of [Value] when [RowType] = "U"
[Value] when [RowType] = "A"
Would i need to create a new column in my table to generate a value for the requirement "Sum of Value when 'U')
I've tried
SUM(IIF([ROWTYPE]='U',[Value],0)) - but that doesn't seem to work.
I've also tried to use the builder within the Query to replicate the same, but again that also doesn't seem to work.
If all else fails i'm content to have to run two queries in Access and then join Excel, but i tihnk for my own learning and knowledge it would be good to know if what i am trying to do is possible.
I was hoping this is possible to compile in Access, but my knowledge of the application is seriously lacking, and despite looking on the MS Access support pages, and also some of the response on the StackOverflow forums, i still can't get my head around what i need to do.
Example of the data
Row Type
Account ID
Value
A
123456789
50.00
U
123456789
30.00
U
123456789
20.00
A
987654321
100.00
U
987654321
80.00
U
987654321
20.00
The data has been loaded into Access, table called "TEST"
This is the SQL i've got, but doesn't give me the desired results.
SELECT [TEST].[ROW TYPE], SUM([TEST].[VALUE]) AS [TEST].[ACCOUNT ID]
FROM [TEST]
GROUP BY [TEST].[ROW TYPE], [TEST].[ACCOUNTID]
When the query generates, would hope to see two rows, one for each account number.
Three row -
Account Number
Sum Value (where row is U)
Value (Where row is A)
I currently get 4 rows in the query. Two results for each account number, one result is the Value when Row Type = A, the other when Row Type = U.
I guess this is what you are after:
SELECT
[Account ID],
Sum(IIf([Row Type]="A",[Value],0)) AS A,
Sum(IIf([Row Type]="U",[Value],0)) AS U
FROM
TEST
GROUP BY
[Account ID];
Output:
Account ID
A
U
123456789
50,00
50,00
987654321
100,00
100,00

Using Lookup and getting a count from each Dataset

I have two datasets and I'm using Lookup to get one result, but the total is only from one dataset. I've tried and tried, but I'm having no luck.
First Dataset is called MedCond
This is the Data:
Drug_Name
Start_Date
Stop_Date
InmateID
Drug_Indication
Created
ID
Second Dataset is called ProblemList
This is the Data:
Medical_Condition
Inmate_ID
Created
ID
Drug Indication and Medical Condition are the same. I want to get a combined total of both.
This only gives me the count of the Drug Indications (which I have them grouped on) =Count(Lookup(Fields!Drug_Indication.Value,Fields!Medical_Condition.Value,Fields!Medical_Condition.Value, "ProblemList"))
I feel like I've tried everything under the sun. I'm extremely exasperated. I'm trying to get a total of each of the conditions/Indications that come from each dataset. For instance, One condition/Indication might be Addiction. There may be four addictions in from the Drug_Indication in the MedCon dataset and five addictions from the Medical_Condition in the ProblemList. I want to see Addictions 9 on the table and so and so forth for each Drug Indication/Medical Condition.
Thanks in advance for your help! Save my sanity. :) I'm sure it's probably something simple?
Tara
Thank you. I've tried using the Inmate_ID and InmateID as the key to join, but I still end up with only one of counts of either Medical_Condition or Drug_Indication.
As an example, there might be 10 addictions in one and 15 addictions in the other. I need them to be grouped under the title addiction (and whatever other titles there might be) with the total being 25.
It would look something like this.
Example Look
Something like this is close, but the counts aren't quite right.
=Count(Lookup(Fields!InmateID.Value, Fields!Inmate_ID.Value, Fields!Medical_Condition.Value, "ProblemList")) + Count(Fields!Drug_Indication.Value)
Maybe it's the way I'm grouping? How do you group on a combination of values such as Medical_condition and Drug_Indication?
Thanks again!
Tara
I think you used the Lookup() wrong. When I look at your two datasets (for me) the key to join the two datasets would be Inmate_ID.
=Lookup(Fields!InmateID.Value, Fields!Inmae_ID.Value, Fields!Medical_Condition.Value, "SecondDatasetName")
This would result in a table like this (The last column comes form the lookup above):
Drug_Name | Start_Date | Stop_Date | InmateID | Drug_Indication | Created | ID | Medical_Condition
Now you can just get the total per column:
Drug_Name | Start_Date | Stop_Date | InmateID | Drug_Indication | Created | ID | Medical_Condition
Total1 Total2
To sum Total1 and Total2 you can add a new tablix and reference to the textbox totals like this:
=ReportItems!Total1TextboxName.Value + ReportItems!Total2TextboxName.Value

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 Dynamic Column Number

I got a request from the client when using SSRS reporting Service. I am kind of new to the SSRS. hope that someone can help me out with the problem below.
I use a common example to show the problem.
In SSRS dataset1, the data is like follows
StuId StuSubject
--------------------------------
1 "Math"
1 "Geography"
2 "Science"
3 "Math"
3 "History"
3 "Music"
In the SSRS dataset2, the data is like this one
StuId StuName
------------------------
1 "Tom"
2 "Joseph"
3 "Linda"
In the SSRS DataSet3 , it would be like
StuId StuInt
--------------------------
1 "Swim"
2 "Chess"
2 "Swim"
2 "Running"
3 "Game"
What i want in the SSRS report would be like this one
StuId StuName StuSubject StuSubject StuSubject StuInt StuInt StuInt
1 "Tom" "Math" "Geography" NULL "Swim" NULL NULL
2 "Joseph" "Science" NULL NULL "Chess" "Swim" "Running"
3 "Linda" "Math" "History" "Music" "Game" NULL NULL
The tricky part is that I don't know what is the maximum number of StuSubject for all these students, or more precisely, I don't like to set the limit of the 'StuSubject' columns because there could be hundred columns in the real case. I have thought of LookupSet function, but seems using LookupSet, you can only join multiple StuSubjects values with "," in one cell.
Any advice / suggestion would be much appreciated and thanks in advance
Edit : I could use Matrix control for one "join" situation, but is this possible to "join" multiple datasets into one finale one ?
The solution for you is to use Matrix report item - it allows to perform data grouping by rows and columns. Your data, returned from DB, should be aggregate of those two datasets that you have - it should be set of rows in format (StuId, StuName, StuSubject).
You can then add row grouping by StuId (and child row grouping by StuName, but this is not necessary), and add column grouping by StuSubject. Details cell will simply output StuSubject.
Notice that although Lookup* functions allow you to do join data while processing the report, they are run while processing report by SSRS and thus are certainly less efficient (from my experience expressions almost always have really bad impact on report performance). Also, doing join whikle processing report is not a canonical way to develop the report, and not the best way to use SSRS engine, which works good when you need simple grouping or do not need grouping at all. The best approach is to generate SQL result as much close to what you want to display, as it is possible, taking into account context of report and common sense of course.
Follow the below steps:
Use your DataSet1 in your matrix as suggested approach from LINK.
Your query would be similar to below:
Select StuId,StuSubject
'StuSubject' + Cast(row_number() over (partition by id order by score) as INT) StuSubjectVal
from table
You should get the result like below (after using matrix).
StuId StuSubject1 StuSubject2 StuSubject3......
Add a column to your matrix, and use the Lookup from DataSet1 to DataSet2.
=Lookup(Fields!StuId.Value, Fields!StuId.Value, Fields!StuName.Value, "DataSet2")
Have not tested it, but it should work.

ssrs count error getting 1's instead of total

Rookie mistake,
here it is:
I got Regions and MembersID, I grouped the regions and want to count the members. When I add the count(MembersID) instead of geeting the total members, I get a bunch of 1:
RegionA 1
1
1
RegionB
1
1
what I want:
RegionA
3
RegionB 2
what I did: Grouped Regions and Count[MembersID]
Please help!
It's a bit tough to tell what you actually need here.
In Group Header rows, you can use aggregate functions, which will look at all rows in that current scope.
Distinct MembersID count in group: =CountDistinct(Fields!MembersID.Value)
Total rows in group: =CountRows()
In detail rows, these aggregate functions will only report on the current rows, so =CountRows() will only ever return one; to get the number of rows in the group you need to set the scope of the function to the group level, e.g. something like =CountDistinct(Fields!MembersID.Value, "Group1") or =CountRows("Group1").
So based on a Dataset:
And a report which incorporates these aggregate function in the group header and detail rows:
You can get the following results:
If this is not sufficient you will need to supply your Dataset details, some sample data and how you need this sample data presented.