多维数据集可以使用计算函数sum, avg,max,min,abs,round。
区别于python源生的计算函数,使用deepcube的计算函数需要先import deepcube的函数库。
from deepcube.cube import function as fn
如下例子是一个开启了累计数计算的财务模型(未启用变动维),数据录入类型为当期发生额的资产负债类科目年末结转场景,将2022年的所有叶子节点的期间(1-12月)求和,再加上2022年的年初数,计算到2023年的年初数:
from deepcube.cube.cube import deepcube
from deepcube.cube import function as fn
def main(p1, p2):
# 从调用方传参中获取当前年,例如2023
cur_year = p2['year']
cube1 = deepcube('cube1')
year = cube1.year
period = cube1.period
scenario = cube1.scenario
version = cube1.version
account = cube1.account
view = cube1.View
entity = cube1.entity
# 从cube加载数据,需要指定取数的范围。
cube1.init_data(
[year[cur_year].shift(-1),
scenario["Actual"],
version["Working"],
period["TotalPeriod"].Base() + period["Beginning"],
account["Total"].Base()],
[year[cur_year],
scenario["Actual"],
version["Working"],
period["Beginning"],
account["Total"].Base()],
)
# 确定一个背景scope范围
cube1.scope(year[cur_year],
scenario["Actual"],
version["Working"],
account["Total"].Base())
# 以下两种写法等价
# 写法一,用+连接period["TotalPeriod"].Base()和period["Beginning"],对这个大的成员集合做sum
cube1.loc[period["Beginning"]] = fn.sum(cube1.loc[
period["TotalPeriod"].Base()+period["Beginning"], year[last_year]])
# 写法二,将period["TotalPeriod"].Base()的sum结果与Beginning做加法
cube1.loc[period["Beginning"]] = fn.sum(cube1.loc[
period["TotalPeriod"].Base(), year['2022']]) + cube1.loc[
period["Beginning"], year[last_year]]
fn.avg(),fn.max(),fn.min(),fn.abs()分别用于计算表达式的平均值,最大值,最小值和绝对值,这几个函数与fn.sum()的语法一样,可以在其中传入cube.loc[…]或多个cube.loc[…]的计算公式。
fn.round()用于将计算结果进行四舍五入,传参除了跟其他计算函数一样的表达式之外,还需要传入要四舍五入的小数位数。
并且多个计算函数互相可以嵌套,以下例子将科目成员Amount_Total的base节点中,ud1为Y的成员求和,并保留2位小数,计算至Amount_S成员:
from deepcube.cube.cube import deepcube
from deepcube.cube import function as fn
def main(p1, p2):
cube1 = deepcube('cube1')
year = cube1.year
period = cube1.period
scenario = cube1.scenario
version = cube1.version
account = cube1.account
view = cube1.View
entity = cube1.entity
# 确定一个背景scope范围
cube1.scope(year["2022", "2023"],
scenario["Actual"],
version["Working"],
period["TotalPeriod"].Base(),
account["Total"].Base())
# 从cube加载数据,需要指定取数的范围。
cube1.init_data()
acc_mbrs = account["Amount_Total"].Base()
cube1.loc[account["Amount_S"]] = fn.round(
fn.sum(cube1.loc[acc_mbr.loc[acc_mbrs.attr('ud1') == 'Y']]), 2)
如果需要对所有计算都四舍五入保留某一个小数位数,可以使用全局设置global_rounding_decimals。如下例就将全局的每次计算都设置为四舍五入保留4位小数。
此外,如果在设置了全局小数位数的同时,在单条计算中使用round函数,则此条计算会使用round函数中指定的小数位数,其他计算仍然使用全局小数位数。
from deepcube import Config
Config.global_rounding_decimals = 4
回到顶部
咨询热线