Average of MS SSRS Tablix group totals - reporting-services

I have a SQL Server Report Builder tablix report that evaluates sales activity over time.
Rows by company, columns are grouped by date. Something like this:
2015 2016 2017
Company1 10 12 1
Company2 6 5 0
Company3 8 10 7
(The report also expands columns into months)
I would like to add a column, or color a background, calculated based on the average of each year's totals. For example, Company1 averages 7.6/year if I include 2017. I would like to be able to say that 2015 was 131% of average, 2016 was 157% of average, and 2017 is 13% of average. Bonus points if I can exclude the current year from the average.
The result might look something like this:
2015 2016 2017
Company1 10 (131%) 12 (157%) 1 (7.6%)
Company2 6 (%%) 5 (%%) 0 (%%)
Company3 8 (%%) 10 (%%) 7 (%%)
Since the source data has one sale per row, and the tablix is what's creating the grouped count by date, I can't seem to just run an average, which just gives me "1", due to the fact that I'm counting on a count column. The source data looks something like this:
CompanyName Date SalesRep Amt Count
Company1, 1/1/2015, salesrepname, 50000, 1
Company1, 2/1/2015, salesrepname, 20000, 1
Company1, 3/1/2015, salesrepname, 50000, 1
Company1, 4/1/2015, salesrepname, 10000, 1
Company1, 5/1/2015, salesrepname, 5000, 1
...
How do I go about getting the average of each year?

If you were just grouping on Company and Year you could override the scope of your aggregates with a group name. However, SSRS doesn't have a way to specify combinations of groups. So in your case you will need to make those sub-calculations available another way. It is usually best to do that in the SQL. You can either add a subquery to your existing query (preferred) or add an additional dataset. If you use a separate dataset you'll also have to match up the values with a Lookup function.
If you try to come up with an elaborate workaround like custom code or referencing textboxes it is going to become difficult to maintain and will be very inefficient.

wouldn't a formula like this work?
=sum(Fields!count.Value)/
(sum(Fields!count.Value,"Year")/countdistinct(Fields!CompanyName.Value,"Year"))
assuming your column group name is Year.

Related

How to sum specific rows and columns in SQL?

