如何获取表底部的父列组的总和?

时间:2019-07-08 06:24:32

标签: reporting-services

我有一个有关订单,其类别,订单月份,状态和金额的数据集。     

+----------+---------------+-------+--------+--------+
|  name    |  category     | month | amount | status |
+----------+---------------+-------+--------+--------+
|  order 1 |  electronics  | April |   100  |  Paid  |
|  order 2 |  electronics  | May   |    50  |  Paid  |
|  order 3 |  electronics  | April |    20  |  Credit|
|  order 4 |  stationary   | May   |    10  |  Paid  |
|  order 5 |  stationary   | April |   150  |  Credit|
|  order 6 |  miscellaneous| May   |    40  |  Paid  |
|  order 7 |  miscellaneous| May   |    10  |  Paid  |
|  order 8 |  Grocery      | April |   100  |  Credit|
|  order 9 |  Grocery      | April |    10  |  Credit|
|  order 10|  Grocery      | May   |    20  |  Paid  |
+----------+---------------+-------+--------+--------+
 

我的报告表应如下所示:

    
+----------------+-------+-------+-------+--------+
|                |     April     |      May       |
+----------------+-------+-------+-------+--------+
|  Category      |  Paid | Credit|  Paid| Credit  |
+----------------+-------+-------+-------+--------+
|  electronics   |  100  |   20  |   50  |   0    |
|  stationary    |  0    |   150 |   10  |   0    |
|  miscellaneous |  0    |   0   |   50  |   0    |
|  grocery       |  0    |   110 |   20  |   0    |
|  Grand Total   |  100  |   280 |   130 |   0    |
|  Monthly total |      380      |       0        |
+----------------+-------+-------+-------+--------+
 

除了每月总计,我都可以使用。

行组-按类别分组。

列组-以月为父组,状态为子级。

我尝试在“总计” 下使用表达式=Sum(Fields!amount.Value,"month parent group")添加一个新行,但该行汇总了“付费”和“信用”列下的值。我需要将其合并。

通过将金额相加,我也能够连续获得月份名称下方的合并结果。

但不能在底部做同样的事情。

2 个答案:

答案 0 :(得分:0)

这是您要寻找的结果吗?

enter image description here

如果是,则如下所示,转到[Sum(amount)],然后将总计添加为列,如果将其添加为行,则将与总计总计相同。

现在您可以隐藏每一行的总计,并且仅保留完整的总计。

注意:由于使用了2个列分组,因此您的月度总数不会低于380。

enter image description here

答案 1 :(得分:-1)

我有一个pivot table的Python解决方案。您可以使用以下代码创建表:

import pandas as pd
df = pd.DataFrame({"category": ["electronics", "electronics", "electronics", 
                            "stationary", "stationary","miscellaneous", "miscellaneous", 
                            "Grocery", "Grocery", "Grocery"],
                "month": ["April", "May", "April", 
                            "May", "April","May", "May", 
                            "April", "April", "May"],
                "amount": [100, 50, 20, 10, 150, 40, 10, 100, 10, 20],
                "status": ["Paid", "Paid", "Credit", "Paid", "Credit",
                              "Paid", "Paid", "Credit", "Credit", "Paid"]})

比使用数据透视表

pd.pivot_table(df,index=["month","status"],values=["amount"],
           columns=["category"],aggfunc=[np.sum], fill_value=0, margins = True)

这将为您提供具有总计利润的表。对于每月总计,请将其添加到您的代码中

pd.concat([
d.append(d.sum().rename((k, 'Total')))
for k, d in table.groupby(level=0)
]).append(table.sum().rename(('Grand', 'Total')))

您有需要的东西。