SSRS - Hide Results for future months - sql-server-2008

I'm producing a line graph within SSRS using SQL Queries. I have a line graph with 4 datasets (3 using lookup function).
My queries calculate values for each month of current year. As we are only in June, values of months from July to December are 0. I don't want that they were displayed on graph and on axis.
Can anyone point me in the right direction? I've tried to add a filter to the category group for the axis but this hasn't worked
Expression: Month_field
Operator: <
Value: =Month(Today())
my sql query is a build up of months for this year so I can get a result for all months ( I was having Issues returning 0 as a value for a month)
eg..
(SELECT * FROM (VALUES(DATENAME(month,'2015-01-01'),1)
,(DATENAME(month,'2015-02-01'),2)
,(DATENAME(month,'2015-03-01'),3)
,(DATENAME(month,'2015-04-01'),4)
,(DATENAME(month,'2015-05-01'),5)
,(DATENAME(month,'2015-06-01'),6)
,(DATENAME(month,'2015-07-01'),7)
,(DATENAME(month,'2015-08-01'),8)
,(DATENAME(month,'2015-09-01'),9)
,(DATENAME(month,'2015-10-01'),10)
,(DATENAME(month,'2015-11-01'),11)
,(DATENAME(month,'2015-12-01'),12)) AS Mnth(" Month ",MnthSort)) AS M
followed by the below to returnvalues greater than 1st fay of current year and less than 1st day of current month
WHERE Received1Date >=dateadd(mm,datediff(mm,0,getdate())-12,0)AND Received1Date < dateadd(mm,datediff(mm,0,getdate()),0)
My issue is when using my query in SSRS on a line graph all months display on Axis and all future results are 0 - I know this is expected because of my query but just wondering if there is something I can do to the graph within SSRS to exclude all future months?

Why don't you filter the data in your query?
select values
from table
where transaction_date >= Getdate()
this way those future months are not options to be displayed.

Related

How to tie the column visibility expression in SSRS to current year and current month?

