Question

I built a SSRS 2005 report on a SSAS 2005 cube. The report has start date and end date parameters from Time dimension. I need to set the default values to be last Sunday and last Saturday separately. (financial week is from Sunday to Saturday)

E.g.

Start date: [Time].[Day].&[20140309] End date: [Time].[Day].&[20140315]

How can I do it dynamically? I mean for this week is above dates, but for next week, it should be 16 March, 22 March. I know how to do it in T-SQL, which will involve some calculation with system date, but MDX?

Était-ce utile?

La solution

You can use some VBA Date functions available in MDX:

StrToMember('[Time].[Day].&['
            + Format(DateAdd('d', - DatePart('w', Now(), 1), Now()), 'yyyyMMdd')
            + ']'
           )

should give you the last Saturday before today, and

StrToMember('[Time].[Day].&['
            + Format(DateAdd('d', - DatePart('w', Now(), 1) - 6, Now()), 'yyyyMMdd')
            + ']'
           )

the last Sunday before that.

The second argument of DateAdd('d', ...) is the number of days to add. And as this is negative here, we go back in time that many days. DatePart('w', ...) returns the weekday number (Sunday = 1, Monday = 2, ...). Thus, if you subtract "weekday number" days from today, luckily you are already at last Saturday. And subtracting six more days, you arrive at the last Sunday before that.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top