Sort by month value in ssrs - sql

I have a pretty awkward setup in SSRS with custom grouping options (i.e. by office, seller, buyer etc.). In example, user can group items by year and then by month. The outcome of the report is then:
Gr1. | Gr2.
2015 | April
2015 | August
2015 | February
2015 | January
and so on...
So both columns are alphabetically ordered, which works excellent for all custom grouping options but months - which should have their own logic for sorting. How could I implement that?

You should try to return all information form the database in its native type. Instead of returning the month names as ‘January’, ‘February’ etc, it would be better to return 20150101, 20150201 for example (or whatever the default date format is for your environment)
You can then alter the format of the type in the report. For example, set up the cell to return
=MonthName(Month(Fields!myDate.Value),False)
To return the name of the Month for any date. SSRS will then know when ordering how to put the dates in the correct order.
ALTERNATIVE
Assuming you do not want to edit the way the data is returned, you can use a Select statement in the code behind your report to manually provide an order for the months.
Supposing the Dataset
Month Val
---------- ---
January 1
February 2
March 3
When sorted by Month returns the following
Instead, insert the following code to your report (right click the report area, select Report Properties, then Code)
public function MonthNumber(MonthName AS String) AS Integer
Dim MonthNum AS Integer = 0
Select Case MonthName
Case "January"
MonthNum = 1
Case "February"
MonthNum = 2
Case "March"
MonthNum = 3
End Select
return MonthNum
end function
Then set the Group Properties to be Sorted on
=Code.MonthNumber(Fields!Month.Value)
Gives the following result
This is because when ordering the set, instead of just looking at the name of the report, it passes the name through the Code, which is effectively telling the report which number to assign to each month. It is then ordered on this value instead of the month's name.
Hopefully this is useful to you. If you have any further question please let me know.

Related

How to make a (MDX) calculation in a SSAS cube to get the difference between a value from the current row and a value from the previous row?

I would like to make a calculation to get the difference between the departDate from my current row and the arriveDateNextStop from my previous row. I have a fact table which has multiple columns. The three most important columns are: id, departDate, arriveDateNextStop.
If I have for example these two rows in my fact table:
id departDate arriveDateNextStop
1 01-01-2019 03-01-2019
1 04-01-2019 07-01-2019
Explanation: On 1 January 2019 I depart to the next destination and I arrive there on 3 January 2019. On 4 January 2019 I again depart to the next destination and I arrive there on 7 January 2019.
Now I would like to know how many days the idle time was (the amount of days between the arrival and the next depart). So with this example the idle time would be 1, because between 3 January 2019 and 4 January 2019 is one day.
First, I made this 'calculation' in Management Studio as a SQL query. See query below:
SELECT s.Id, s.departDate as Depart_current_place, s.arriveDateNextStop as Arrival_next_stop, LAG(arriveDateNextStop) OVER (ORDER BY arriveDateNextStop) AS Arrival_current_stop, DATEDIFF(DAY, LAG(arriveDateNextStop) OVER (ORDER BY arriveDateNextStop), departDate) AS Amount_of_days
FROM MyTable s
WHERE Id = 9
GROUP BY s.departDate, s.Id, s.arriveDateNextStop
ORDER BY s.departDate
This query works fine, but how can I do this in my cube as a calculation in MDX?
I don't have the same example, but the similar cube structure with Completed/Received date:
with
member departDate as [Received].[Year Qtr Month].CurrentMember.Member_Key
member arriveDate as [Completed].[Year Qtr Month].CurrentMember.Member_Key
member arriveDateNextStop as [Completed].[Year Qtr Month].CurrentMember.Lead(1).Member_Key
member idleDays as departDate-arriveDateNextStop
SELECT NON EMPTY { departDate,arriveDate,arriveDateNextStop,idleDays } ON 0
, NON EMPTY
{ ([Completed].[Year Qtr Month].[Date].ALLMEMBERS
* [Received].[Year Qtr Month].[Date].ALLMEMBERS ) } ON 1
FROM ( SELECT ( { [Completed].[Year Qtr Month].[Date].&[6213] } ) ON COLUMNS
FROM [MyCube])
I also have integer key for a date dimension (CurrentMember.Member_Key). 1 = 1998-01-01, 2 = 1998-01-02 etc. till today. You need to create a property in a Date dimension if your Date key is classic YYYYMMDD (which you cannot subtract to get days difference, I can do that in my example). And use it like CurrentMember.Properties("property name") instead of Member_Key.
Main formula part: Lag/Lead function to get prev. or next member.
Please update in case of questions.

Tableau Trend line -date calculations

I have dimension as date, --(Date has discrete dates- some dates from this months , some dates from last and so on - with data.)
more dimensions are : Team name & Team ID
other dimension is Result.
Result column has value : pass or fail ---only two values.
below data gets refreshed everyday.
date Team name Team ID Result
24/07/2008 lol 458 pass
27/01/2017 pop 1478 fail
28/02/2018 laugh 99 pass
and so on
I want to show dates on x axis, Result : Pass or Fail on trend-lines .
what would be my Y axis?
Want to show pass or fail team ID wise on trend lines!
If its December month then team ID which have passed /failed for 31st of December 2017 should show up.
similarly for Jan month (team ID ) which have passed or failed should show of 31st jan 2018.
AND for Feb month it should show pass / fail for current date --- {Team count pass or fail for particular current day}
please help with calculated filed calculations
Try This:
if Date= today() then 1
elseif dateadd('day',-
day(DATEADD('month',1,Date)),DATEADD('month',1,Date))=Date then 1
else 0
than set filter to 1

