简体   繁体   English

Excel数据透视表问题-如何获取行区域字段的小计?

[英]Excel pivot table question - How to get subtotals for a row area field?

I have the following situation in database: 我在数据库中有以下情况:

Department table holds all departments in the company. 部门表包含公司中的所有部门。

Employee table holds all employees, together with their department id and AllowedAbsenceDays field, which tells us how many days is the employee alowed to be absent in the current year. Employee表保存所有员工,以及他们的部门ID和AllowedAbsenceDays字段,该字段告诉我们当年允许缺勤多少天。

Absence table holds all absences that occur in the company. 缺勤表保存公司中发生的所有缺勤情况。 It has employee id and date of absence, and also the reason of the absence (vacation, sick leave, personal days, and potentially more in the future...) 它具有员工ID和缺勤日期,以及缺勤原因(休假,病假,个人工作日,以及将来可能更多的原因...)

替代文字

I am supposed to create a report which lists all employees and shows how many days they are allowed to be absent together with how many days have they been absent (and what is the reason of absence). 我应该创建一个报告,其中列出所有雇员,并显示允许他们缺勤多少天,以及他们缺勤了多少天(以及缺勤的原因是什么)。

I have grouped the data by employee and by reason of absence: 我已按员工和缺勤原因对数据进行了分组:

替代文字

This is what I am able to get so far, by putting the Department, Employee and Allowed Absence Days in the pivot row area, Reason in the column area and Sum of Days absent in the data area: 通过将“部门”,“员工”和“允许缺勤日”置于数据透视表行区域,“原因”置于列区域以及“数据天数缺失”,我到目前为止可以做到这一点:

替代文字

The problem is that I am not able to get the subtotals of allowed absence days per department (which is a firm requirement). 问题是我无法获得每个部门允许的缺勤天数小计(这是一项严格的要求)。 The final report should look somewhat like this (i have photoshopped this). 最终报告应该看起来像这样(我已经把它照相了)。

替代文字

Is there any way to get these subtotals? 有什么办法可以得到这些小计? Maybe I should prepare the data for the pivot table in a different way? 也许我应该以其他方式为数据透视表准备数据? Please note that the Grand Total column should include only the actual days of absence (not the Allowed Absence Days). 请注意,“总计”列应仅包括实际缺勤天数(而不是“允许缺勤天数”)。

Example workbook is available for download here 示例工作簿可在此处下载

Thanks to everybody who is still reading :) 感谢每个仍在阅读的人:)

PS The real case is different (in the problem domain). PS实际情况有所不同(在问题域中)。 This is a somewhat contrived example, but the basic problem is the same. 这是一个有些人为的示例,但是基本问题是相同的。

It's a classic data normalisation issue. 这是经典的数据标准化问题。 The "Allowed absence days" field relates to the employee and not to any particular absence. “允许的缺勤天数”字段与员工相关,与任何特定的缺勤无关。 The repetition of this info in every row (record) is what is causing the problem. 导致此问题的原因是在每一行(记录)中重复此信息。

To achieve this within an Excel sheet and pivot table, you could remove the "Allowed absence days" column and instead use "Allowed absence days" as one of the values in the "Reason" column. 要在Excel工作表和数据透视表中实现此目的,您可以删除“允许的缺勤天数”列,而可以使用“允许的缺勤天数”作为“原因”列中的值之一。 It might be beneficial to either show allowed days as a negative amount or to show days for the other absence reasons as negatives. 将允许的天数显示为负数或将其他缺勤原因的天数显示为负数可能会有所帮助。 Otherwise the overall grand total will make no real sense. 否则,总体总数将毫无意义。

Sheet and pivot table should look roughly like this: 工作表和数据透视表应大致如下所示:

Excel spreadsheet showing restructured data: column A is department, B is employee, C is days absent, D is reason. Excel电子表格,其中显示了重组后的数据:列A为部门,列B为员工,列C为缺勤天,列D为原因。 Allowed absence days is now a reason and associated days absent is negative http://img527.imageshack.us/img527/1979/so1.png 现在允许的缺勤天数是个原因,而缺勤天数是负数http://img527.imageshack.us/img527/1979/so1.png

Excel透视表显示C到E列中缺席的天数总和。C列中允许缺勤的天数

Repeating the department for every employee doesn't seem ideal but I suppose an employee could change department during the year so you might/might not have to account for that somehow. 对每个员工重复部门似乎并不理想,但我想一个员工可以在这一年内更换部门,因此您可能/不必以某种方式考虑这一点。 You also need it to make the pivot table work anyway 您还需要它来使数据透视表始终起作用

If you absolutely 100% cannot have the grand total affected in this way then I don't believe that you can achieve this with a pivot table. 如果您绝对不能100%以这种方式影响总计,那么我不认为您可以使用数据透视表来实现这一目标。 You might want to look at using ADO to query the sheet and use some VBA macros to format and output the results. 您可能想看看使用ADO查询工作表并使用一些VBA宏来格式化和输出结果。 This page should give you some idea of what's involved 该页面应让您对所涉及的内容有所了解

Just leave "allowed absence days" blank in all rows, and then at the bottom/top put in rows with the wished for numbers for "allowed absence days" and blanks for the other stuff. 只需在所有行中将“允许缺勤天数”留空,然后在底部/顶部将希望填入“允许缺勤天数”的数字填入行,并在其他地方填入空白。

Then just put your "allowed absence days" in data instead of columns in the pivot and drag it to the left. 然后,将您的“允许缺勤天数”放到数据中,而不是在数据透视表中将列拖到左侧。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM