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

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

Related

Display Measure depending on Date, combine two measure from different dates

Background:
I am preparng a dataset for a report.
To simplify lets say it is two measures, one Category and a time dimension.
The main Report Parameter is todays date.
I would like to see the first Measure from yesterday (Value 000) and the second measures
from the day before yesterday (Value 001). Because previous days data is not available yet for Value 001.
Lets assume 2020-04-27 is today (passed on from the Reporting Tool SSRS).
AS-IS
SELECT {[Measures].[Value 000], [Measures].[Value 001]} ON COLUMNS
, ( {[DIM Category].[Category].&[1], [DIM Category].[Category].&[2]},
[DIM Date].[Y-M-D ISO].[Date].[2020-04-27].LAG(2): [DIM Date].[Y-M-D ISO].[Date].[2020-04-27].LAG(1)
)
ON ROWS
FROM [My_Cube]
Output:
[Value 000] [Value 001]
Category 1 2020-04-25 88 16
Category 1 2020-04-26 89 (null)
Category 2 2020-04-25 90 14
Category 2 2020-04-26 92 (null)
I tried two MDX-Queries for each date and combined the Sets in the Reporting tool but I am wondering if there is a comfortable way to do that in one MDX query.
The following would be the desired output.
To-Be
Output:
[Value 000] [Value 001]
Category 1 89 16 (Value from day before, for Value 0001)
Category 2 92 14
The Date doesn't need to be shown in the output (but can be)
You can solve the issue by using calculated measures. Follwoing example is based on adventureworks . I want to rebuild your senario for Jan 23 2013
The initial query is there to show values for yesterday abd day before
with
member measures.Yesterday
as
sum([Date].[Date].currentmember.lag(1),[Measures].[Internet Sales Amount])
member measures.DayBefore
as
sum([Date].[Date].currentmember.lag(2),[Measures].[Internet Sales Amount])
select
{
[Measures].[Internet Sales Amount],measures.Yesterday ,measures.DayBefore
}
on columns,
{
([Product].[Category].[Category],[Date].[Date].&[20130120]:[Date].[Date].&[20130123])
}
on rows
from
[Adventure Works]
where [Date].[Date].&[20130123]
Result
Now lets remove the date from the rows and put it in where
with
member measures.Yesterday
as
sum([Date].[Date].currentmember.lag(1),[Measures].[Internet Sales Amount])
member measures.DayBefore
as
sum([Date].[Date].currentmember.lag(2),[Measures].[Internet Sales Amount])
select
{
[Measures].[Internet Sales Amount],measures.Yesterday ,measures.DayBefore
}
on columns,
{
([Product].[Category].[Category])
}
on rows
from
[Adventure Works]
where [Date].[Date].&[20130123]
Result

Average of MS SSRS Tablix group totals

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.

SSRS 2 column groups in matrix, 1 row reads from 1, the rest read from the other? (pls help)

Is there a way in SSRS to have an additional row within your row group, to look at a different column group than the rest of the row group
Let's say I have STATES, SALES, MONTH, and BUCKET_MONTH as my dataset fields BUCKET_MONTH is already calculated for me, based off of the MONTH. I want to show something like this:
SAMPLE DATA LIKE THIS FOR FLORIDA (and other months but BUCKET_MONTH only matters for florida let's pretend)
STATE MONTH SALES BUCKET_MONTH
FL JAN 50 FEB
FL FEB 125 FEB
FL MAR 100 MAY
FL APR 0 MAY
FL MAY 100 MAY
SSRS MATRIX MIGHT LOOK LIKE THIS: ?
| 2 groups ?
| MONTH
| BUCKET_MONTH (I can hide this header)
-----------------------------------
1 col group|
STATE | SALES
BUCKET | SALES <-- this row is only visibile for FL which I know how to do
EXPECTED RESULTS WOULD LOOK LIKE THIS
JAN FEB MAR APR MAY JUN JUL
---------------------------------------------------------------------
CA 100 300 150
FL 50 125 100 0 100
FL BUCKET 175 200 <-- BUCKET_MONTH**
MA 0 200 250 50
BUCKET_MONTH in ds shows FEB for the rows with Jan,Feb MONTH, and shows MAY for Mar,Apr, May MONTH
Is there a way to do this in SSRS? Where one of the rows looks at a different column group to establish what column to put the SUM of SALES in?
Much appreciation in advance!
You have to add BUCKET_MONTH as parent column group in your matrix.
Add BUCKET_MONTH in the Column Groups pane, then delete the created row in the matrix selecting Delete groups only option. Now add MONTH as child group in column groups pane.
Add STATE in rows group pane and add a row for bucket total.
Use this expression for BUCKET TOTAL:
=IIF(
Fields!BUCKET_MONTH.Value=Fields!MONTH.Value,
SUM(Fields!SALES.Value,"BUCKET_MONTH"),
Nothing
)
It should produce:
UPDATE: Expression updated taking in account that MONTH and BUCKET_MONTH fields are actually dates.
=IIF(
UCASE(format(Fields!BUCKET_MONTH.Value,"MMMM yy"))=
UCASE(format(Fields!MONTH.Value,"MMMM yy")),
SUM(Fields!SALES.Value,"BUCKET_MONTH"),
Nothing
)
Let me know if this helps.

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!