Power BI - 将周数转换为一年中的月份

Power BI - Convert Week Number to Month of the year

我在下面得到了这个示例 table。

Ticket Number Week Number New Status Date Month Name
1 39 01-Oct-2021 01:42:26 AM
2 39 01-Oct-2021 01:42:26 AM
3 39 01-Oct-2021 01:42:26 AM
4 39 01-Oct-2021 01:42:26 AM
5 39 01-Oct-2021 01:42:26 AM
6 40 06-Oct-2021 01:46:57 AM
7 40 06-Oct-2021 01:46:57 AM
8 40 06-Oct-2021 01:46:57 AM
9 40 06-Oct-2021 01:46:57 AM
10 40 06-Oct-2021 01:46:57 AM

我需要使用 Power BI 根据 “周数” 填写 “月份名称” 列。

您可以使用幂查询获得结果

let
    Source = Excel.Workbook(File.Contents("C:\Ashok\Power BI\Stack Overflow\data_24_oct_21.xlsx"), null, true),
    Data_Sheet = Source{[Item="Data",Kind="Sheet"]}[Data],
    #"Promoted Headers" = Table.PromoteHeaders(Data_Sheet, [PromoteAllScalars=true]),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Inbox Number", Int64.Type}, {"Week Number", Int64.Type}, {"New Status Date", type datetime}}),
    #"Inserted Month Name" = Table.AddColumn(#"Changed Type", "Month Name", each Date.MonthName([New Status Date]), type text)
in
    #"Inserted Month Name"

你的问题不是很清楚。

假设你想获得

  • Month Name from the date of the fitness of the week given by the week number
  • 周数计算为 ISO 周数,

然后我会先添加一个自定义函数来计算相关日期:

//fnDateFromISOwn
//compute date of the first day of the week given an ISO weeknumber
//if the Year is not entered, it will default to "this" year
//Year must be a full year: eg 2021 vs 21. The latter will not be interpreted as the year 2021.

(wn as number, optional year as number) =>
let  
    yr = if year = null then Date.Year(DateTime.LocalNow()) else year,
    wn1Start = Date.StartOfWeek(#date(yr,1,1),Day.Monday),
    w1 = if Date.AddDays(wn1Start,3) < #date(yr,1,1) then Date.AddDays(wn1Start,7) else wn1Start

in Date.AddDays(w1, 7*(wn-1))

然后您可以在主代码中添加一个自定义列:

    #"Added Custom" = Table.AddColumn(#"Previous Step", "Month", each 
        Date.MonthName(fnDateFromISOwn([Week Number], Date.Year([New Status Date]))))

来源

结果

另一方面,

  • 如果存在周数总是对应于New Status Date
  • 的关系
  • 它仍然是一个 ISO 周数,
  • 并且您仍然想要与该周开始日期相对应的月份名称

然后您可以使用更简单的公式,您只需计算前一个星期一到 New Status Date,并从该日期获取 MonthName。

Date.MonthName(Date.StartOfWeek([New Status Date],Day.Monday)))