如何在Power BI中计算截至当前月份的累计值并隐藏后续月份?
Power BI累计求和并隐藏后续无数据月份
原始数据
| 月份 | 数值 |
|---|---|
| 2023年1月 | 2 |
| 2023年2月 | 4 |
| 2023年3月 | 0 |
需求
计算截至当前月份的累计求和值,仅显示到最后一个有非零数值的月份,后续无数据(数值为0)的月份不显示。预期结果:
2023年1月 2
2023年2月 6
不应显示3月
现有问题
使用以下DAX公式时,3月会被计算出累计值6并显示:
Actuals running total in Month 2: = CALCULATE( SUM('BI_ClientAdoptionReport'[Actuals]), FILTER( ALL('BI_ClientAdoptionReport'[Month]), ISONORAFTER('BI_ClientAdoptionReport'[Month], MAX('BI_ClientAdoptionReport'[Month]), DESC) ) )
解决方案
修正后的DAX公式如下:
累计求和 = VAR LastValidMonth = CALCULATE( MAX('BI_ClientAdoptionReport'[Month]), 'BI_ClientAdoptionReport'[Actuals] <> 0 ) RETURN IF( MAX('BI_ClientAdoptionReport'[Month]) > LastValidMonth, BLANK(), CALCULATE( SUM('BI_ClientAdoptionReport'[Actuals]), FILTER( ALL('BI_ClientAdoptionReport'[Month]), 'BI_ClientAdoptionReport'[Month] <= MAX('BI_ClientAdoptionReport'[Month]) ) ) )
逻辑说明
LastValidMonth变量:定位到最后一个存在非零实际值的月份,本例中为2023年2月。- 空值判断:如果当前行的月份晚于这个有效月份,返回空值(
BLANK()),Power BI默认会隐藏空值对应的行。 - 累计计算:对于有效范围内的月份,计算从起始到当前月份的累计求和。
内容的提问来源于stack exchange,提问作者deeeeeeeeeeee
相关产品推荐
相关产品推荐

