如何使用多维数据集函数在Excel中过滤多个维度?

我读过这篇文章( 使用多维数据集函数来获得过滤的维度 ),这是相当有帮助的,但我想添加一个更多级别的过滤。

所以我们假设在PowerPivot中我的数据如下所示:

Month Category Product # Revenue January Trucks 00000001 $50000 January Trucks 00000002 $75000 January Cars 00000005 $45000 January Cars 00000008 $90000 January Trucks 00000003 $10000 February Cars 00000005 $10000 

所以基本上我有汽车或卡车,我想在1月份,2月份的时候,把每个类别中最畅销的2款产品归还。

我可以很容易地find最畅销的产品,如果我只有一个维度过滤。 所以我可以在一月份find最畅销的产品(卡车或汽车)。 我在上面提供的链接中使用了该方法。 但是我想在这一层增加一层,说一月份只能find最畅销的车。

我怎么去做这个? 我希望我可以使用“非空”,只是添加每个过滤的维度/条件,我可以,但也许我不明白语法应该如何。

我已经创build了一个自定义函数,可以满足您的要求,因此可以避免您的车辆销售数据的多维报告所需的多重嵌套多维数据集function的复杂devise和维护。

另一个优点是使用这种方法,可以很容易地创build和编辑多种变化,以提供用于未来报告需求的附加function。

用法与Excel内置的Rank函数类似:

  • 从工作表中调用函数,指定月份和/ 类别,以及您需要返回的排名结果“排名”。

例如,为了获得1月份销售的顶级(#1)卡车,您可以使用公式:

 =GetTopSeller ( "January", "Trucks", 1 ) 

或者,要获得单元格A1中列出的月份的第十畅销车,您可以使用公式:

 =GetTopSeller ( A$1, "Cars", 10 ) 

下图显示了语法和用法 ,以及用于testing函数的示例数据集 ,以及基于示例数据*的示例输出

语法和用法,示例数据,示例输出

 Option Explicit 'Written by ashleedawg@outlook.com for https://stackoverflow.com/q/47213812 Const returnForNoValue = "" 'could be Null, 0, "(Not Found)", etc Public cnn As New ADODB.Connection Public rs As New ADODB.Recordset Public strSQL As String 'Additional features which can be easily added on request if needed: ' add constants to specify Revenue or Product ID ' allow annual reporting ' allow list of vehicle types, months, etc ' make case insensitive Public Function GetTopSeller(sMonth As String, sCategory As String, _ sMonthRank As Integer) As Variant() '1=ProductID 2=Revenue Dim retData(1 To 2) As Variant strSQL = "Select Month, Category, [Product #], Revenue from [dataTable$] " & _ "WHERE [Month]='" & sMonth & "' AND [Category]='" & sCategory & "' _ Order by [Revenue] DESC" ' close Excel Table DB _before_ opening If rs.State = adStateOpen Then rs.Close rs.CursorLocation = adUseClient ' open Excel Table as DB If cnn.State = adStateOpen Then cnn.Close cnn.ConnectionString = "Driver={Microsoft Excel Driver " & "(*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=" & _ ActiveWorkbook.Path & Application.PathSeparator & ActiveWorkbook.Name cnn.Open ' find appropriate data With rs .Open strSQL, cnn, adOpenKeyset, adLockOptimistic .MoveFirst If (.RecordCount <= 0) Or (.RecordCount < sMonthRank) _ Or (sMonthRank = 0) Then GoTo queryError 'move the Nth item in list .Move (sMonthRank - 1) retData(1) = ![Product #] retData(2) = !Revenue End With 'return value to the user or cell GetTopSeller = retData Exit Function queryError: 'error trapped, return no values retData(1) = returnForNoValue retData(2) = returnForNoValue GetTopSeller = retData End Function 

下面是将函数复制到工作簿中的说明,从而使其可以作为工作表函数访问。 或者,示例工作簿可以保存为加载项,然后通过创build对加载项的引用从任何工作簿进行访问。


如何将VBA函数添加到您的工作簿中:

  1. select下面的VBA代码,然后按Ctrl + C进行复制。

  2. 在您的Excel工作簿中,点击Alt + F11打开VBA编辑器(又名VBE )。

  3. 单击VBE中的插入菜单,然后select模块

  4. 按Ctrl + V粘贴代码。

  5. 单击VBE中的debugging菜单,然后select**编译项目*。 这将检查代码是否有错误。 理想情况下, “无”将会发生,这意味着它没有错误和良好的去。

  6. 单击VBE右上angular的“ ”closuresVBE窗口。

  7. 保存工作簿。 您的新function现在可以使用


function的使用应该是不言自明的,但如果您需要更改,遇到问题或有任何疑问,请不要犹豫与我联系。

祝你好运!