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

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.

Related

Is it possible to limit RunningValue to look back for a certain number of rows?

Im trying to average the past 5 rows in my table i created in SSRS grouped by date(Monday of every week). Ive tried runningValue however it looks back at all the past rows for each group. Is there a way to limit the scope to just the past 5 rows or weeks for each Date group.
Thanks
I would accomplish this with grouping. I don't know what your dataset is like but I assume it is a SQL query you can modify. The easiest solution would be to add a week number column to your query. For example:
SELECT datepart(week, YOURDATE) as WeekNumber
More info on datepart:
https://learn.microsoft.com/en-us/sql/t-sql/functions/datepart-transact-sql?view=sql-server-2017
Once you have a week number, use the table creation wizard in Report Builder and add WeekNumber as a row group. This will group your values by week number and give you a total under each week. You can change the total by double clicking and making it AVG() instead of SUM().
Note: If you already have each 5 day period in a group, you should be able to right click that and add total. At which point you can just change the SUM to AVG there.

Count days of week to detect missing days

I've created an expression
=WeekDay([ApptDate])) and then used it as a group in a report. Sometimes all days of a week show up, and sometimes only five, or six days, in the footer for this group.
Day Appts
Monday 10
Tuesday 16
Thursday 5
Saturday 9
Totals: 4 40
It is easy to get the sum of appts (40), but I need to have a count of days that appear in this footer section to make it easy to spot when days are missing.
You could create a separate table containing the weekday names and a sort order (i.e. Monday=1, Sunday=7) and then create a join with that table to always return all day names and the count.
Unfortunately I can't include screen shots at the moment (I'll try and add this evening when I get home).
SELECT tbl_WeekDays.DayName
, COUNT(WeekDay(ApptDate)) AS Appts
FROM tbl_WeekDays LEFT JOIN DateTable ON tbl_WeekDays.DayName=WEEKDAYNAME(WeekDay(DateTable.ApptDate))
GROUP BY tbl_WeekDays.DayName, tbl_WeekDays.SortOrder
ORDER BY tbl_WeekDays.SortOrder
With the tbl_WeekDays table on the left of the LEFT join all records from that table will be returned.
I've also fully qualified each field name with the table name although you don't need to do this if the name isn't ambiguous.
You can have seven stacked textboxes, one for each weekday, with these ControlSources:
=Abs(Sum(1=Weekday[ApptDate])))
=Abs(Sum(2=Weekday[ApptDate])))
...
=Abs(Sum(7=Weekday[ApptDate])))

Using condition to set SSRS matrix column name

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.

SSRS Line Chart X-Axis group by Month

everyone. For the life of me I cannot figure out why the X-Axis is pulling 2 dates in each month when I want it to group by each month. In the Values I have:
Value Field =RunningValue(Fields!new_actualsalesfromsplit.Value, Sum, "Chart1_SeriesGroup")
Category Field: =Fields!closedate.Value
Category Groups =Month(Fields!closedate.Value)
Group On =Month(Fields!closedate.Value)
Series Group by ["salesperson']
The chart should have a line for each sales person, and each month should be a cumulative representation of the sales by that person. Thanks for any help.
Set the category field to "=Month(Fields!closedate.Value)", or something similar that will net the results you need. Right now, even though you're grouping by month, you're still telling SSRS to use the atomic data for your X axis, so that's what it's doing.
It may make your task simpler to just add a Calculated Field to your dataset - open the dataset properties window, go to the fields tab, and click "add". Set that field to your month value, use it for grouping and your X axis.

How to deal with counting items by date in MySQL when the count for a given date increment is 0?

I'm looking to make some bar graphs to count item sales by day, month, and year. The problem that I'm encountering is that my simple MySQL queries only return counts where there are values to count. It doesn't magically fill in dates where dates don't exist and item sales=0. This is causing me problems when trying to populate a table, for example, because all weeks in a given year aren't represented, only the weeks where items were sold are represented.
My tables and fields are as follows:
items table: account_id and item_id
// table keeping track of owners' items
items_purchased table: purchaser_account_id, item_id, purchase_date
// table keeping track of purchases by other users
calendar table: datefield
//table with all the dates incremented every day for many years
here's the 1st query I was referring to above:
SELECT COUNT(*) as item_sales, DATE(purchase_date) as date
FROM items_purchased join items on items_purchased.item_id=items.item_id
where items.account_id=125
GROUP BY DATE(purchase_date)
I've read that I should join a calendar table with the tables where the counting takes place. I've done that but now I can't get the first query to play nice this 2nd query because the join in the first query eliminates dates from the query result where item sales are 0.
here's the 2nd query which needs to be merged with the 1st query somehow to produce the results i'm looking for:
SELECT calendar.datefield AS date, IFNULL(SUM(purchaseyesno),0) AS item_sales
FROM items_purchased join items on items_purchased.item_id=items.item_id
RIGHT JOIN calendar ON (DATE(items_purchased.purchase_date) = calendar.datefield)
WHERE (calendar.datefield BETWEEN (SELECT MIN(DATE(purchase_date))
FROM items_purchased) AND (SELECT MAX(DATE(purchase_date)) FROM items_purchased))
GROUP BY date
// this lists the sales/day
// to make it per week, change the group by to this: GROUP BY week(date)
The failure of this 2nd query is that it doesn't count item_sales by account_id (the person trying to sell the item to the purchaser_account_id users). The 1st query does but it doesn't have all dates where the item sales=0. So yeah, frustrating.
Here's how I'd like the resulting data to look (NOTE: these are what account_id=125 has sold, other people many have different numbers during this time frame):
2012-01-01 1
2012-01-08 1
2012-01-15 0
2012-01-22 2
2012-01-29 0
Here's what the 1st query current looks like:
2012-01-01 1
2012-01-08 1
2012-01-22 2
If someone could provide some advice on this I would be hugely grateful.
I'm not quite sure about the problem you're getting as I don't know the actual tables and data they contain that generates those results (that would help a lot!). However, let's try something. Use this condition:
where (items.account_id = 125 or items.account_id is null) and (other-conditions)
Your first query is perfectly acceptable. The fact is you don't have data in the mysql table and therefore it can't group any data together. This is fine. You can account for this in your code so that if the date does not exist, then obviously there's no data to graph. You can better account for this by ordering the date value so you can loop through it accordingly and look for missed days.
Also, to avoid doing the DATE() function, you can change the GROUP BY to GROUP BY date (because you have in your fields selected DATE(pruchase_date) as date)