SSRS Generic Drop Down Month Parameter

I am using Visual Studio 2012 and SQL Server 2012. I'm trying to create a simple drop down parameter to show generic month/year over a given timeframe, lets say last 5 or 6 years (for example... Sept 2016, Oct 2016, etc).
Edit: My goal with this is that I have a StartDate field for a record and all records that fall within a certain Month, I want to capture with the parameter.
I have already been using a parameter for Start Date and End Date using Date/Time field when creating my parameter to get a range. I no longer wish to use that because my end user wants it simply by month/year.
This is the code I have which shows only the Month of the Start Date right now. How do I amend this code to show Month and Year?
SELECT DISTINCT DATENAME(month,A.[STRT_DTTM]) AS [Month]
FROM Work as A
WHERE A.[STRT_DTTM] IS NOT NULL
ORDER BY DATENAME(month,A.[STRT_DTTM])
Additionally once code is amended, how would I go about this to sort month by normal calendar flow (aka Jan, Feb, Mar, Apr...?)
Try SSRS, which you probably have -
Then have a look at this - Basically you choose the 'Date' type for the drop down parameter: https://technet.microsoft.com/en-us/library/aa337401(v=sql.105).aspx
SELECT DISTINCT DATENAME(month,A.[STRT_DTTM]) AS [Month]
FROM Work as A
WHERE A.[STRT_DTTM] IS NOT NULL
ORDER BY DATENAME(month,A.[STRT_DTTM])

Pass the monthly value of an SSRS report chart into another report with date from and date to parameters

I have a yearly chart that it broken down into the 12 months Jan - Dec. The report contains various parameters including a yearly dropdown that changes the chart and report.
This all works fine within the first report.
The problem is that I have set up an action on the chart to go to a second report with a monthly breakdown, so my question is how can I pass the monthly value from the first report to the second?
The monthly report has an additional date from and date to parameter, so for the month of January it would need the values: Date From: 01/01/2010 and Date To: 31/01/2010 for example.
Thanks in advance.
Since you have the year and month integer values, you can construct the start and end dates to pass to your other report using expressions.
The start of the month will be:
=DateSerial(Fields!Year.Value, Fields!Month.Value, 1)
Where Year and Month are the integer values from the Chart/Dataset.
The end date is a bit more complicated; since the day part can be 30/31, etc, but we can just add one month to the above expression to get the first of the next month, then go back a day:
=DateAdd(DateInterval.Day
, -1
, DateAdd(DateInterval.Month, 1, DateSerial(Fields!Year.Value, Fields!Month.Value, 1)))
This way your drillthrough report can get its date based parameters and you don't need any changes to your dataset/parent report.

How to get month within a range vb2010

I just don't know how to go about this.
I designed a program that uses MS Access as its database. I have a field for month and year (the field data type is text) where user can register details. The program will register the month and year the user have chosen e.g month= September, year=2011.
My problem now is how to chose a range of data to view by the user using the month and year as a criteria e.g the User may want to view data range from (September 2011 to July 2013).
I couldn't figure out even how to try. Help will be highly appreciated.
Perhaps you could change your application logic to store the month and year as their respective numbers rather than text and change the field data types to numeric.
You could then construct a DateTime object from them, for example September would be 9 and you could use code like the following:
var startDate = new DateTime(year, month, 1); // get year and month as integers from database, uses the first as the date
var endDate = new DateTime(year, month, 10); // change the date but keeps the month and year the same
var endDate2 = startDate.AddMonths(1); // adds 1 month to the date
Alternatively, you could try using a calendar control to allow the user to select two dates instead of building it from a number of fields. Depending on what you are using this could be achieved a number of ways, for example in ASP.Net or WPF you could use two calendar controls and just use their SelectedDate properties as your range.
A range is from a startpoint until an end point. For the startpoint you can add automatically the first of Month. For the endpoint is it more complicated because there is no fix endpoint. What you can do is following:
Write an array that contains for each month the days (e.g. 30 or 31). Except for Febrauary there is a fix pattern.
for Febrauary use the selected year to check is the year a leap year or not. If not add 28, else add 29.
After that create the date string for your SQL:
Startdate 1.9.2011. Do for the entdate the same.
After that, I think you can use the keyword between in your SQL query.
You can assume that everything is entered on the first day of each month. I would pull the information using a query to the database.
select * from [tablename] where DateSerial([colYear], [colMonth], 1) between DateSerial([fromYear], [fromMonth], 1) and DateSerial([toYear], [toMonth], 1)
In this question are some ways to do this:
First. Filter the dates in a range assuming that you use a date like '07-12-2012'
i.e. September 2011 to July 2013
Where DateColumn > '09-01-2011' and DateColumn < '07-31-2013'
OR
Specify a Date and a Year
Where month(DateColumn)='1' and year(DateColumn)='2016'
Note:
There are many ways to do this.
You can Manipulate your statement depending on your desired output.