I have a report which grabs a whole bunch of statistical data that represent system alarms.
I've made a drilldown style report by pulling back all the stats in a user selected date range and then I added a group on my alarm's codeId so I would have something to group on.
What I would like to do is have a count of the number of items within these groups by codeId. I plan to use the count as part of a concatenated string that makes up the whole group header. It basically says "Alarm Code # {codeId} - {Description} - Total: {Total}".
My issue is that to do a count in my query then I have to pre-group the data before it hits the report. And if I do that then I do not have my detail data to expand the group on. And running a summary and a detail dataset is a bit over my head but an option I might have to look into.
So I was hoping I could use a group variable to count the number of items contained within the group. Can anyone tell me a rough set of steps to try to pull that off?
Seems like using a Group variable here would make this more complicated than it needs to be.
Since you already have a group in SSRS, you can use the CountRows function. Put this expression into a placeholder, for example:
="Alarm Code #" & Fields!CodeID.Value
& " - " & First(Fields!Description)
& " - Total: " & CountRows()
Or, depending on size, you can add a correlated subquery to your SQL query:
SELECT
CodeID,
MyField,
MyOtherField,
(SELECT COUNT(*) FROM myTable t1 WHERE t1.CodeID = t2.CodeID) AS MyRowCount
FROM
myTable t2
This is not particularly efficient SQL, so if you are running this on hundreds of thousands of rows or more, I'd go with the first approach.
Related
I am new to SSRS and have a SUM(IIF question.
My data set contains four columns: Date, GroupID, PlanPaid, and NetworkIndicator.
Here is an example of the data set:
I am trying to SUM the [PlanPaid] amount when [NetworkIndicator] = "In Network".
However, I need this amount broken up by the [Date]. I tried accomplishing this by creating the expression:
=Sum(IIf(Fields!NetworkIndicator.Value = "In Network"
, Fields!PlanPaid.Value
, Nothing)
, "Claims_Rolling12")
But this expression returns the same amount (total) across all [Dates]. How do I break it up so that it is grouped by the correct [Date]?
Here is a photo of my Tablix and my current Groups: [Tablix and Groups]
And here is a photo of the output: [Output]
You haven't said where you want this sum to appear, so the answer here might not work. If it doesn't then edit your question to show what you expect the output to look like based on your sample data.
I'm assuming here that you want to add a new column to the report that shows "In Network total" by date.
The easiest way to do this is to add a row group that groups by date, then within this group you can use a simple expression, like the one you tried, but without specifying the scope.
=SUM(IIF(Fields!NetworkIndicator.Value = "In Network", Fields!PaidPlan.Value, Nothing))
This expression will only sum rows that are within the current scope, in this case the scope will be the row group you created to group by dates.
As IO said, if this is not helpful, edit your question and show what you expect your end result to look like, based on the sample data you supplied and then I can look at it again.
I want to build a SSRS report that has column as week numbers - 8 weeks for 8 columns starting with current. This report is run every week and current week number is set then. So both column names and their values should change .Is it possible to build something like this in SSRS?
I tried doing this with a dynamic SQL based stored proc in dataset. However for every run I don't even see the columns values updating dynamically
Here's an example :
Also I am trying to avoid these week numbers as row values and then using matrices
My stored proc looks something like this
declare #n tinyint = datepart(wk, getdate())
declare #n1 tinyint = (#n+1), #n2 tinyint =(#n+2), #n3 tinyint =(#n+3), #n4 tinyint =(#n+4), #n5 tinyint =(#n+5), #n6 tinyint =(#n+6)
exec ('Select b.sku, b.['+#n+'], b.['+#n1+'], b.['+#n2+'], b.['+#n3+'], b.['+#n4+'], b.['+#n5+']...
Will appreciate any help in this direction.. many thanks!
When working with SSRS it's generally best to avoid dynamic SQL and pivoting the data in the SQL. Use the SQL to get the raw data you need and then let SSRS do the pivoting and aggregation. This way you take advantage of what they each do best. I know you said you want to avoid matrices, but it is the best way to make the report dynamic.
So you should either return all the data in one dataset and use filters on your matrices OR write two queries and have each one populate a matrix. BTW a matrix is just a table with a column group added, so don't be intimidated by them.
There are 2 ways to do this with a standard tablix.
Calculate the column headers as expressions using concatenation of Wk and some date math to find the correct week number and return the same sort of thing from your query (e.g. columns are current_week, week_minus_1, week_minus_2...)
Return the column headers as additional columns in your query that are the same value for every row (e.g. ColHeader0, ColHeader1...). Your data columns would still be relative weeks (e.g. ValueWeek0, ValueWeek1...). In your report the column header would have an expression like =First(Fields!ColHeader0.Value). This is a more flexible approach since it lets you pick 8 historical weeks instead of only the last 8 weeks if you add a parameter.
EDIT - Clarifications
The reason that you get the blank column Wk48 is approximately that you have created your report looking for that column that won't be there the next time. SSRS looks for exact columns. You should you use relative column names for either of the options I have specified:
exec ('Select b.sku, b.['+#n+'] as Wk0, b.['+#n1+'] as Wk1, b.['+#n2+'] as Wk2, b.['+#n3+'] as Wk3, b.['+#n4+'] as Wk4, b.['+#n5+'] as Wk5...
This will allow you to populate the aliased Wk0 column with the appropriate current week data and still make sure that it can be consistently referenced as the base week by SSRS.
To change the column headers you can:
Independently calculate the week numbers in SSRS in the column header expressions: ="Wk" + CStr(<correct week calculation>).
Return the column headers in the result set and access them in the column header expression:
exec ('Select b.sku, b.['+#n+'] as Wk0, b.['+#n1+'] as Wk1, b.['+#n2+'] as Wk2, b.['+#n3+'] as Wk3, b.['+#n4+'] as Wk4, b.['+#n5+'] as Wk5..., ''Wk'''+#n+' as ColHeader0, ''Wk'''+#n1+' as ColHeader1...
and reference the returned column headers from the SSRS column header expression as =First(Fields!ColHeader0.Value).
Here's a solution that worked for me:
Create parameters (say CurrWk, CurrWk1) ,set as hidden and store 'Default value' and 'Available value' equals to current week number (datepart(wk, now()) and any subsequent week by doing a +1, +2, +3.. etc.
Write a query expression . Click onto fx beside dataset query space and write the select query for your program embedding parameter values in the expression window. For eg ="Select SKU, [" & Parameter!CurrWk.Value & "] as Wk1,
[" & Parameter!CurrWk.Value & "] as Wk1 from Sales_Table"
Before passing this query as a 'command text expression' please ensure this query is working in sql ssms.
Save the expression. Now find 'Fields' tab on the left hand side panel.You need to map the fields manually from the query here. If this is not done, there is a very high chance you seean empty field list and wont be able to access them at all. This may be because ssrs do not store query metadata directly from expressions.
You can avoid part of the issue by having atleast the static fields , for example here SKU listed in the 'Fields' list by first running a sql query with static field(select SKU from Sales_Table ). You can then go back to update dataset- change query to expression and embed the parameterized field names.
Map field names. In this example I chose 'Query Type' fields and set Field names as SKU, CurrentWeek, NextWeek and mapped to source SKU, Wk and Wk1 respectively.
Click on 'Refresh Fields' at the bottom. Now you have a dataset with the complete field list. Use these in charts, tables . Run it every week and note the numbers changing as expected.
In case you are using this dataset in a table, make sure you set headers with Labels of Parameters (for eg here I did =Parameters!CurrWk.Label for col with current week data)
That's it!
I have written a Query in MSBI Query Editor as follows,
SELECT
DATEPART(year,tblReservation.Date) AS "Year",
tblReservation.Room AS "Room No.",
COUNT(tblReservation.Room) AS "No. Of Times",
CASE
WHEN PaidFor = 1
THEN
COUNT(tblReservation.Room)*tblPrice.Price
ELSE 0
END AS "ActualRevenue",
COUNT(tblReservation.Room)*tblPrice.Price AS "TargetRevenue"
FROM tblReservation INNER JOIN tblRoom
ON tblReservation.Room = tblRoom.RoomNumber
INNER JOIN
tblPrice ON tblRoom.PriceID = tblPrice.PriceID
GROUP BY tblReservation.Room, tblPrice.Price,
tblReservation.PaidFor, DATEPART(year,tblReservation.Date)
This Query is showing me output like this(Matrix Layout MSBI),
My Query output is as,
My Report design layout is,
What I want is, in my Matrix layout, the room no. 100 is two times, for year 1999 & 2013.
It is occupying two rows. I want this in one row in my Matrix layout.
How should I do ?
Thanks.
It's difficult to tell what's going on with your matrix without more details, but this should all work fine without any special effort.
Take a subset of your data:
Create a standard matrix, adding a few extra columns to to the column group:
You can see the row group is based off RoomNo and the column group is based off Year. The data cells are just aggregated values. This works as expected for room 100:
Without more detail it's impossible to tell what's causing your issue, but the above shows this is certainly possible with everything as standard.
In your case I would try:
Checking the row groups details and see if it is grouped on anything other than RoomNo; same for the column group and Year.
Create a new text matrix from scratch with the above grouping and see if this new item is working as expected.
As the title says, I'm looking for a way to calculate the percentage of completed tasks a person has. Data is structured so that each individual has can have many tasks, and each task can have many sub-tasks, as described below:
Employees
(pk)(AutoNumber)employee_id
(Text)FirstName
(Text)LastName
Tasks
(pk)(AutoNumber)task_id
(fk)(Number)employee_id
(Memo)Description
(Yes/No)Completed
Sub-Tasks
(pk)(AutoNumber)subtask_id
(fk)(Number)task_id
(Memo)Description
(Yes/No)Completed
I've been trying to make a report (named "Task Completion Rates") that lists all employees, the number of completed tasks they have, the total number of tasks they have, and their completion rate as a percentage. At the moment, I have the following two queries:
SELECT Count(employee_id) FROM [Tasks] AS TotalTasks WHERE [Tasks].employee_id =
Reports![Task Completion Rates]!txt_employeeID
SELECT Count(employee_id) FROM [Tasks] AS CompletedTasks WHERE [Tasks].employee_id = Reports![Task Completion Rates]!txt_employeeID AND [Tasks].Completed = 1
Are both of those necessary, or is there a way to get both counts from one query? Also, how would I go about using those totals in the report? The report's Record Source is set to the Employees table so that it can list them all. I've got text boxes ready to go for each total, but I'm having problems using the expression builder in the source for each text box to actually display the results of the queries. I set the source for one of the text boxes to =[qry_TotalTasksPerEmployee]![TotalTasks] (via expression builder) but it keeps prompting me to enter a value for [qry_TotalTasksPerEmployee]. Any ideas on how get this working?
Try taking the mean of your completed value. For example:
SELECT employee_id, -avg(Completed) FROM [Tasks] GROUP BY employee_id
The - before avg is because true is stored as -1, as pointed out by #Neil.
I'm not sure what you mean about the report - it should be easy to place query results in a report.
I'm not really into msaccess reports but since completed seems to be either a 0 or a 1 you should work towards the following query
-Edit turns out it's either 1 or -1 for bools so can't do a sum(completed) but need an IIF()
Select employee_id, count(*), sum(IIF(completed,1,0)), sum(IIF(completed,1,0))*100/count(*) as rate
From employees
Left join tasks
On tasks.employee_id = employees.employee_id
Group by employee_id
Note that this does create a division by zero problem for employees without tasks. You will have to deceide how to deal withbthose (exclude, show as 0% or ...)
I have the following code attached to a text box in a form:
=DSum("[subform].Form![POINTS]","ATTENDANCE","[subform].Form![EMPLOYEE NO] = [EMPLOYEE NO]")
Ideally this would yield the total amount of points accrued by the employee we are currently searching for. However, what I am getting is the total sum of rows in my table.
Does anybody have any idea of how I could get the total sum of the values instead of the number of rows?
Thanks
If you want to get the total from a subform, and your subform in in sync with the main one, it will be much more efficient to procede this way:
create txtTotalPoints textbox = sum(Points) in the footer of your subform
refer to that control from your main form: txtMainResult =subform!form!txtTotalPoints
Hide txtTotalPoints (or the footer itself)
That will generally be much faster.
As far as I know, the Domain functions such as DSum, DLookup, DCount etc. are used to lookup and return values from a table. The first argument is the field, the second the table, and the third is the criteria or WHERE statement that makes sure you get the correct set of records. Your first argument refers to a form's field. I think this is incorrect. Your first item in your WHERE statement is also a form field. I this this is also incorrect. You need to try something like this instead:
=DSum("POINTS","ATTENDANCE","[EMPLOYEE NO] = " & [subform].Form![EMPLOYEE NO])