Power BI 中每个月的每周周期列

Weekly Period Column For Each Month in Power BI

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

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

这将为我提供下面的自定义列“每周时段”:

但我需要的是这样的每周周期:

我是在查询设计器(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() 函数,就像我在上面使用的那样。

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

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