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
这将为我提供下面的自定义列“每周时段”:
但我需要的是这样的每周周期:
每个月的第一周从该月的第一天开始,并且必须是工作日(不是周末)。例如,2019 年 9 月的第一周从 Monday 2nd 开始。此外,每个月的最后一周期结束于该月的最后一天。
您知道如何更改此代码或我需要使用哪些其他代码才能拥有 This weekly period 如下列所示?非常感谢。
此外,我还想要另一列提供月份的周数,如上所示。如果有人可以在日期 table.
帮助我构建这两个自定义列,我将不胜感激
我是在查询设计器(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
我有一个带有日期列的日期 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
这将为我提供下面的自定义列“每周时段”:
但我需要的是这样的每周周期:
每个月的第一周从该月的第一天开始,并且必须是工作日(不是周末)。例如,2019 年 9 月的第一周从 Monday 2nd 开始。此外,每个月的最后一周期结束于该月的最后一天。 您知道如何更改此代码或我需要使用哪些其他代码才能拥有 This weekly period 如下列所示?非常感谢。
此外,我还想要另一列提供月份的周数,如上所示。如果有人可以在日期 table.
帮助我构建这两个自定义列,我将不胜感激
我是在查询设计器(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