PBI同比计算

时间:2019-02-18 16:17:16

标签: powerbi dax

我有一张数据表。并需要返回上一年和同一周的值,以便我可以计算出每周的单位和百分比差异。这是表的简化版本。我还有其他列需要进行类似的计算。

FileDate    Units   UnitsLY WeekNum Year    UnitsYOY%
1/22/2019 0:00  3751    2890    4   2019    
1/15/2019 0:00  3518    2625    3   2019    
1/8/2019 0:00   4075    2590    2   2019    
1/1/2019 0:00   4349    2982    1   2019    

WeekNum = WeekNum(PaymentsByWeek[FileDate])
Year = Year(PaymentsByWeek[FileDate])

UnitlyLY,WeekNum和Year是基于Filedate的计算字段。我想计算每个特定星期的同比单位变化和百分比变化。

UnitsLY = IF (
   HASONEVALUE ( 'Calendar'[CalendarYear] )
       && HASONEVALUE ('Calendar'[WeekNumberOfYear] ), CALCULATE(
        Max(PaymentsByWeek[Units]),
       FILTER (
           ALL ( 'Calendar' ),
           'Calendar'[CalendarYear] = VALUES ( 'Calendar'[CalendarYear] )-1
               && 'Calendar'[WeekNumberOfYear] = VALUES ( 'Calendar'[WeekNumberOfYear] )
              )       )   )
PaymentsByWeek),  PaymentsByWeek[Year] =  PaymentsByWeek[Year] -1  &&  'PaymentsByWeek'[WeekNum]=thisweek  ))

下面的公式给了我“检测到循环依赖”。我也尝试将UnitsLY计算值放入公式中,但仍然是同样的问题。

UnitsYOY%= DIVIDE(PaymentsByWeek [Units]-[UnitsLY],[UnitsLY])

最后,我使用vars尝试了此操作,但它返回了所有零值

UnitsYOY% = var week= (PaymentsByWeek[WeekNum]) var lastyear =( PaymentsByWeek[Year] -1)  var ly =( CALCULATE(  sum(PaymentsByWeek[Units]) ,
                    FILTER('PaymentsByWeek'
                    ,'PaymentsByWeek'[WeekNum] =week && PaymentsByWeek[year]= lastyear )))   Return Divide(PaymentsByWeek[Units]-ly ,ly)

如何计算特定星期之间的单位和百分比变化?

2 个答案:

答案 0 :(得分:0)

如何使用DAX内置的时间智能功能?

CALCULATE(
          DIVIDE(PaymentsByWeek[Units]-[UnitsLY],[UnitsLY]),
          PARALLELPERIOD(PaymentsByWeek[FileDate],-1,year)
         )

啊,明白了。循环引用问题的症结在于您必须在[UnitsLY]计算公式中引用[Units]。柱。也许这可以解决问题:

(1)删除[UnitsLY]计算。列,而是使用度量-像这样-我在这里假设“单位”列包含“所有年份”的数据:
 CALCULATE(SUM(PaymentsByWeek[Units]),FILTER(ALL('Calendar'),'Calendar'[WeekNum] = MIN('Calendar'[WeekNum]) -1 ))

(2)使用另一种度量来计算百分比变化,即,在DIVIDE函数中使用上述度量。

或者,如果您不想在任何地方显示UnitsLY,也可以使用1个小节。

答案 1 :(得分:0)

我已经解决了这个问题。 我正在创建列,这给了我依赖性错误。我已经创建了所有3个字段作为度量,它们可以正常工作。

PmtsLY = calculate( MIn(PaymentsByWeek[Pmts]),
FILTER(ALL('PaymentsByWeek'),
    'PaymentsByWeek'[Year]= Min('PaymentsByWeek'[Year])-1 && 'PaymentsByWeek'[WeekNum] = Min('PaymentsByWeek'[WeekNum]) ))  

PmtsYOY = calculate( Min(PaymentsByWeek[Pmts]) - PaymentsByWeek[PmtsLY] )


PmtsYOY% = calculate( DIVIDE(  (Min(PaymentsByWeek[Pmts]) - PaymentsByWeek[PmtsLY]) ,  PaymentsByWeek[PmtsLY],0)*100  )

PmtsYOY = calculate( Min(PaymentsByWeek[Pmts]) - PaymentsByWeek[PmtsLY] )