I have an SSRS report that has all 12 months as columns and one more column called "Year". I already have a condition set that takes the current month of the report run date and displays only the months that have passed. For example, the expression looks like this for March =IIF(Month(today()) < 3, True, False).
Now I have to tie this to the Year column as well. The condition is- if the Year column has years prior to the current year, the condition above should not apply. So, if I am running 2020 data today, all 12 month columns should appear. If I am running 2021 data, I should only see the January column. Can some one please help me with this? And yes, there will only be one year in the "Year" column. The data will never have more than one year.
Thank you.
Edit 1:
If the report is run for 2020, the data should look like this on the report.
If it is run for 2021, only the YEAR and JANUARY columns should appear. Starting February of this year, YEAR, JANUARY, AND FEBRUARY should appear.
As your dataset is not normalised, you will have to set the column visibility on each column as you are doing already.
Try using the following expression. (using March as an exmaple)
=(Month(today()) < 3 AND MAX(Fields!Year.Value, "DataSet1") = YEAR(Today())
Note: The "DataSet1" reference is the name of your dataset. It is case sensitive and must be enclosed in quotes as shown above.
There is no need for the IIF() expression as the above will return true if either condition is met.
Alternative
If your data was normalised (you may be able to edit your dataset query) and looked something like this...
Year Month Amount
2021 1 10
2021 2 15
Then you could simply use a matrix instead of a table, and have a column group by month. There would be no need to set visibility on anything as the data simply is not there so no column would be rendered for the months with no data.

Finding difference at group level

I have 3 months of AR data. From SQL I am populating details records for 3 months. In SSRS I am grouping the totals by month and showing only group summary. I want to calculate the variance by difference between current month and previous month. Please see the attached output file.
Could anyone help me on finding the variance at group level. How do I calculate the difference by using previous function?
Is there any way to find individual group sum of the particular field like this one
Sum(Fields!Current.Value, "DataSet1")
Something like this perhaps?
=Sum(iif(month(Fields!myDate.Value) = month(Max(Fields!myDate.Value)), Fields!myVal.Value, 0))
-Sum(iif(month(Fields!myDate.Value) = month(dateadd("M", -1, max(Fields!myDate.Value))), Fields!myVal.Value, 0))
Where myDate is the date used to group the months on, and myVal is the specific data for the column
UPDATE
Using this dataset
EOM myValue
09/30/2015 2
10/31/2015 6
11/30/2015 19
Gives this basic report
You can then use this formula (based on the original one in the original answer above) to determine the difference between the last and the penultimate row
=Sum(
iif(month(CDate(Fields!EOM.Value)) = month(Max(Fields!EOM.Value)),
Fields!myValue.Value,
0)
)
-Sum(iif(month(CDate(Fields!EOM.Value)) = month(dateadd("M", -1, max(CDate(Fields!EOM.Value)))),
Fields!myValue.Value,
0)
)
Then place this in the footer of the tablix as shown
This shows the difference between the two rows
Is this the behaviour you require? If not please clarify further in the original question as an update/edit.

R how to select 150 days with only month and day information

I was able to select last 150 days from database when having column 'year' as follow:
data1 = dbGetQuery(conn_data, statement=paste("SELECT *, STR_TO_DATE(CONCAT(yyyy,'-',mm,'-',dd),'%Y-%m-%d') as dt FROM stations_daily_data", "WHERE STR_TO_DATE(CONCAT(yyyy,'-',mm,'-',dd),'%Y-%m-%d') >= DATE_SUB(CURDATE(), INTERVAL 150 DAY)"))
But now all data were averaged to date and thus only have columns 'month' and 'day' (no column 'year'), and I was stuck in how to select last 150 days this time. Here is the simplified example of data frame with original one of 17 million rows:
df <- data.frame(ID=c(1:5,50001:50005),mm=c(rep(1,5),rep(12,5)),dd=c(1:5,27:31),value=c(21:30))
Feb 29 can be ignored since 150 days is a significant amount of time period.
I tried add column 'year' so that I could use the code above, but it would be wrong if say, current date is at the beginning of a year, also make changes to a big table in R would run out of R memory, I'm not familiar with database query, is it possible that I can do this by just using query instead of read the table into R and then make changes in the data frame in R, any suggestion would be appreciated!
EDIT:
The column 'year' is no longer needed since its all been averaged to date, which means now May 5th would be the average of 60 years of May 5th of each year. Next I would like to select last 150 days(averaged), the reason I tried to add column 'year' was simply try to make it easier to select.
Since I need to run the data every day, so if the day is after the month of June it would be easy just to use the current year, but if it's the month of February, then it would be current year-1, this could be done if the data is much smaller, now if I make change to the data frame, the R would pop out error of 'out of memory', that's why I was wondering if there is a way to select in database query or functions in R that wouldn't cost much memory, thanks!
You could write a function to calculate year based on a reference year plus an adjustment based on a cut off month. Then you could use the order function to order the data.frame based on calculated year, month, and day, without inserting the new calculated year field into the data.frame.
Won't have a great performance on 17 million row dataset though, since you are still ordering every row.
# some dummy data (not worrying about illegal dates like Feb 31)
set.seed(123)
da <- data.frame(mm=sample(1:12, 20, replace=T),
dd=sample(1:31, 20, replace=T))
# function to calculate year from reference year and cut off month
calc_year <- function(mm_vec, ref_year, cut_month) {
ref_year + ifelse(mm_vec >= cut_month, 0, -1)
}
# order the data.frame by year, month, and day
# (taking 2014 as ref. year & assuming months before June are from prior year
da[with(da, order(calc_year(mm_vec=mm, ref_year=2014, cut_month=6), mm, dd)), ]
# if you want just the first 5 rows
da[with(da, order(calc_year(mm_vec=mm, ref_year=2014, cut_month=6), mm, dd)), ][1:5,]

Grouping months by quarter over multiple years depending on a dynamic start month

Using MySQL and PHP I am building a JSON array to populate a data table.
For the purposed of my question suppose the data table has the following columns:
Year: 2010,2011,2012,2013...<br/>
Month: 1,2,3,4,5...<br/>
Value: 100, 150, 200 etc...<br/>
The table structure cannot be altered and my solution needs come into the MySQL query
The data can be viewed either monthly, quarterly or yearly. Monthly and yearly is achieved easily through grouping by year and month.
Quarterly data can be grouped by calendar quarter (Jan-Mar, Apr-Jun, Jul-Sep, Oct-Dec) by this group statement:
GROUP BY year, round((month/3)+0.3,0)
So where Jan, Feb and March might all have 100 for their value the summed result is 300, same for other months.
Now my problem comes when I want to group values by a financial quarter, for example a quarter that starts in Feb, or any other quarters.
I have a statement that works for the quarter grouping using two variables that can be accessed via the SQL query, start_year (i.e. 2014) and start_month (i.e. 2)
GROUP BY year, (((round(((((month-(start_month-1))+((year-start_year)*12))-((year-start_year)*12))/3)+0.33,0)/4)+4)-floor(((round(((((month-(start_month, '%m')-1))+((year-start_year)*12))-((year-start_year*12))/3)+0.33,0)/4)+4)))*12
which basically will assign a 0,3,6,9 value to each calendar month for the purposes of grouping.
In the financial year starting February this works fine for quarters 1-3, however breaks for the final quarter as it includes Nov and Dec 2014 data and Jan from 2015.
As a result I get 5 rows of data instead of 4.
This is because of the preceding GROUP by year clause, an important addition as we might want to generate a table that views sequential quarters for multiple years.
So what I am looking for is a way of grouping the years together by offsetting the start month.
So when the year starts in Jan it will group Jan-Dec but if we change that to starting Feb it will group Feb-Jan.
Any ideas, suggestions most welcome!
Regards,
Carl
I solved a similar problem just now (a Moodle report aggregating assignment scores by year and quarter) with something like this:
select year(from_unixtime(s.timemarked)) as year, quarter(from_unixtime(s.timemarked)) % 4 + 1 as quarter, count(distinct data1) as "tickets graded" from mdlassignment_submissions s where grade >= 0 group by year, quarter order by year, quarter;
The relevant part for what you're doing is quarter(from_unixtime(s.timemarked)) % 4 + 1 as quarter
As another commenter pointed out, MySQL has a quarter() function, but it doesn't do financial quarters. However, since (as I understand it, at least, based on consulting the relevant wikipedia page) financial quarters are just offset by 1, the % 4 + 1 at the end should convert it.

Get sum of values based on the value of 2 other date related columns

Given the sample data in the screenshot below, would it be possible in mysql to return a sum of values from monthly_amount only where the values are before this month. I used a join to pull this data. The 5 left columns are from one table, and the rest are from another.
The issue I'm running into is, lets say its April of 2015, I can't just do a sum WHERE goal_year <= 2015 AND month_id_FK <= 4, or else I'll get only those 4 months from both years, when in that scenario, I really want all the months from 2014, plus the 4 months from 2015.
I could handle this in PHP, but I wanted to first see if there would be a way to do this in mysql?
try
WHERE Goal_Year*100+month_id_FK <= 201504
alternatively:
WHERE
GOAL_YEAR < 2015 OR
(GOAL_YEAR = 2015 and month_id_FK <= 4)
select sum(monthly_amount) from table where goaldate<(SELECT CURDATE())
this is not the actual query for your table..but if you do like this you will get the answer
you need the sum of monthly amount where the date is before current-date means today.
then you can just compare the currentdate with goal date