在Power BI上使用DAX创建自定义指标

时间:2019-08-15 04:58:59

标签: powerbi formula dax powerbi-desktop

  

适用于所有Power BI用户

我已经从下面的表“ Example”中创建了一个表视觉名称作为原始数据(类似于结果表,但Ind列除外)。我想创建一个指标,该指标将基于使用DAX的列总数。

Company | Rev 2018      | Rev 2019       | Rev YoY(%) | 
-----------------------------------------------------------
A       |   440,980,812 |   321,015,626 | -27.20%     | 
B       |   587,171,150 |   248,150,205 | -57.74%     | 
C       |   693,692,632 |   255,633,145 | -63.15%     | 
D       |   753,951,313 |   266,033,862 | -64.71%     | 
E       |   387,652,076 |   393,439,270 |   1.49%     | 
Total   | 2,863,447,983 | 1,484,272,108 | -48.16%     | 

下面是我当前的度量计算

Rev 2018 = CALCULATE(sum(Example[Rev]),Example[Year]=2018)
Rev 2019 = CALCULATE(sum(Example[Rev]),Example[Year]=2019)
Rev YoY(%) = ([Rev 2019]-[Rev 2018])/[Rev 2018]

我想创建一个指标(Ind),如果公司的年同期收益百分比(%)(对于公司A为-27.20%)大于或等于(> =)总体/总年同期收益百分比(%),则该指标将显示1 (占总数的-48.16%),否则将显示0。并且它将根据切片器的选择进行更改(如果选择了Jan,则更新的值应基于Jan进行更改,并且对于其他选择(如Feb,Mar等)也是如此。 / p>

目前,该值基于年初至今的值。基于1月,2月等月份,这些值将针对收入和同比进行更新,同时指标度量也应更新。

最终结果如下所示

Company | Rev 2018      | Rev 2019       | Rev YoY(%) | Ind
-----------------------------------------------------------
A       |   440,980,812 |   321,015,626 | -27.20%     | 1
B       |   587,171,150 |   248,150,205 | -57.74%     | 0
C       |   693,692,632 |   255,633,145 | -63.15%     | 0
D       |   753,951,313 |   266,033,862 | -64.71%     | 0
E       |   387,652,076 |   393,439,270 |   1.49%     | 1
Total   | 2,863,447,983 | 1,484,272,108 | -48.16%     | 1

如果有解决方案,请提供帮助。

我尝试使用filer(allcrossfiler),但它使字段不变,而切片器不会改变

Rev_total% = 
CALCULATE(
    [Rev YoY(%)],
    ALLCROSSFILTERED(Example)
)

基本原始数据

Company Year    Month   Rev
A   2018    Jan 3715518
A   2018    Feb 62195456
A   2018    Mar 47896563
A   2018    Apr 30397293
A   2018    May 13316124
A   2018    Jun 54702783
A   2018    Jul 23559246
A   2018    Aug 56357008
A   2018    Sep 91266366
A   2018    Oct 7826397
A   2018    Nov 30081453
A   2018    Dec 19666605
A   2019    Jan 20525691
A   2019    Feb 55636582
A   2019    Mar 70832178
A   2019    Apr 51101460
A   2019    May 71658353
A   2019    Jun 51261362
B   2018    Jan 70866878
B   2018    Feb 16605125
B   2018    Mar 77399457
B   2018    Apr 93675100
B   2018    May 24187836
B   2018    Jun 17141132
B   2018    Jul 23189326
B   2018    Aug 1228527
B   2018    Sep 77025448
B   2018    Oct 69069603
B   2018    Nov 61201073
B   2018    Dec 55581645
B   2019    Jan 49529171
B   2019    Feb 30268530
B   2019    Mar 58895051
B   2019    Apr 16378441
B   2019    May 63289350
B   2019    Jun 29789662
C   2018    Jan 28386565
C   2018    Feb 55081195
C   2018    Mar 98650639
C   2018    Apr 13600972
C   2018    May 79286377
C   2018    Jun 97910757
C   2018    Jul 59601906
C   2018    Aug 60499979
C   2018    Sep 10555754
C   2018    Oct 21239252
C   2018    Nov 79278588
C   2018    Dec 89600648
C   2019    Jan 27489712
C   2019    Feb 8085774
C   2019    Mar 33489287
C   2019    Apr 52598275
C   2019    May 50816690
C   2019    Jun 83153407
D   2018    Jan 69955023
D   2018    Feb 1684049
D   2018    Mar 44503967
D   2018    Apr 91505045
D   2018    May 74480545
D   2018    Jun 70038948
D   2018    Jul 28811752
D   2018    Aug 82052925
D   2018    Sep 97215945
D   2018    Oct 48093159
D   2018    Nov 96939697
D   2018    Dec 48670258
D   2019    Jan 68414609
D   2019    Feb 34593576
D   2019    Mar 28277668
D   2019    Apr 46146140
D   2019    May 83794133
D   2019    Jun 4807736
E   2018    Jan 21180873
E   2018    Feb 14552267
E   2018    Mar 27409537
E   2018    Apr 68894164
E   2018    May 24608038
E   2018    Jun 12774844
E   2018    Jul 13193433
E   2018    Aug 89921780
E   2018    Sep 34581806
E   2018    Oct 52068148
E   2018    Nov 11374013
E   2018    Dec 17093173
E   2019    Jan 21748970
E   2019    Feb 95983245
E   2019    Mar 49661560
E   2019    Apr 90056699
E   2019    May 72277971
E   2019    Jun 63710825

1 个答案:

答案 0 :(得分:1)

您可以使用以下内容:

Indicator = if(Example[Rev YoY(%)] > CALCULATE(Example[Rev YoY(%)];ALL(Example[Company]));1;0)

ALL正在发挥作用,它告诉公司所有公司数据,但仍保留所有其他过滤器。

一些建议:

我将使用实际日期,并合并“年和月”列, 这样可以更轻松地处理将来的数据。

您现在拥有2018年和2019年,如果您的数据增长了怎么办?还有更多年。.最好谈论PrevYear / NextYear。您可以做的是在数据RevNextYear中添加一列,基于此,您的报告将始终有效:

RevNextYear = CALCULATE(sum(RawRevenue[Rev]);
    FILTER(RawRevenue;RawRevenue[Company] = EARLIER(RawRevenue[Company]) &&
    RawRevenue[Month] = EARLIER(RawRevenue[Month]) && 
    RawRevenue[Year] =  EARLIER(RawRevenue[Year]) + 1)
    )