SSRS - 如何使用SSRS表达式拉上周五

时间:2017-04-11 19:23:30

标签: reporting-services ssrs-2008-r2 ssrs-2012

我需要帮助来拉动前几个工作日,不包括SSRS中的周末。我使用以下代码,但是这段代码看起来并没有超过周末。

以下是我通常使用的代码:

=DateAdd(DateInterval.Day
, Switch(DatePart(DateInterval.WeekDay, Today()) = 2, -3
,DatePart(DateInterval.WeekDay, Today()) = 1, -2
,True, -1)
, Today())

有没有人想过如何编写SSRS表达式以查看周末和仅拉出工作日?

4 个答案:

答案 0 :(得分:1)

您可能在一周的第一天设置不同(取决于地区或服务器的配置方式)。无论此设置如何,在FirstDayOfWeek函数中明确设置DatePart都应该会产生一致的结果:

=DateAdd(DateInterval.Day
    , Switch(DatePart(DateInterval.WeekDay, Today(), FirstDayOfWeek.Sunday) = 2, -3
        ,DatePart(DateInterval.WeekDay, Today(), FirstDayOfWeek.Sunday) = 1, -2
        ,True, -1)
    , Today())

答案 1 :(得分:1)

继续探索这个并与其他人一起工作......这就是我们想要查看前一个工作日并在周一提取报告时排除周末的情况。此表达式将添加到日期参数。

=Switch(WeekdayName(DatePart("w", Today)) = "Wednesday",DateAdd(DateInterval.Day
    , Switch(DatePart(DateInterval.WeekDay, Today()) = 2, -3
 ,DatePart(DateInterval.WeekDay, Today()) = 1, -2,True, -2)    , Today()),

    WeekdayName(DatePart("w", Today)) = "Thursday",DateAdd(DateInterval.Day
    , Switch(DatePart(DateInterval.WeekDay, Today()) = 2, -3
 ,DatePart(DateInterval.WeekDay, Today()) = 1, -2,True, -2)    , Today()),

    WeekdayName(DatePart("w", Today)) = "Friday",DateAdd(DateInterval.Day
    , Switch(DatePart(DateInterval.WeekDay, Today()) = 2, -3
 ,DatePart(DateInterval.WeekDay, Today()) = 1, -2,True, -2)    , Today()),

    WeekdayName(DatePart("w", Today)) = "Saturday",DateAdd(DateInterval.Day
    , Switch(DatePart(DateInterval.WeekDay, Today()) = 2, -3
 ,DatePart(DateInterval.WeekDay, Today()) = 1, -2,True, -2)   , Today()),

    WeekdayName(DatePart("w", Today)) = "Sunday",DateAdd(DateInterval.Day
    , Switch(DatePart(DateInterval.WeekDay, Today()) = 2, -3
 ,DatePart(DateInterval.WeekDay, Today()) = 1, -2,True, -3)    , Today()),

    WeekdayName(DatePart("w", Today)) = "Monday",DateAdd(DateInterval.Day
    , Switch(DatePart(DateInterval.WeekDay, Today()) = 2, -3
 ,DatePart(DateInterval.WeekDay, Today()) = 1, -2,True, -4)    , Today()),

    WeekdayName(DatePart("w", Today)) = "Tuesday",DateAdd(DateInterval.Day
    , Switch(DatePart(DateInterval.WeekDay, Today()) = 2, -3
 ,DatePart(DateInterval.WeekDay, Today()) = 1, -2,True, -2)    , Today())

)

答案 2 :(得分:0)

如果您希望在星期一运行报表时将日期参数默认为星期五(否则默认值是前一天),请尝试以下操作:

=DateAdd(DateInterval.Day, IIf(DatePart(DateInterval.Weekday, Today) = 2, -3, -1), Today)

这可能是你真正需要的。

以下表达式可用于将日期参数默认为上周五,无论报告运行的哪一天。这可以为每周报告提供一些价值。

=DateAdd(DateInterval.Day, -(DatePart(DateInterval.Weekday, DateAdd(DateInterval.Day, 1, Today), 0, 0)), Today)

答案 3 :(得分:0)

此表达式还将拉出我最近一直在使用的上一个工作日。只需使用此代码更新您的Date参数。

=DateAdd("d",Switch(DatePart("w", Today) = 2,-3,DatePart("w", Today) = 1,-2,True,-1),Today)