场景背景:某门店预算项目,已有各品类的单价数据,各个门店各品类的销售折扣,销量数据。计算各门店各品类的销售金额,并计算各门店平均折扣和各品类的平均折扣。
财务模型搭建如下:
计算过程:
1,计算所有base节点的
实际单价=单价*折扣率
销售金额=实际单价*销售量
无折扣销售金额=单价*销售量
2,计算所有entity,category的非base节点的
折扣率=销售金额/无折扣销售金额 ,并保留四位小数
脚本如下:
from deepcube.cube.cube import deepcube
from deepcube.cube import function as fn
from deepfos.options import OPTION
def main(p1, p2):
# 获取表单传参的year,version,period,赋值到变量中
cur_year = p2['year']
cur_version = p2['version']
cur_period = p2['period']
# 实例一个deepcube对象,传参为cube元素名和path,如果cube元素名在应用中唯一,可以不传path
cube1 = deepcube('cube1', path='/deepcubeCase/cube')
# 为了后续计算写法简单,在这里将cube的维度赋值给同名变量(注意这里cube1.year中的year是字段名)
year = cube1.year
period = cube1.period
scenario = cube1.scenario
version = cube1.version
account = cube1.account
category = cube1.category
entity = cube1.entity
# 从cube加载数据,需要指定取数的范围。
cube1.init_data([year[cur_year],
scenario["Actual"],
version[cur_version],
account["Discount", 'UnitPrice', 'ActualUnitPrice', 'SalesVolume', 'SalesAmount', 'UndiscountedSalesAmount'],
period[cur_period]])
# 确定一个背景scope范围
cube1.scope(year[cur_year],
scenario["Actual"],
version[cur_version],
account["Discount", 'UnitPrice', 'ActualUnitPrice', 'SalesVolume', 'SalesAmount', 'UndiscountedSalesAmount'],
entity["Total"].IDescendant(),
period[cur_period],
category['Total'].IDescendant()
)
# 第一批计算的范围都是base节点
cube1.scope(entity["Total"].Base(), category['Total'].Base())
# 清数,清掉计算1目标集范围
cube1.clear_data(account['ActualUnitPrice', 'SalesAmount', 'UndiscountedSalesAmount'])
# 计算所有base节点的 实际单价=单价*折扣率
cube1.loc[account['ActualUnitPrice']] = cube1.loc[account['Discount']] * cube1.loc[account['UnitPrice']]
# 计算所有base节点的 销售金额=实际单价*销售量
cube1.loc[account['SalesAmount']] = cube1.loc[account['ActualUnitPrice']] * cube1.loc[account['SalesVolume']]
# 计算所有base节点的 无折扣销售金额=单价*销售量
cube1.loc[account['UndiscountedSalesAmount']] = cube1.loc[account['UnitPrice']] * cube1.loc[account['SalesVolume']]
# 计算所有entity,category的非base节点的折扣率=销售金额/无折扣销售金额
# 清数,清掉计算2目标集范围
cube1.clear_data(entity["Total"].IDescendant(), category['Total'].IDescendant() - category['Total'].Base(),
account['Discount'])
cube1.clear_data(entity["Total"].IDescendant() - entity['Total'].Base(), category['Total'].IDescendant(),
account['Discount'])
# 先算entity的IDescendant且category的非base
cube1.loc[entity["Total"].IDescendant(), category['Total'].IDescendant() - category['Total'].Base(), account[
'Discount']] = fn.round(cube1.loc[account['SalesAmount']] / cube1.loc[account['UndiscountedSalesAmount']], 4)
# 再算entity的非base且category的IDescendant
cube1.loc[entity["Total"].IDescendant() - entity['Total'].Base(), category['Total'].IDescendant(), account[
'Discount']] = fn.round(cube1.loc[account['SalesAmount']] / cube1.loc[account['UndiscountedSalesAmount']], 4)
# 将计算结果写入Cube
cube1.submit_calc()
将其绑定至表单保存后执行,最终计算结果如图:
回到顶部
咨询热线