- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已阅读这篇文章 ( use Cube Functions to get filtered dimensions ),它非常有帮助,但我想再添加一级过滤。
假设我的数据在 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 种产品。
如果我只过滤一维,我就可以轻松找到最畅销的产品。这样我就可以找到一月份最畅销的产品(卡车或汽车)。我使用了上面提供的链接中的方法。但我想添加一层,并说,只查找一月份最畅销的卡车。
我该如何去做呢?我希望我可以使用“非空”并根据需要添加每个过滤维度/条件,但也许我不明白语法应该如何。
最佳答案
我创建了一个应该满足您的要求的自定义函数,因此避免了车辆销售数据多维报告所需的多个嵌套多维数据集函数的复杂设计和维护。
另一个优点是,使用此方法,可以轻松创建和编辑多个变体,以便为 future 的报告需求提供附加功能。
用法与Excel内置的Rank
函数类似:
<小时/>For example, to get the Top (#1) selling Truck for January, you could use formula:
=GetTopSeller ( "January", "Trucks", 1 )
or, to get the 10th best selling car for the month listed in cell A1, you could use formula:
=GetTopSeller ( A$1, "Cars", 10 )
下图显示了语法和用法,以及测试函数时使用的示例数据集,以及示例输出基于样本数据*。
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
<小时/>
下面是将函数复制到工作簿中的说明,从而使其可以作为工作表函数进行访问。或者,可以将示例工作簿另存为附加组件,然后通过创建对该附加组件的引用从任何工作簿进行访问。
<小时/>选择下面的 VBA 代码,然后按 Ctrl+C 进行复制。
在 Excel 工作簿中,按 Alt+F11 打开 VBA 编辑器(又名 VBE)。
单击 VBE 中的插入菜单,然后选择模块。
按 Ctrl+V 粘贴代码。
单击 VBE 中的调试菜单,然后选择 **编译项目*。这会检查代码是否有错误。理想情况下,“什么也不会发生”,这意味着它没有错误并且可以正常运行。
通过单击 VBE 右上角的“✘”关闭 VBE 窗口。
保存工作簿。 您的新功能现在可以使用!
该功能的使用应该是不言自明的,但如果您需要更改、遇到问题或有任何疑问,请随时与我联系。
祝你好运!
关于excel - 如何使用立方体函数过滤Excel中的多个维度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47213812/
C语言sscanf()函数:从字符串中读取指定格式的数据 头文件: ?
最近,我有一个关于工作预评估的问题,即使查询了每个功能的工作原理,我也不知道如何解决。这是一个伪代码。 下面是一个名为foo()的函数,该函数将被传递一个值并返回一个值。如果将以下值传递给foo函数,
CStr 函数 返回表达式,该表达式已被转换为 String 子类型的 Variant。 CStr(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CSng 函数 返回表达式,该表达式已被转换为 Single 子类型的 Variant。 CSng(expression) expression 参数是任意有效的表达式。 说明 通常,可
CreateObject 函数 创建并返回对 Automation 对象的引用。 CreateObject(servername.typename [, location]) 参数 serv
Cos 函数 返回某个角的余弦值。 Cos(number) number 参数可以是任何将某个角表示为弧度的有效数值表达式。 说明 Cos 函数取某个角并返回直角三角形两边的比值。此比值是
CLng 函数 返回表达式,此表达式已被转换为 Long 子类型的 Variant。 CLng(expression) expression 参数是任意有效的表达式。 说明 通常,您可以使
CInt 函数 返回表达式,此表达式已被转换为 Integer 子类型的 Variant。 CInt(expression) expression 参数是任意有效的表达式。 说明 通常,可
Chr 函数 返回与指定的 ANSI 字符代码相对应的字符。 Chr(charcode) charcode 参数是可以标识字符的数字。 说明 从 0 到 31 的数字表示标准的不可打印的
CDbl 函数 返回表达式,此表达式已被转换为 Double 子类型的 Variant。 CDbl(expression) expression 参数是任意有效的表达式。 说明 通常,您可
CDate 函数 返回表达式,此表达式已被转换为 Date 子类型的 Variant。 CDate(date) date 参数是任意有效的日期表达式。 说明 IsDate 函数用于判断 d
CCur 函数 返回表达式,此表达式已被转换为 Currency 子类型的 Variant。 CCur(expression) expression 参数是任意有效的表达式。 说明 通常,
CByte 函数 返回表达式,此表达式已被转换为 Byte 子类型的 Variant。 CByte(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CBool 函数 返回表达式,此表达式已转换为 Boolean 子类型的 Variant。 CBool(expression) expression 是任意有效的表达式。 说明 如果 ex
Atn 函数 返回数值的反正切值。 Atn(number) number 参数可以是任意有效的数值表达式。 说明 Atn 函数计算直角三角形两个边的比值 (number) 并返回对应角的弧
Asc 函数 返回与字符串的第一个字母对应的 ANSI 字符代码。 Asc(string) string 参数是任意有效的字符串表达式。如果 string 参数未包含字符,则将发生运行时错误。
Array 函数 返回包含数组的 Variant。 Array(arglist) arglist 参数是赋给包含在 Variant 中的数组元素的值的列表(用逗号分隔)。如果没有指定此参数,则
Abs 函数 返回数字的绝对值。 Abs(number) number 参数可以是任意有效的数值表达式。如果 number 包含 Null,则返回 Null;如果是未初始化变量,则返回 0。
FormatPercent 函数 返回表达式,此表达式已被格式化为尾随有 % 符号的百分比(乘以 100 )。 FormatPercent(expression[,NumDigitsAfterD
FormatNumber 函数 返回表达式,此表达式已被格式化为数值。 FormatNumber( expression [,NumDigitsAfterDecimal [,Inc
我是一名优秀的程序员,十分优秀!