我想计算过去5年中公司的平均得分。在计算中不包括NULL(但包括0)。以下是数据:
Company Year Score
Company A 2014 1
Company B 2014 1
Company C 2014
Company D 2014 0
Company A 2015 1
Company B 2015 0
Company C 2015
Company D 2015 0
Company A 2016 1
Company B 2016 1
Company C 2016 1
Company D 2016 1
Company A 2017 1
Company B 2017 1
Company C 2017 1
Company D 2017 1
Company A 2018 1
Company B 2018 1
Company C 2018 1
Company D 2018 1
基本上,如果您看到C公司和D公司看起来相似。但是,2014年和2015年,公司C的得分为NULL,而公司D的得分为0。
C的平均值为100%(3/3),而D公司的平均值为60%(3/5)
使用以下代码,对于没有NULL的公司,我可以完美地实现结果:
_COL_Liquidity Score 2 =
VAR MaxSelectedYear = 'Data'[Year]
VAR OnePriorYear = MaxSelectedYear - 1
VAR TwoPriorYear = MaxSelectedYear - 2
VAR ThreePriorYear = MaxSelectedYear - 3
VAR FourPriorYear = MaxSelectedYear - 4
VAR MinYearInData = MINX(ALL('Data'), 'Raw Data'[Year])
RETURN
DIVIDE(
CALCULATE(SUM('Data'[Score]), FILTER(ALLEXCEPT('Data',Company), 'Raw Data'[Year] = MaxSelectedYear))
+
CALCULATE(SUM('Data'[Score]), FILTER(ALLEXCEPT('Data',Company), 'Raw Data'[Year] = OnePriorYear))
+
CALCULATE(SUM('Data'[Score]), FILTER(ALLEXCEPT('Data',Company), 'Raw Data'[Year] = TwoPriorYear))
+
CALCULATE(SUM('Data'[Score]), FILTER(ALLEXCEPT('Data',Company), 'Raw Data'[Year] = ThreePriorYear))
+
CALCULATE(SUM('Data'[Score]), FILTER(ALLEXCEPT('Data',Company), 'Raw Data'[Year] = FourPriorYear))
,
IF(OnePriorYear < MinYearInData, 1,
IF(TwoPriorYear < MinYearInData,2,
IF(ThreePriorYear < MinYearInData, 3,
IF(FourPriorYear < MinYearInData, 4,5)
)
)
),
BLANK()
)
预期输出:
Company A - 100%
Company B - 80%
Company C - 100%
Company D - 60%