I'm trying to come up with an SSRS expression to return a previous value from the year prior. As you can see in the screenshot, I'm trying to return the value of 22 in the "Prior Year Actual" column. This value aligns with April 2020. The last month I have in the dataset currently is April 2021. Essentially I want to return the value from the year prior based on the last column of April 2021.
Can anyone help with this? Greatly appreciated!
If your month fields are dates in a column group, you could get the MAX date and add it to the dataset.
DECLARE #MAX_DATE DATE = (SELECT MAX(DATE_FIELD) FROM TABLE)
SELECT *, #MAX_DATE AS MAX_DATE
FROM TABLE
Then your expression could use the new field to make it work like you want.
=MAX(IIF(Fields!Date.Value = DATEADD("y", -1, Fields!MAX_DATE.Value), Fields!COLUMN.Value, NOTHING))
This worked! Thank you very much Hannover for your expression suggestion!!
=MAX(IIF(Fields!DateTest.Value= DATEADD("yyyy", -1, MAX(Fields!DateTest.Value)), Fields!Operating_Days.Value, NOTHING))
I am creating a report and would like to retrieve data based on a date field (string) in the format YYYYWW. I want to filter the data so I retrieve the current month, previous month, the month before that and older values - using 4 different datasets.
For example:
201547 = November 2015
How do I create a calculated member which converts a string into the month name and year? I would like to use this value to filter out the data from the cube. Is there any other way of doing this?
:]1
Try adding a calculated field and set it to this expression:
=MonthName(Month(DateAdd("ww",CInt(MID("201547",5,LEN("201547")))
,DateSerial(CInt(LEFT("201547",4)),1,1)))) & " " & LEFT("201547",4)
Then use that field to filter your report.
Replace the string for the field returning "201547"
=MonthName(Month(DateAdd("ww",
CInt(MID(Fields!YearWeek.Value,5,LEN(Fields!YearWeek.Value)))
,DateSerial(CInt(LEFT(Fields!YearWeek.Value,4)),1,1))))
& " " & LEFT(Fields!YearWeek.Value,4)
UPDATE: MDX script for querying a calulated member with the required format:
I've tested with the following MDX:
WITH MEMBER [Measures].[month&year] AS
'FORMAT(DateAdd("ww",STRTOVALUE(MID("201547",5,LEN("201547"))) ,CDATE(DateSerial(LEFT("201547",4),1,1))),"MMMM") + " " + LEFT("201547",4)'
SELECT { [month&year] } ON COLUMNS
FROM [Your Cube]
To use it in your query replace "201547" for [Dimension].[Attribute].MemberValue that corresponds to your cube structure:
WITH MEMBER [Measures].[month&year] as
'FORMAT(DateAdd("ww",STRTOVALUE(MID([Dimension].[Attribute].MemberValue,5,LEN([Dimension].[Attribute].MemberValue)))
,CDATE(DateSerial(LEFT([Dimension].[Attribute].MemberValue,4),1,1))),"MMMM") + " " + LEFT([Dimension].[Attribute].MemberValue,4)'
SELECT
{ [Dimension].[Attribute].[Attribute] }
{[month&year]} ON COLUMNS
FROM [Your Cube]
Once you have this field in your dataset you can easily filter the values using a parameter.
Let me know if this can help you.
Using a combination of the resources found out on the internet you can find the information you require.
First you need to find the first day of the first week of the year. This has already been determined by this answer as
DateAdd("d",
(-1) * (CInt(New DateTime(Year(Now), 1, 1).DayOfWeek) +
IIf(CInt(New DateTime(Year(Now), 1, 1).DayOfWeek) < DayOfWeek.Monday,
7,
0)
) + 1,
New DateTime(Year(Now), 1, 1))
This needs to be adapted for any year - not just the current one by finding the year from the string as follows
CInt(Left(Parameters!myDate.Value,4))
Now, as noted in this resource you can use a DateAdd to add the number of weeks of the year you have entered to the first date of the year
=(DateAdd(DateInterval.WeekOfYear,
CInt(Right(Parameters!myDate.Value,2))-1,
<<FIRST_DAY_OF_YEAR>>)
You then want to get the month name of the month of this date, using Month and MonthName. Smashing it all together would result in the following
=MonthName(Month(DateAdd(DateInterval.WeekOfYear,
CInt(Right(Parameters!myDate.Value,2))-1,
DateAdd("d",
(-1) * (CInt(New DateTime(CInt(Left(Parameters!myDate.Value,4)), 1, 1).DayOfWeek) +
IIf(CInt(New DateTime(CInt(Left(Parameters!myDate.Value,4)), 1, 1).DayOfWeek) < DayOfWeek.Monday,
7,
0)
) + 1,
New DateTime(CInt(Left(Parameters!myDate.Value,4)), 1, 1))
)))
This will get a Textual value of the month for the interval
Create a new placeholder in the same textbox and then repeat the above for finding the year
=Year(DateAdd(DateInterval.WeekOfYear, ...
(Remember to remove the last brace otherwise you’ll get a End of Statement expected error.)
This shoud give what you require, so
201501 = December 2014
201547 = November 2015
201553 = December 2015
201622 = May 2016
Let me know if this helps, or if you have further questions
Part 1: Establishing which day falls into which week
ISO-8601 declares the international standards for converting dates into week numbers, and vice-versa.
ISO standard weeks start on a Monday. A year can have either 52 or 53 weeks.
There are 4 ways of establishing when Week 1 starts:
- It is the first week with a majority (4 or more) of its days in January.
- Its first day is the Monday nearest to 1 January.
- It has 4 January in it. Hence the earliest possible dates are 29 December through 4 January, the latest 4 through 10 January.
- It has the year's first working day in it - i.e. excluding Saturdays, Sundays and the 1st of January.
Part 2: Excel calculations
This answer assumes that the dates are being stored as 6-character strings.
First determine the year and what day of the week it starts on.
1) Strip the first four characters from the string.`
=left(<cell containing the year+week string>, 4)
2) Prepend "1/1/" to it.
="1/1/"&left(<cell containing the year+week string>, 4)
3) Convert this string to an Excel date
=datevalue(<the last result>)
4) Determine the Weekday of the 1st day of that year.
=weekday(<the last result>)
5) Calculate the Excel date of the first Monday.
=if((<the last result>-4)<0,<the result of step 3>+8-<the last result>, <the result of step 3>+1-<the last result>)
6) Work out how many weeks on from that we are:
=right(<original 6 char year/week string>,2)
7) Convert 6) to a number
=value(<last result>)
8) Add that to the Monday of Week 1
=<result of step 5>+<result of step 7>*7
9) Convert this to a month number
=month(<last result>)
10) Convert this to a month name
=choose(<last result>,"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")
11) Add the year back on
=<lastresult>&" "&<result of 1>
This gives the month number which contains the Monday of the current numbered week.
If cell A1 contains '201547
then cell B1 should contain:
=CHOOSE(MONTH(IF((WEEKDAY(DATEVALUE("1/1/"&LEFT(A1,4)))-4)<0,DATEVALUE("1/1/"&LEFT(A1,4))+1-WEEKDAY(DATEVALUE("1/1/"&LEFT(A1,4))),DATEVALUE("1/1/"&LEFT(A1,4))+8-WEEKDAY(DATEVALUE("1/1/"&LEFT(A1,4))))+7*(VALUE(RIGHT(A1,2)))),"January","February","March","April","May","June","July","August","September","October","November","December")&" "&LEFT(A1,4)
If A1 contains 201547 then B1 returns "November 2015".
Further considerations:
You may want to make it more sophisicated, so that a month ending in a Monday does not have the week commencing with its final Monday counted as part of the month. More generally, that week only counts as part of a month is 3 or more of the days are in the month. Or 3 or more of its working days are part of the month.
As it is, I would sort with the calculated month and year as primary key and the YYYYMM code as secondary key.
I have some records in a query with a column of dates. I want it to only show the records that occur in March. This link
https://support.office.com/en-US/Article/Examples-of-query-criteria-3197228c-8684-4552-ac03-aba746fb29d8#bm1 shows the different types of criterion.
The one below in the table describes how to show only what occurs in a particular month:
"Contain a date that falls in a specific month (irrespective of year), such as December
DatePart("m", [SalesDate]) = 12
Returns records where the transactions took place in December of any year."
I don't know what the SalesDate means in the criteria function, and there isn't any explanation on the page.
[SalesDate] implied a Date/Time field named SalesDate. If your Date/Time field is named something else, such as invoice_date, substitute that name in the DatePart expression:
DatePart("m", [invoice_date]) = 12
For March instead of December invoices, use this:
DatePart("m", [invoice_date]) = 3
You could also use Month() instead of DatePart to get the same result:
Month([invoice_date]) = 3
How do I build an expression in SSRS that only captures the Month and Year and not the day from a datestamp field?
I am trying to create a Month To Date column in SSRS. I have a date timestamp field, I also created a Month field and a Year field in hopes of solving my own problem. I didn't.
I have an expression built that allows me to capture the month and it works, but in my data set I have July data for both 2013 and 2014. This expression I only want to capture 2014.
=Count(IIF(Fields!Date_Month.Value = Month(Today()), Fields!AcctNo.Value, Nothing),
"Eligibility")
and I got it to work for the Year:
=Count(IIF(Fields!Year.Value = Year(Today()), Fields!AcctNo.Value, Nothing),
"Eligibility")
Can I somehow combine the 2 expressions? Should I?
Orrrrrrrrrr
I have tried to use my datestamp field to no avial: I get #Error with this abomination
=Count(IIF(Fields!Datestamp.Value = Month(Today()), Fields!AcctNo.Value,
Nothing), "Eligibility")
I'd think the preferred way is to use my above datestamp field and parse out the month and year. It's the way I'd do it....if I knew how to actually do it.
As you've suggested, you can combine the two expressions you have to get your required result with a few small changes:
=Count(IIf(Year(Fields!Datestamp.Value) = Year(Today)
and Month(Fields!Datestamp.Value) = Month(Today)
, Fields!AcctNo.Value
, Nothing)
, "Eligibility")
This will count all rows in the Dataset that have the same year and month as the current date.
I am trying to write an expression to count the number of requests within a specific month from a year's worth of data.
I have tried:
=sum(iif((datepart("M",Fields!RequestDate.Value)) = (datepart("m",Now(-1))),1,0))
and many more different versions. Can someone point me in the right direction?
It seems in your example you're counting events from last month? I'm not sure what Now(-1) is trying to calculate in your example.
Anyway, this example worked for me in identifying counts from last month:
=Sum(IIf(DatePart("m", Fields!RequestDate.Value) = DatePart("m", DateAdd("m", -1, Now()))
, 1
, 0))
You can update the DateAdd("m", -1, Now()) part of the expression to set the month you want to update against.