Using condition to set SSRS matrix column name - reporting-services

I was trying something new, using matrix and only want to see the recent two years and i made 2 columns. The first column had this expression '=MAX(Fields!Year.Value)-1' and the second column has the expression '=MAX(Fields!Year.Value)'. I expected to only see 2 year columns but it shows all of them and on the last column it sums up all the orders. See the photos below:
In the design view my matrix looks like this:
The output in report is:
The problem is that year 2007 only exists in another year and month, but i dont know why it shows up here. see the image next to see the original data.
The original data is:
And I just want to see the recent 2 years and ignore the rest of the years, like follows:
Thanks in advance.

You are adding each year order, instead you have to add only those that correspond to your column year. For previous last year you have to sum only its orders values.
Use these expressions to Sum only the two recent years orders:
Previous last year:
=Sum(
iif(Fields!Year.Value=Max(Fields!Year.Value)-1,Fields!Total_Ord.Value,0)
)
Last year:
=Sum(
IIF(Fields!Year.Value=MAX(Fields!Year.Value),Fields!Total_Ord.Value,0)
)
UPDATE: Don't group by year and just add two columns under the Month column group.
This is the matrix you should have:
It will preview:
Let me know if this can help you.

Related

how do i group customer entries using months across different years in ssrs reports in nav

I am trying to get Sum of each customer ledger entries and group the Ssrs report into months across different years. For instance, I should be able to filter with the year 011117..310318, this should give me entries of each customer grouped per month: November, December, Jan(18), Feb, Match.
You can do this by using a matrix in your report.
Add the matrix and set the row group to group by Customer (whatever makes the customer unique such as an ID).
Add column groups by month and then a parent column group by year. If you don't have separate columns for months and years in your dataset, you can use expression into the group expression such as..
=Month(Fields!myDateField.Value)
=Year(Fields!myDateField.Value)
In the data 'cell' just drag the value you want to aggregate there.
That should give you a basic working matrix.
If you need more help refer to the SSRS documentation on the matrix control.
https://learn.microsoft.com/en-us/sql/reporting-services/report-design/create-a-matrix-report-builder-and-ssrs?view=sql-server-ver15

Count the number of columns in a column group

I need to report the average number of customer visits each engineer makes per month.
My SQL query creates a temporary table of months between the start date and end date and left joins this to the main data table, to ensure rows are returned even for months where no visits were made.
So an example of the data returned from SQL may be:
My report has two column groups, one for year and one for month, and I have a row group for the engineer.
For this report, the date is always returned as the first of the month, even though the actual visit could be on any date.
At the end of each year there is a cell which contains Count(Customer) and totals the number of visits the engineer made in that year. I would also like to have a cell which displays the average number of visits made each month in that year.
For a complete year I could simply divide by 12. However for a partial year I need to count the number of month columns for that year.
I tried CountDistinct(Month) but this only counts months where at least one visit was made, making the monthly average incorrect.
How can I get a count of the number of columns in a column group, including columns with no data?
Thanks.
The way I would do this would be to add a column into your temporary dates table that had the number of months selected in it.
You could do this by either counting the months in the temp table then appending the value to it or, if the dates table contains more than just months then work it out based on the parameters you pass in.
For example
SELECT *, DATEFIFF("m", #startDate, #endDate) as NoOfMonths
INTO #myTempDateTable
FROM myDateTable
WHERE etc...
Then in SSRS you can simply divide your total by this number.

Calculating average in SSRS Matrix Table alongside filtered columns to separate out current versus previous years

I have a dataset, returned from a SQL query, that has the following data:
TYPE_CODE
YEAR
The data set spans the years 2014 through 2019. The TYPE_CODE has 6 different values.
How do I setup an SSRS matrix to provide the following layout and data:
So far I have a matrix setup (see the pic below) that has a row group (TYPE_CODE1) for the TYPE_CODE data, and two column groups (YEAR_PREV and YEAR_CURRENT) that are filtered as follows:
- The second column in the matrix is the YEAR_PREV group, and is filtered to not show 2019 data (YEAR <> 2019)
- The 4th column in the matrix is the YEAR_CURRENT group, and is filtered to only show 2019 data (YEAR = 2019)
This method correctly splits my data, with the green highlighted columns in the pic below representing what is correct:
What is not correct is the average column, as I cannot figure out how to setup that column to only average the columns to the left (the previous years - 2015-2018) and not include the column to the right (2019).
I have tried several different expressions to no avail, primarily trying to limit the count function to only the YEAR_PREV group, like so:
=count(Fields!TYPE_CODE.Value, "PREV_YEAR")/4
This throws an error telling me that something along the lines of "group cannot be used in aggregate function...".
How do I calculate the average column correctly?
You need to have 2 column groups with filters applied, first one where year equals 2015-2018 and the second one equals only 2019.
In between each of these columns you need insert your average column that will reflect years 2015-2018.

SSRS Sum values by week,month,and year

Report value of stock
I've created a stock value report.
The problem occurs with the addition of columns (month, year). I would like to set it to show the value of the last week in the month and year.
After reading your comment I think I know the best way for you to get there.
You want only the value of the last week within a Year/Month/Week column group. I would keep all the groups the way you have them setup with two small tweaks.
Drop the detail of the week group, keep the group, just no detail column group band.
Add a column group footer to the week group and simply apply the value as
=Last(Fields!WeekValue.Value)
That way you should have Year/Month and one column for weeks, the footer column and you simply pull across the last value from within that group.

How to show all legend keys for 0 or null value results, in SSRS?

I'm trying to build a bar chart that displays Events per Year. Each year divided into 4 quarters.
The problem arises in case that one, or more, quarter doesn't have any events in any year. In this case the legend key for this quarter doesn't appear.
For example, if no events took place in 1st quarter, then the legend show keys for 2nd, 3rd and 4th quarters only.
I need to make the legend always shows all keys, even if one, or more, doesn't have any values.
How can I accomplish this??
A similar question is here but the answer doesn't solve the problem.
Any help is appreciated.
Applying #alejandro answer, I get this result.
The missing quarter, Quarter 3, is renamed to Series1 and located at the first place. Any suggestions?
There is no way to build the legends and labels based on data that is not present in your fields.
You can create a table or a CTE with all quarters, then use LEFT JOIN operator to relate each row in your data with the corresponding quarter, if a quarter doesn't match any row in your data it will return null for each column in your data but will include the quarter, which lets SSRS build the legend.
WITH quarters
AS (SELECT 1 [Quarter]
UNION
SELECT 2
UNION
SELECT 3
UNION
SELECT 4)
SELECT a.[quarter],
b.*
FROM quarters a
LEFT JOIN YourDataTable b
ON a.[quarter] = b.[quarter]
After that you can use Quarter field in your Chart and it will show all quarters in the legend even if there is no data in one or more quarters.
Let me know if this helps.