简体   繁体   English

使用 Excel 数据透视表作为另一个数据透视表的数据源

[英]Use Excel pivot table as data source for another Pivot Table

I have a Pivot table in excel that is using a raw table as its data source.我在 excel 中有一个数据透视表,它使用原始表作为其数据源。 This pivot table is doing a bunch of grouping and summing of rows.这个数据透视表正在对行进行一堆分组和求和。

I'd like to now use the result of this new pivot table as the data source for a new pivot table which will further modify this data.我现在想使用这个新数据透视表的结果作为新数据透视表的数据源,它将进一步修改这些数据。

Is this possible with excel?这可以用excel吗? I suppose you could call it 'nested pivot tables'我想你可以称之为“嵌套数据透视表”

In a new sheet (where you want to create a new pivot table) press the key combination (Alt+D+P). 在新工作表中(您要在其中创建新的数据透视表),按下组合键(Alt + D + P)。 In the list of data source options choose "Microsoft Excel list of database". 在数据源选项列表中,选择“Microsoft Excel数据库列表”。 Click Next and select the pivot table that you want to use as a source (select starting with the actual headers of the fields). 单击“下一步”,然后选择要用作源的数据透视表(从字段的实际标题开始选择)。 I assume that this range is rather static and if you refresh the source pivot and it changes it's size you would have to re-size the range as well. 我假设这个范围是相当静态的,如果你刷新源枢轴并改变它的大小,你也必须重新调整范围。 Hope this helps. 希望这可以帮助。

  • Make your first pivot table. 制作第一个数据透视表。

  • Select the first top left cell. 选择第一个左上角的单元格。

  • Create a range name using offset: 使用offset创建范围名称:

    OFFSET(Sheet1!$A$3,0,0,COUNTA(Sheet1!$A:$A)-1,COUNTA(Sheet1!$3:$3))

  • Make your second pivot with your range name as source of data using F3. 使用F3将您的范围名称作为数据源进行第二次调整。

If you change number of rows or columns from your first pivot, your second pivot will be update after refreshing pivot 如果您更改第一个数据透视表中的行数或列数,则刷新数据透视后将更新第二个数据透视表

GFGDT GFGDT

You have to convert the pivot to values first before you can do that: 您必须先将数据透视转换为值,然后才能执行此操作:

  • Remove the subtotals 删除小计
  • Repeat the row items 重复行项目
  • Copy / Paste values 复制/粘贴值
  • Insert a new pivot table 插入新的数据透视表

As @nutsch implies, Excel won't do what you need directly, so you have to copy your data from the pivot table to somewhere else first. 正如@nutsch所暗示的那样,Excel不会直接执行您所需的操作,因此您必须先将数据从数据透视表复制到其他位置。 Rather than using copy and then paste values, however, a better way for many purposes is to create some hidden columns or a whole hidden sheet that copies values using simple formulae. 但是,与其使用复制然后粘贴值不同,更好的方法是创建一些隐藏列或使用简单公式复制值的整个隐藏表。 The copy-paste approach isn't very useful when the original pivot table gets refreshed. 当原始数据透视表被刷新时,复制粘贴方法不是很有用。

For instance, if Sheet1 contains the original pivot table, then: 例如,如果Sheet1包含原始数据透视表,则:

  • Create Sheet2 and put =Sheet1!A1 into Sheet2!A1 创建Sheet2并将=Sheet1!A1放入Sheet2!A1
  • Copy that formula around as many cells in Sheet2 as required to match the size of the original pivot table. 根据需要在Sheet2中围绕尽可能多的单元格复制该公式,以匹配原始数据透视表的大小。
  • Assuming that the original pivot table could change size whenever it is refreshed, you could copy the formula in Sheet2 to cover the whole of the potential area the original pivot table could ever take. 假设原始数据透视表在刷新时可以更改大小,您可以复制Sheet2中的公式以覆盖原始数据透视表可能占用的整个潜在区域。 That will put lots of zeros in cells where the original cells are currently empty, but you could avoid that by using the formula =IF(Sheet1!A1="","",Sheet1!A1) instead. 这将在原始单元格当前为空的单元格中放置大量零,但您可以通过使用公式=IF(Sheet1!A1="","",Sheet1!A1)来避免这种情况。
  • Create your new pivot table based on a range within Sheet2, then hide Sheet2. 根据Sheet2中的范围创建新的数据透视表,然后隐藏Sheet2。

