1

我有一个带有日期列的日期表。我想每个月都有一个周期。到目前为止,我已经构建了一个自定义列,以便每个周期从星期一开始,到星期日结束:

let         
startOfWeek = Date.StartOfWeek([Date]),
endOfWeek = Date.EndOfWeek([Date]),
yearOfStartOfWeek = Date.Year(startOfWeek),
yearOfEndOfWeek = Date.Year(endOfWeek),
    valueToReturn = if yearOfStartOfWeek = yearOfEndOfWeek then
    Text.From(Date.StartOfWeek([Date])) & " - " & Text.From(Date.EndOfWeek([Date]))
else
    Text.From(Date.StartOfWeek([Date])) & " - " & Text.From(Date.EndOfWeek(endOfWeek))
in valueToReturn

这会给我下面的自定义列“每周期间”: 在此处输入图像描述

但我需要的是这种每周周期的方式:

  • 每个月的第一周从当月的第一天开始,并且必须是工作日(不是周末)。例如,2019 年 9 月的第一周从 Monday 2nd 开始。此外,每个月的最后一个周期间以该月的最后一天结束。您是否知道如何更改此代码或我需要使用哪些其他代码才能拥有如下列的每周周期?非常感谢。 在此处输入图像描述

  • 另外,我想要另一列给我月份的周数,如上所述。如果有人可以帮助我在日期表中构建这两个自定义列,我将不胜感激。

4

2 回答 2

0

我在查询设计器(语言 M)中使用以下表达式进行了操作。转到Add Column > Custom Column(我在两个单独的列中做了):

'Get first day of month
FirstDayOfMonth = Date.StartOfMonth([Column1]) 'Make sure the outcome is a date format

'Get first working day of month (without Saturday and Sunday)
FirstWorkingDayOfMonth = if Date.DayOfWeek([FirstDayInMonth]) = Day.Saturday 
                            then Date.AddDays([FirstDayInMonth], 2) 
                         else if Date.DayOfWeek([FirstDayInMonth]) = Day.Sunday 
                            then Date.AddDays([FirstDayInMonth], 1) 
                         else [FirstDayInMonth]

对于周数列,您可以使用该Date.DayOfWeek()函数,就像我在上面使用的一样。

于 2019-08-30T04:55:43.823 回答
0

对于那些可能有帮助的人,我在这里找到了每周期间的答案:

let         
startOfWeek = Date.StartOfWeek([Date]),
endOfWeek = Date.EndOfWeek([Date]),
weekOfStartOfWeek = Date.WeekOfYear(startOfWeek),
weekOfEndOfWeek = Date.WeekOfYear(endOfWeek),

valueToReturn = if  (Date.Month(Date.StartOfWeek([Date])) = Date.Month([Date]) and Date.Month(Date.EndOfWeek([Date])) = Date.Month([Date]))
then 
Text.From(Date.StartOfWeek([Date])) & " - " & Text.From(Date.EndOfWeek([Date])) 

else if  Date.Month(Date.StartOfWeek([Date])) <> Date.Month([Date]) then
    Text.From([Date.StartOfMonth([Date])]) & " - " & Text.From(Date.EndOfWeek(endOfWeek))

   else
    Text.From(Date.StartOfWeek([Date])) & " - " & Text.From(Date.EndOfMonth([Date]))

in
valueToReturn
于 2019-10-09T14:29:15.097 回答