If then语句使用两个不同的日期范围

时间:2019-05-16 15:13:45

标签: if-statement powerbi powerquery m

在Power BI中,我正在使用一个周期表,该周期表应用了一些步骤来获取今天的日期,设置范围,将范围转换为表格,然后在Power Query中设置输出。一切都按预期进行,但重点放在范围步骤。

在这里是这样:

    = {
                    {"Day", 
                    Date.AddDays(TodaysDate,-1), 
                    Date.AddDays(TodaysDate,-1),
            1},
                    {"Week to Date", 
                    Date.From(Date.StartOfWeek(TodaysDate,Day.Thursday)), 
                    Date.AddDays( Date.From(Date.StartOfWeek(TodaysDate,Day.Thursday)) , +6), 
            2},
                    {"Week", 
                    Date.AddWeeks( Date.From(Date.StartOfWeek(TodaysDate,Day.Thursday)) , -1), 
                    Date.AddDays( Date.From(Date.StartOfWeek(TodaysDate,Day.Thursday)) , -1), 
....

这是示例输出:

enter image description here

但是我想要的是,如果今天等于星期一,那么仅显示星期五的日期。 这是我的尝试:

= {
            {"Day", 
            if Date.DayofWeek(TodaysDate) = 1 
       then 
    [
            Date.AddDays(TodaysDate,-3), 
            Date.AddDays(TodaysDate,-3),
    ]
            else
    [
            Date.AddDays(TodaysDate,-1), 
            Date.AddDays(TodaysDate,-1),
    ] ....

请注意,“日期”是“昨天”。 我正在努力做到这一点,以便人们可以看到前一天的工作正常,而星期一的前一天是星期五。

这是整个代码:

    let
    TodaysDate = Date.From(DateTimeZone.FixedUtcNow()),
    Ranges = {
                {"Day", 
                Date.AddDays(TodaysDate,-1),                 
                Date.AddDays(TodaysDate,-1), 

        1},
                {"Week to Date", 
                Date.From(Date.StartOfWeek(TodaysDate,Day.Thursday)), 
                Date.AddDays( Date.From(Date.StartOfWeek(TodaysDate,Day.Thursday)) , +6), 
        2},
        {"Week", 
                Date.AddWeeks( Date.From(Date.StartOfWeek(TodaysDate,Day.Thursday)) , -1), 
                Date.AddDays( Date.From(Date.StartOfWeek(TodaysDate,Day.Thursday)) , -1), 
        3},
                {"Month To Date", 
                Date.From(Date.StartOfMonth(TodaysDate)), 
                TodaysDate, 
                4},
                {"Month", 
                Date.AddMonths(Date.From(Date.StartOfMonth(TodaysDate)),-1), 
                Date.AddMonths(Date.From(Date.EndOfMonth(TodaysDate)),-1), 
                5},
                {"Rolling 13Wks", 
                Date.AddWeeks(TodaysDate,-13) + #duration(1,0,0,0), 
                TodaysDate, 
                6},
                {"Year To Date", 
                Date.From(Date.StartOfYear(TodaysDate)), 
                TodaysDate, 
                7}

             },
    GetTables = List.Transform(Ranges, 
            each CreatePeriodTable(_{0}, _{1}, _{2}, _{3})),
    Output = Table.Combine(GetTables),
    #"Filtered Rows" = Table.SelectRows(Output, each true)
in
    #"Filtered Rows"

1 个答案:

答案 0 :(得分:1)

我认为最简单的方法是为前一天创建一个变量:

let
    TodaysDate = Date.From(DateTimeZone.FixedUtcNow()),
    PrevWorkDate = if Date.DayOfWeek(TodaysDate) = 1
                   then Date.AddDays(TodaysDate,-3)
                   else Date.AddDays(TodaysDate,-1),

    Ranges = {
                {"Day", 
                PrevWorkDate,                 
                PrevWorkDate, 

        1},

    [...etc...]

否则,您可能可以执行以下操作:

let
    TodaysDate = Date.From(DateTimeZone.FixedUtcNow()),
    Ranges = {
        if Date.DayOfWeek(TodaysDate) = 1
        then
            {"Day", 
                Date.AddDays(TodaysDate,-3), 
                Date.AddDays(TodaysDate,-3),
             1}
        else
            {"Day", 
                Date.AddDays(TodaysDate,-1), 
                Date.AddDays(TodaysDate,-1),
             1},