pnr mnd pris
1 1 600
1 7 900
2 1 600
2 7 600
3 1 40
3 7 40
I have trouble how to sum specific rows on the columns. Looking at the above, the table is called travel and it has 3 columns:
pnr - Personal Number
mnd - Month
Pris - Price
So what I want is to sum total of the price for the a specific month, so in this case, it should be 1240 USD and month 1. For the month 7, it should be 1540 USD.
I have trouble to do the query correct. So far from I have tried is this:
SELECT t.rnr, t.mnd, SUM(t.pris)
FROM travel AS t
WHERE t.mnd = 1
The result I get is 3720 USD which I have no idea how the SQL managed to calculate this for me.
Appreciate if someone could please help me out!
For this you need to drop the pnr column from the output (it is not relevant and will cause your data to split) and add a GROUP BY:
SELECT t.mnd, SUM(t.pris)
FROM travel AS t
WHERE t.mnd = 1
GROUP BY t.mnd
Live demo: https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=b34ec2bb9c077c2d74ffc66748c5c142
(The use of an aggregate function without grouping, as you've got now, is not a standard SQL feature and can often be turned off in MySQL. If turned on, you might not always get the result you expected/intended.)
just group your result with mnd column
SELECT t.mnd, SUM(t.pris)
FROM travel AS t
group by t.mnd

Need To Calculate Percentage Difference In ssrs with multiple row and column Grouping

I have row and column grouping.
I need difference and percentage difference for each month as shown below:
Example:
march diff
2013 2014
Hyderabad 12 15 15-12=3
Like this result I want
and also I need percentage difference ---- (This Year – Last Year) / Last Year
I typically try to handle this in the SQL and make the reports dumb. In your case, I would make the result set look something like this:
City MonthID Month YearDiffID YearDiff Value
--------- ------- ----- ---------- -------- -----
Hyderabad 3 March 2013 2013 12
Hyderabad 3 March 2014 2014 15
Hyderabad 3 March 9998 Diff 3
Hyderabad 3 March 9999 % Diff 0.25
Your Row Group would be City. Your Column Group 1 would group/sort on the MonthID integer, and display the Month string. Your Column Group 2 would group/sort on the YearDiffID integer, and display the YearDiff string.
If you provide your SQL, I can help you get the result set to look like this.
If you insist on calculating this in the report and all you have is City, Date, and Value fields, your expressions will look something like this:
Diff calculation, assuming you are grouping by City (Row) and Month (Column 1) and this calc is outside the Year (Column 2) Grouping and have added a Parameter for #ThisYear so this report will keep working in future years: =sum(iif(Year(Fields!Date.Value) = #ThisYear,Fields!Value.Value,nothing)) - sum(iif(Year(Fields!Date.Value) = #ThisYear-1,Fields!Value.Value,nothing))
% Diff calculation, with the aforementioned assumptions: =(sum(iif(Year(Fields!Date.Value) = #ThisYear,Fields!Value.Value,nothing)) - sum(iif(Year(Fields!Date.Value) = #ThisYear-1,Fields!Value.Value,nothing)))/sum(iif(Year(Fields!Date.Value) = #ThisYear-1,Fields!Value.Value,nothing))

To calculate sum of the fields in a matrix with column grouping

I am working on a ssrs report with column grouping. the followin is my scenario.
Matrix 1:
ID 2012 2013
1 20 40
1 30 50
Total 50 90
Matrix 2:
ID 2012 2013
1 60 70
1 60 80
Total 120 150
I need the sum of matrix1 and matrix2 like below:
ID 2012 2013
1 170 240
But I got the result like :
ID 2012 2013
1 410 410
I have applied column grouping in all the 3 matrices and gave the expression to get sum for matrix 3 as: =Sum(Fields!amount1.Value, "dsmatrix1") + Sum(Fields!Tamount1.Value, "dsmatrix2")
Please help me to get a solution for this.
Thanks!
I think I know what's going on. Correct me if I'm wrong.
Based on what I'm seeing, I'm guessing that Matrix 1 and Matrix 2 only have three fields each, an ID field, an amount field (being "amount1" or "Tamount1"), and a year field.
Your column grouping is manipulating the display of the data to show all values broken out by year. This works fine when looking at data from a single dataset. However, your formula is specifying that the sum of everything in the Amount1 field of dsmatrix1 and the Tamount1 field of dsmatrix2 should be added. This does not take into account the column grouping. Your expression is essentially taking all of the values from both datasets and adding them together.
Not knowing more about your query structure or how the data is filtered, my best guess is that you need another SQL dataset. In this case, you would take the queries from your two previous datasets and union them with the "Union All" command. Note that you will want to use Union All and not just Union. More on that here: What is the difference between UNION and UNION ALL?
Your end result should look something like this:
--This will be your dsmatrix1 query copied and pasted
Select ...
Union All
--This will be your dsmatrix2 query copied and pasted
Select ...
--Place one single Order by clause at the bottom
Order by ...
Note: for your two queries to be unioned properly, you'll need to make sure that each have the same number of fields, each with the same data types. Then you can point your third matrix to the new dataset.
Hope that helps!

ssrs find count of multiple non-null columns in a row

I need some help with this issue in SSRS to find the YTD(%) for the YTD columns..
I have SSRS 2008 tablix region where the columns are State,Category,January, February, March,...,December, YTD. These columns have both the sums and the percentage values depending on the row. I need help to calculate the YTD when the column is %.
example:
State Category January February March...... YTD
MN A 200 200 0...... 400
MN B 21 12 0........ 33
MN A% 12.5 12.5 0........ ??
For A,B above, the YTD is simply adding the report items January+February+March+.... which will be 400 and 33 as shown(assuming only Jan and Feb have real values > 0).
For the row A% above(??), since this a percent column, my YTD should be the average of the non-null/non-zero columns...in this case (12.5+12.5)/2=12.5 but I am unable to find out the count of the non-null columns for each category (January, February, etc)...I have tried avg,count, sum, etc but i am not getting the correct value. If I can figure out the denomintor(which will be the count of the non-null columns in January, February,...,December), the problem will be solved as I can then do:
( Jan+Feb+March+...+Dec)/Count(Jan,Feb,Mar,..,Dec)
The Count should return me the count of the non-null/non-zero columns only so I can do the average calculation..please help.
I am not sure what you want.
If all that you want is to find out average of a row in the last column you can do this:
Make the expression of the column in which you want average like this:
Sum(Fields!Jan.Value + Fields!Feb.Value...+Fields!Dec.Value)/
Sum(IIF(IsNothing(Fields!Jan.Value),0,1) +
IIF(IsNothing(Fields!Feb.Value),0,1) +
.
.
IIF(IsNothing(Fields!Dec.Value),0,1)
)
so, in the denominator, you are doing this:
Sum(1+0+1+1+0...+0)
i.e. 1 is returned if Field is not null, otherwise 0

MS Access: Using Single form to enter query parameters in MS access

compliment of the day.
Based on the previous feedback received,
After creating a Ticket sales database in MS Access. I want to use a single form to Query the price of a particular ticket at a particular month and have the price displayed back in the form in a text field or label.
Below are sample tables and used query
CompanyTable
CompID CompName
A Ann
B Bahn
C Can
KK Seven
- --
TicketTable
TicketCode TicketDes
10 Two people
11 Monthly
12 Weekend
14 Daily
TicketPriceTable
ID TicketCode Price ValidFrom
1 10 $35.50 8/1/2010
2 10 $38.50 8/1/2011
3 11 $20.50 8/1/2010
4 11 $25.00 11/1/2011
5 12 $50.50 12/1/2010
6 12 $60.50 1/1/2011
7 14 $15.50 2/1/2010
8 14 $19.00 3/1/2011
9 10 $40.50 4/1/2012
Used query:
SELECT TicketPriceTable.Price
FROM TicketPriceTable
WHERE (((TicketPriceTable.ValidFrom)=[DATE01]) AND ((TicketPriceTable.TicketCode)=[TCODE01]));
In MS Access, a mini boxes pops up to enter the parameters when running the query. How can I use a single form to enter the parameters for [DATE01] and [TCODE01]. and the price displayed in the same form in a textfield (For further calculations).
Such as 'Month' field equals to input to [DATE01] parameter
'Ticket Code' equals to input for [TCODE01] parameter
Textfield equals to output of the query result (Ticket price)
If possible, I would like to use only the Month and Year in this format MM/YYYY.The day is not necessarry. How can I achieve it in MS Access?
If any question, please don't hesitate to ask
Thanks very much for your time and anticipated feedback.
You can refer to the values in the form fields by using expressions like: [Forms]![NameOfTheForm]![NameOfTheField]
Entering up to 300 different types of tickets
Answer to your comment referring to Accessing data from a ticket database, based on months in MS Access)
You can use Cartesian products to create a lot of records. If you select two tables in a query but do not join them, the result is a Cartesian product, which means that every record from one table is combined with every record from the other.
Let's add a new table called MonthTable
MonthNr MonthName
1 January
2 February
3 March
... ...
Now if you combine this table containing 12 records with your TicketTable containing 4 records, you will get a result containing 48 records
SELECT M.MonthNr, M.MonthName, T.TicketCode, T.TicketDes
FROM MonthTable M, TicketTable T
ORDER BY M.MonthNr, T.TicketCode
You get something like this
MonthNr MonthName TicketCode TicketDes
1 January 10 Two people
1 January 11 Monthly
1 January 12 Weekend
1 January 14 Daily
2 February 10 Two people
2 February 11 Monthly
2 February 12 Weekend
2 February 14 Daily
3 March 10 Two people
3 March 11 Monthly
3 March 12 Weekend
3 March 14 Daily
... ... ... ...
You can also get the price actually valid for a ticket type like this
SELECT TicketCode, Price, ActualPeriod AS ValidFrom
FROM (SELECT TicketCode, MAX(ValidFrom) AS ActualPeriod
FROM TicketPriceTable
WHERE ValidFrom <= Date
GROUP BY TicketCode) X
INNER JOIN TicketPriceTable T
ON X.TicketCode = T.TicketCode AND X.ActualPeriod=T.ValidFrom
The WHERE ValidFrom <= Date is in case that you entered future prices.
Here the subquery selects the actually valid period, i.e. the ValidFrom that applies for each TicketCode. If you find sub-selects a bit confusing, you can also store them as query in Access or as view in MySQL and base a subsequent query on them. This has the advantage that you can create them in the query designer.
Consider not creating all your 300 records physically, but just getting them dynamically from a Cartesian product.
I let you put all the pieces together now.
In Access Forms you can set the RecordSource to be a query, not only a table. This can be either the name of a stored query or a SQL statement. This allows you to have controls bound to different tables through this query.
You can also place subforms on the main form that are bound to other tables than the main form.
You can also display the result of an expression in a TextBox by setting the ControlSource to an expression by starting with an equal sign
=DLookUp("Price", "TicketPriceTable", "TicketCode=" & Me!cboTicketCode.Value)
You can set the Format of a TextBox to MM\/yyyy or use the format function
s = Format$(Now, "MM\/yyyy")