简体   繁体   English

如何通过Excel数据透视表中的单元格公式过滤日期?

[英]How do I filter dates by a cell formula in an Excel pivot table?

I am attempting to make some reports in Excel using a pivot table of a Sql server view. 我正在尝试使用Sql Server视图的数据透视表在Excel中进行一些报告。 One of the customer requirements is to be able to filter the reports based on a date range they select, which seems reasonable. 客户要求之一是能够根据他们选择的日期范围来过滤报告,这似乎是合理的。 The end user process needs to be fairly simple, so my preference is to have a cell for begin date and one for end date and allow the user to enter these and refresh the table. 最终用户流程必须非常简单,所以我的首选是为开始日期使用一个单元格,为结束日期使用一个单元格,并允许用户输入这些内容并刷新表格。

There are two problems which I haven't been able to get past: 我无法解决两个问题:

  1. Selecting a date filter doesn't appear to provide any way for me to reference a cell as a source for the value. 选择日期过滤器似乎无法为我提供任何方式来引用单元格作为值的来源。 I have to put it in at the time. 我当时必须把它放进去。

  2. It seems as if it is only possible to define a date range if I am grouping on the date field. 如果我在日期字段上进行分组,似乎只能定义一个日期范围。 For some of the reports, this violates the specification. 对于某些报告,这违反了规范。 The Report Filter section of a pivot table only seems to allow me to filter by individual values rather than a range. 数据透视表的“报告过滤器”部分似乎仅允许我按单个值而不是范围进行过滤。 This gets unmanageable on real data which might have hundreds of dates to go through. 对于可能要经历数百个日期的真实数据,这变得难以管理。

One thing I have tried is to create an intermediate table without any aggregates columns in a separate sheet in the same workbook, and then filter that. 我尝试过的一件事是创建一个中间表,该表在同一工作簿的单独工作表中没有任何聚合列,然后对其进行过滤。 This hid the columns correctly in that intermediate table, but had no effect on the pivot table which derived from it. 这样可以正确地将列隐藏在该中间表中,但对从其派生的数据透视表没有任何影响。

Does anyone have ideas for how I can make this work? 有人对我如何进行这项工作有想法吗? It seems like a fairly obvious use case - is there something I am missing about how pivot tables are supposed to be used? 似乎是一个非常明显的用例-关于应该如何使用数据透视表,我是否缺少某些东西?

The easies thing you can do is changing your pivot table query on the fly. 您可以轻松完成的工作就是即时更改数据透视表查询。

When the source cell is changed ( Worksheet_Changed event), append " AND WhateverDate > '1/1/1111'" to Me.PivotTables("Your pivot").PivotCache.CommandText and refresh the pivot. 当源单元格发生更改时( Worksheet_Changed事件),将" AND WhateverDate > '1/1/1111'"追加到Me.PivotTables("Your pivot").PivotCache.CommandText并刷新数据透视表。

I remember I solved a similar requirement some years ago by making the SQL query dynamic (ie putting it together as a string in VBA, and using the cells with the range values in the where condition). 我记得几年前我通过使SQL查询动态化(即将其作为字符串在VBA中放在一起,并在where条件中使用具有范围值的单元格)解决了类似的要求。 ant the VBA macor was then called with a button labeled "refresh". 然后,使用标记为“刷新”的按钮调用VBA宏。

But I am sorry do not have this code available any more, I just remember the general idea. 但是很抱歉,现在没有此代码了,我只记得总体思路。

This has been dead for a while, but I thought I would post a different method. 这已经有一段时间了,但是我想我会发布另一种方法。 add a formula to the source table that returns a '1' for thing you want, and a '0' for things you don't, and then add a filter in the pivot table that filters only the '1'. 向源表添加一个公式,该公式针对您想要的事物返回“ 1”,对于您不需要的事物返回“ 0”,然后在数据透视表中添加仅过滤“ 1”的过滤器。

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

相关问题 在 Excel 中使用公式获取数据透视表单元格值 - Get a Pivot Table Cell value with a formula in Excel Excel数据透视表:无法按日期过滤 - Excel pivot table: Can't filter on dates 如何在Excel的PowerPivot数据透视表中将单元格值链接为过滤器? - How to link cell value as a filter in PowerPivot Pivot Table in Excel? 如何根据单元格值过滤 Excel pivot 表? - How to filter Excel pivot table based on Cell value? 如何创建 Power Query 自定义列并将 excel 公式插入到最终表格的每个单元格中? - How do i create a Power Query custom column AND insert an excel formula into each cell of the final table? 如何在此 Excel Pivot 表中添加过滤器以返回小于 10,000,000 美元的值? - How do I add a filter to return values less than $10,000,000 in this Excel Pivot Table? 使用单元格值过滤日期之间的数据透视表 - Filter Pivot Table Between Dates Using Cell Values Excel数据透视表按单元格值筛选并且单元格值不存在 - Excel Pivot table filter by cell value and cell value does not exist 如何将单元格“地址”公式嵌套到VLookup中? 在Excel中 - How do I nest the Cell “Address” Formula into a VLookup? in Excel VBA Excel,如何在单元格中插入公式? - VBA Excel, how do i insert a formula into a cell?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM