当前位置:网站首页>你了解PowerBI中的去年同期吗
你了解PowerBI中的去年同期吗
2022-07-25 06:18:00 【PowerBI木小桼】
同比是分析指标中最基础的指标,但是你真的了解PowerBI中同比的实现吗?
我们先来看一下常见的我们书写同比的方式
sales = SUM( 'FactSales'[sales] )
sales.PY =
CALCULATE(
[sales],
DATEADD( 'DimDate'[Date], -1, YEAR )
)
sales.PY2 =
CALCULATE(
[sales],
SAMEPERIODLASTYEAR( 'DimDate'[Date] )
)
无论是使用DATEADD还是SAMEPERIODLASTYEAR都可以实现同比的计算,

一如上图所示,看起来没有任何问题,但是当我们把粒度细到天时再来看数据,2022年7月的数据只24号,可是上面两种方法在月维度时计算同比时计算的是2021年7月整月。

这时候很人很多可能会想到用月度至今计算就会正确了,是这样吗,我们来看一下
sales.MTD = TOTALMTD( [sales], 'DimDate'[Date] )
sales.MTD.PY =
CALCULATE(
[sales.MTD],
DATEADD(DimDate[Date], -1, YEAR )
)

会发现即便我们使用了MTD来计算去年同期,可是在月维度上仍然计算的是去年整月的数据,仔细观看数据会发现只需要在计算去年同期时如果当期未发生,就不计算,即因为当期数据只到2022.7.24,所以在计算去年同期时只需要计算到2021.7.24即可。
sales.PY3 =
VAR maxdate =
CALCULATE (
LASTNONBLANK ( 'DimDate'[Date], [sales] ),
ALL ( 'DimDate'[Date] )
)
RETURN
CALCULATE (
[sales],
SAMEPERIODLASTYEAR (
FILTER ( VALUES ( 'DimDate'[Date] ), 'DimDate'[Date] <= maxdate )
)
)

可以看到在月维度上去年同期只计算到了2021.7.24,并且当年未发生的日期上也不再显示值。
但是也会存在特殊情况,比如2020年2月是闰月有29天,2021年2月只有28天,所以在计算2021.2的去年同期时仍然会计算2020.2全月的数据,即计算到2020.2.29,但这正是我们需要的。

边栏推荐
- 深度解析:2022年最火的商业模式链动2+1,是合法模式吗?
- Classic cases of static keywords and block blocks
- Unity 模型简化/合并 一键式插件
- 【datawhale202207】强化学习:策略梯度和近端策略优化
- 剑指 Offer 54. 二叉搜索树的第k大节点
- UML modeling tools Visio, rational rose, powerdesign
- mysql数据库备份和恢复
- (15) [driver development] over written copy
- MySQL中建表时 pk、nn、qu、b、un、zf、ai、g代表的意思
- [datawhale202207] reinforcement learning: strategy gradient and near end strategy optimization
猜你喜欢

New developments in Data Governance: what is the impact of the EU's Data Governance Research Report on China

Unity Animator动画与状态机

剑指 Offer 45. 把数组排成最小的数

Mechanism and principle of multihead attention and masked attention

VSCode 如何开启多个终端?如何横向显示?

四、MFC工具栏、运行时类信息机制、运行时创建机制

The LAF protocol elephant of defi 2.0 may be one of the few profit-making means in your bear market
![[daily practice] day (14)](/img/83/d924fa0fc5ae01d151a0880da62f7e.png)
[daily practice] day (14)

深度解析:2022年最火的商业模式链动2+1,是合法模式吗?

ARM裸板调试之JTAG调试源码级调试
随机推荐
Vbs script COM object extension and use (3)
Android interview question: why do activities rebuild ViewModel and still exist—— Jetpack series (3)
深度解析:2022年最火的商业模式链动2+1,是合法模式吗?
Seekbar attribute reference
Data too long for column ‘data‘ at row 1以及设置成longblob造成的乱码解决。node-mysql
U-boot-1.1.6 transplant notes (beginner)
VSCode 如何开启多个终端?如何横向显示?
(16) [system call] track system call (3 rings)
Leetcode/ number of 1 in the first n digit binary
Qt 5界面修改无效的问题解决QtDesigner修改之后无效的解决办法
mysql数据库备份和恢复
How to play a data mining game entry Edition
Scientific computing library numpy Foundation & Improvement (Understanding + explanation of important functions)
(15) [driver development] over written copy
Sword finger offer 36. binary search tree and bidirectional linked list
都说ScreenToGif是GIF录制神器,却不知其强大之处远不在此
context must be a dict rather解决
嵌入式c语言开发之宏定义求两个数的最大值的使用技巧
How to troubleshoot the problem of too many inodes
(16)[系统调用]追踪系统调用(3环)