首页 文章

计算每月变化百分比

提问于
浏览
0

我正在尝试计算PowerBI中每月的净收入变化百分比 .

Time Patterns "Period Comparison Pattern"之后,其中指出:

例如,月度计算(MOM)将当前选择与一个月前的相同选择进行比较 .

This Reddit Post,其中规定:

扩展:CALCULATE(SUM(YourValueCol),PREVIOUSMONTH(YourDateCol))

我觉得我正在关注这些 . 见下文:


选定的模型尺寸:

  • 日期型号: Month (一年中的月份数)

  • NR :(计算: SUM(Orders[Unit Net Revenue USD])

得到:

Month | NR
----------------------
1       $5       
2       $6
3       $7

上个月的选定模型尺寸:

  • 日期型号: Previous Month (一年中的月份数)

  • NR :(计算: SUM(Orders[Unit Net Revenue USD])

得到:

Previous Month | NR
----------------------
1                $6       
2                $7
3                $8
---------------------
Total:            $21

因此,我尝试将当前月NR与上个月NR进行比较,最终获得%变化:

This Month NR = CALCULATE(SUM(Orders[Unit Net Revenue USD]), 'Date - Period'[Month]) // Month = 2 (February)

Prior Month NR = CALCULATE(SUM(Orders[Unit Net Revenue USD]), 'Date - Period'[PreviousMonth]) // Month = 1 (January)

% Change = DIVIDE([This Month NR], [Prior Month NR], BLANK())-1

// Also tried:

% Change = (([This Month NR] - [Prior Month NR])/ [Prior Month NR])

对于 This Month NR ,它给了我每行NR的总和,对于 Prior Month NR ,它给了我预期的值 . 为什么每月给出每年的总和?例如 . ,

Year |  Month  | This Month NR               | Prev Month NR
------------------------------------------------------------------
2015     1       $18 ($5 + $6 + $7 for 2015)  $6               
2015     2       $18                          $7
2015     3       $18                          $8 
------------------------------------------------------------------

预期:

Year |  Month  | This Month NR               | Prev Month NR
------------------------------------------------------------------
2015     1       $5                          $4               
2015     2       $6                          $5
2015     3       $7                          $6 
------------------------------------------------------------------

如果我执行以下计算字段

NR Measure = SUM('Retailer Sales'[Net Revenue])
This Month NR = CALCULATE([NR Measure], 'Date'[Month Num])
Prior Month NR = CALCULATE([NR Measure], 'Date'[Previous Month])

然后过滤 dimension Yeardimension Month NumThis Month NRPrior Month NR 我得到:

Year  | Month   | This Month NR | Prev Month NR
------------------------------------------------------------------
2015     1       $6              $6               
2015     2       $7              $7
2015     3       $8              $8 
------------------------------------------------------------------

预期(再次):

Year  | Month   | This Month NR | Prev Month NR
------------------------------------------------------------------
2015     1       $6              $5               
2015     2       $7              $6
2015     3       $8              $7 
------------------------------------------------------------------

对于上个月,如果我尝试改为 Prior Month NR = CALCULATE([NR Measure], PREVIOUSMONTH('Retailer Sales'[Date])) - Prior Month NR 对于相同的过滤器而言只是空白 .

我在DAX做错了什么?


enter image description here

1 回答

  • 0

    不要做计算列 - 它们应该(几乎)永远不会在DAX中使用 .

    改写措施:

    This Month NR = CALCULATE(SUM(Orders[Unit Net Revenue USD])
       Prior Month NR = CALCULATE([This Month NR], PRIORMONTH('Date'[DateKey]))
       % Change = DIVIDE([This Months NR], [Prior Month NR]) - 1
    

    这应该可行, as long as (!) 您的日期表设计正确 - 它必须是日历表,日期键为实际日期(年/月/日) . 如果您将日期表构建为"period"(即年月),则PRIORMONTH和其他时间智能功能将不起作用,因为"year-month"不是日期 .

    See Calendar table design requirements here

相关问题