当前位置:网站首页>BI技巧丨当月期初
BI技巧丨当月期初
2022-06-12 07:26:00 【PowerBI丨白茶】

最近在群里,小伙伴提出了一个有意思的算法逻辑,决定分享给大家。
先来看看本期的案例数据:

如图,数据相对而言比较简单,小伙伴的具体需求如下:
1.用每月最早日期的数据,减去上月最晚日期的数据;
2.如果为最早月份,则上月数据为0;
3.数据如果出现断档,则当月减之前最近月份的最晚日期数据;
4.结果只呈现每月最早日期的结果;
5.Total汇总逻辑为每月最早日期的结果求和。

从实际业务的角度出发,我们日常打交道最多的模块,就是财务模块了。而针对不同的行业,每个财务模块对于一些业务逻辑的定义和算法,都是有区别。
根据小伙伴提出的需求,我们将其导入到PowerBI中。

思考一下,为了实现小伙伴的需求,我们需要注意的点有哪些呢?
1
2
3
…
1.数据存在断档情况,因此我们需要特殊处理;
2.每月数据有不规律的多条,我们需要取到最小日期和最大日期;
3.总计需要进行结果汇总,因此涉及到了迭代函数。
缕清思路,我们就可以解决上述需求了。
构建基础指标
添加如下度量值,这样做的目的,是方便后面的函数引用,以减少代码书写和提升运行性能。
001.Value =
SUM ( 'Fact_Data'[TSN Value] )
计算上一区间最大日期的数据
代码如下:
002.LastMonthMaxDateValue =
VAR CurrentMonth =
MAX ( 'Fact_Data'[Month] )
VAR LastMonth =
CALCULATETABLE (
VALUES ( 'Fact_Data'[Last Update] ),
FILTER ( ALL ( 'Fact_Data' ), 'Fact_Data'[Month] < CurrentMonth )
)
VAR LastMonthMaxDate =
TOPN ( 1, LastMonth, 'Fact_Data'[Last Update], DESC )
VAR LastMonthMaxDateValue =
CALCULATE (
[001.Value],
FILTER ( ALL ( 'Fact_Data' ), 'Fact_Data'[Last Update] = LastMonthMaxDate )
)
RETURN
IF ( [001.Value] <> BLANK (), LastMonthMaxDateValue, BLANK () )
结果如下:

这里和小伙伴解释一下,代码每一层的用意:
1.变量CurrentMonth是为了获取当前上下文所对应的年月字段;
2.变量LastMonth通过CALCULATETABLE函数,获取小于当前年月的所有日期;
3.变量LastMonthMaxDate利用TOPN函数,获取小于当前年月所有日期中的最大日期;
4.变量LastMonthMaxDateValue利用CALCULATE函数重置上下文,计算距今最近区间最大日期的数据。
5.结果输出,简单的逻辑判定是否数据存在为空的情况。
计算每日当期值
代码如下:
003.CurrentDateValue =
[001.Value] - [002.LastMonthMaxDateValue]
结果如下:

计算每月期初的当期值
代码如下:
004.CurrentMonthMinValue =
VAR CurrentMinDate =
CALCULATE (
MIN ( 'Fact_Data'[Last Update] ),
ALL ( 'Fact_Data' ),
VALUES ( 'Fact_Data'[Month] )
)
VAR CurrentMonthMinValue =
CALCULATE (
[003.CurrentDateValue],
FILTER (
ALL ( 'Fact_Data'[Last Update] ),
'Fact_Data'[Last Update] = CurrentMinDate
)
)
RETURN
IF ( [001.Value] <> BLANK (), CurrentMonthMinValue, BLANK () )
结果如下:

解释说明:
1.变量CurrentMinDate通过CALCULATE与MIN函数的组合,获取最小日期,ALL函数的作用是为了移除其他维度的筛选效果,VALUES将年月字段的筛选效果追加回来,以获取每月的最小日期;
2.变量CurrentMonthMinValue通过CALCULATE与ALL的组合,计算每月最小日期的当期值,即当月期初;
3.结果输出,简单的逻辑判定是否数据存在为空的情况。
解决总计问题
代码如下:
005.Result =
SUMX ( VALUES ( 'Fact_Data'[Month] ), [004.CurrentMonthMinValue] )
结果如下:

通过SUMX迭代器,解决总计汇总的问题。


Demo文件在知识星球。
这里是白茶,一个PowerBI的初学者。
边栏推荐
- Exploring shared representations for personalized federated learning paper notes + code interpretation
- 8 IO Library
- Planning and design of 1000 person medium-sized campus / enterprise network based on ENSP and firewall (with all configuration commands)
- 新知识:Monkey 改进版之 App Crawler
- Problems encountered in learning go
- Construction of running water lamp experiment with simulation software proteus
- Study on display principle of seven segment digital tube
- 五、EL 表达式& JSTL 标签库
- Installation and use of eigen under vs2017
- tmux 和 vim 的快捷键修改
猜你喜欢

Study on display principle of seven segment digital tube
![[Li Kou] curriculum series](/img/eb/c46a6b080224a71367d61f512326fd.jpg)
[Li Kou] curriculum series

Kotlin插件 kotlin-android-extensions

Scons compiling imgui

Summary of machine learning + pattern recognition learning (I) -- k-nearest neighbor method

Personalized federated learning using hypernetworks paper reading notes + code interpretation

Introduction to JDE object management platform and use of from

sql——课程实验考查

Pyhon的第四天

Set up a remote Jupiter notebook
随机推荐
i. Mx6ul porting openwrt
Map to sort
Study on display principle of seven segment digital tube
我人生中的第一个需求——Excel数据批量上传到数据库
RT thread studio learning summary
Paddepaddl 28 supports the implementation of GHM loss, a gradient balancing mechanism for arbitrary dimensional data (supports ignore\u index, class\u weight, back propagation training, and multi clas
Difference and application of SPI, UART and I2C communication
Freshmen are worried about whether to get a low salary of more than 10000 yuan from Huawei or a high salary of more than 20000 yuan from the Internet
Why must coordinate transformations consist of publishers / subscribers of coordinate transformation information?
[image detection] SAR image change detection based on depth difference and pcanet with matlab code
Nine project management issues that PM should understand
Problems encountered in learning go
Static coordinate transformation in ROS (analysis + example)
Putty installation and use
Day 5 of pyhon
Use of gt911 capacitive touch screen
Detailed explanation of memory addressing in 8086 real address mode
d的扩大@nogc
RT thread studio learning (IX) TF Card File System
Detailed explanation of multi coordinate transformation in ROS (example + code)