Personally, I got around this in a slightly different way - I had a pivot table querying an SQL server source and I was using the timeline slicer to restrict the results to a date range - I then wanted to summarise the pivot results in another table. 就个人而言,我以一种略微不同的方式解决了这个问题 - 我有一个数据透视表查询SQL服务器源,我使用时间线切片器将结果限制为日期范围 - 然后我想在另一个表中汇总数据结果。

I selected the 'source' pivot table and created a named range called 'SourcePivotData'. 我选择了'source'数据透视表并创建了一个名为'SourcePivotData'的命名范围。

Create your summary pivot tables using the named range as a source. 使用命名范围作为源创建摘要数据透视表。

In the worksheet events for the source pivot table, I put the following code: 在源数据透视表的工作表事件中,我输入以下代码:

Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)

'Update the address of the named range
ThisWorkbook.Names("SourcePivotData").RefersTo = "='" & Target.TableRange1.Worksheet.Name & "'!" & Target.TableRange1.AddressLocal

'Refresh any pivot tables that use this as a source
Dim pt As PivotTable
Application.DisplayAlerts = False
For Each pt In Sheet2.PivotTables
    pt.PivotCache.Refresh
Next pt
Application.DisplayAlerts = True

End Sub

Works nicely for me! 对我来说很好用! :) :)

here is how I've done this before. 这是我以前做过的方式。

  1. put a dummy column "X" off to the right of your source pivot table. 将虚拟列“X”放在源数据透视表的右侧。
  2. click in that cell and start your pivot table. 单击该单元格并启动数据透视表。
  3. once the dialogue box pops up you can edit the data range to include your pivot table. 弹出对话框后,您可以编辑数据范围以包含数据透视表。
  4. this may require you to Refresh the source table first and then refresh your secondary pivot table...or do refresh all twice 这可能需要您先刷新源表,然后刷新辅助数据透视表...或者刷新所有两次

I guess your end goal is to show Distinct (unique) values inside your original Pivot table. 我猜你的最终目标是在原始数据透视表中显示不同(唯一)值。

For example you could have data set with OrderNumber, OrderDate, OrderItem, orderQty 例如,您可以使用OrderNumber,OrderDate,OrderItem,orderQty设置数据

First pivot table will show you OrderDate and sum of OrderQty and you probably want to see Count of unique orders in the same Pivot . 第一个数据透视表将显示OrderDate和OrderQty的总和, 您可能希望在同一个数据透视表中看到唯一订单的数量 You woudln't be able to do so within standard pivot table 您无法在标准数据透视表中执行此操作

If you want to do it, you would need Office 2016 (or perhaps pover Pivot might work). 如果你想这样做,你需要Office 2016(或者可能是pover Pivot可能会工作)。 In office 2016 select your data > Insert > Pivot Table > choose tick "Add this data to the Data Model" 在办公室2016中选择您的数据>插入>数据透视表> 选择勾选“将此数据添加到数据模型”

After that, you would be able to select grouping method as Distinct (Count) 之后,您可以选择分组方法为Distinct(Count)

As suggested you can change the pivot table content and paste as values. 如建议您可以更改数据透视表内容并粘贴为值。 But if you want to change the values dynamically the easiest way I found is Go To Insert->create pivot table Now in the dialog box in the input data field select the cells of your previous pivot table. 但是如果要动态更改值,我发现的最简单方法是转到Insert->create pivot table在,在输入数据字段的对话框中,选择上一个数据透视表的单元格。

I love the offset option -- i had to create a range called that first and did a -3 instead of a -1.我喜欢偏移选项——我必须先创建一个称为该范围的范围,然后执行 -3 而不是 -1。 Then used that new range for my new pivot table.然后将这个新范围用于我的新数据透视表。 Now when the first pivot changes, the second will pickup the new rows (or less rows)...现在,当第一个枢轴更改时,第二个将拾取新行(或更少的行)...

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

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