一年中当前周与前几周之间的差异

时间:2019-05-29 06:48:05

标签: dax

我有一个SQL查询,该查询以周为单位计算了当前工作日与以前工作日之间的差,因此我需要将其转换为DAX查询。

SQL:

DATEDIFF(Week,Getdate(),
      DATEADD (WEEK, CAST (RIGHT(CAST ([Code] AS nvarchar),2) AS int),
      DATEADD (YEAR, ([Code] / 100)-1900, 0)) - 4 - 
      DATEPART(dw, 
                 DATEADD (WEEK, CAST (RIGHT(CAST ([Code] AS nvarchar),2) AS int),
       DATEADD (YEAR, ([Code] / 100)-1900, 0)) - 4) + 1)

 AS [WeekIndex]


enter code here

预期结果:如果代码= 201821,则WeekIndex必须为-53,因为当前星期是第22周

1 个答案:

答案 0 :(得分:0)

我认为您正在寻找这样的东西:

[Measure] :=
    VAR code = "201821"
    VAR code_year = LEFT ( code, 4 )
    VAR code_week = RIGHT ( code, 2 )
    VAR code_date = DATE ( code_year, 1, -2 ) - WEEKDAY ( DATE ( code_year, 1, 3 ) )
        + code_week * 7
RETURN  
    DATEDIFF ( TODAY (), code_date, WEEK )

VAR [code_date]将返回给定VAR [code]的星期一作为日期值