话不多说,上才艺!
为了实现这个功能,我们需要写一个自定义函数,(报表属性=》代码)
因为我的数据里面有的值为“NAN”,所以我需要排除掉这些数据,代码如下
#region "排除值为NAN的元素"
Public Function arrayCheck(array as Object(),keyWord as Object) as Object()
System.Array.sort(array)'将数据集转为数组并排序(升序,降序排序则使用System.Array.Reverse(数组名))
dim temp as Object()
dim i as integer
dim j as integer=0
for i=0 to array.length-1
if(array(i)<>keyWord) then
Redim Preserve temp(0 to j)
temp(j)=array(i)
j=j+1
end if
next i
return temp
end Function
#end region
接下来就可以计算百分位数了,在下面接着写上如下代码
#region "根据传入的数据集合P%计算百分位数,dataList:数据集,percentage:P%"
Public Function getPercentage(dataList as Object(),percentage as double) as string
dim temp() as Object
dim length as integer
temp=arrayCheck(dataList,"NAN")
if(IsNothing(temp)) then
return "NAN"
else
System.Array.sort(temp)
length=temp.length
end if
if(length=1) then'如果只有一条数据,百位位数为该条数据的值
return temp(0)
end if
dim j as integer'百分位数的整数部分
dim g as double'百分位数的小数部分
dim value as double'百分位数
dim pValue as double=(length-1)*percentage+1'计算指数
dim str as string=CStr(pValue)
dim arr as Array=Split(str,".")'根据小数点截取字符串
if(arr.length>1) then'判断是否有小数
j=CInt(arr(0))
g=CDbl(arr(1))
else
j=CInt(arr(0))
g=0
end if
Select Case g'判断小数位数是否为0,并计算百分位数
case 0
value=temp(j)
case else
g=pValue-j
value=g*temp(j)+(1-g)*temp(j-1)
end Select
return CStr(value)'返回
end Function
#end region
接下来就是调用所写的函数,获取需要计算的数据集所用的方法是:LookupSet()
语法:
LookupSet(source_expression, destination_expression, result_expression, dataset)
参数:
source_expression
(Variant) 在当前作用域中计算结果并指定要查找的名称或键的表达式。例如,=Fields!ID.Value。
destination_expression
(Variant) 针对数据集中的每行计算结果并指定要匹配的名称或键的表达式。例如,=Fields!Product.Value。
result_expression
(Variant) 针对数据集中的行计算结果并指定要检索的值的表达式,其中 source_expression = destination_expression。例如,=Fields!ActualBCT.Value。
dataset
指定报表中数据集的名称的常量。例如,“DataSet_Data”。
返回:
返回 VariantArray,如果没有匹配项,则返回 Nothing。
表达式如下:
=Code.getPercentage(LookupSet(Fields!Product.Value,Fields!Product.Value,Fields!ActualBCT.Value,"DataSet_Data"),0.1)
以上就是本章所有内容,希望对您有所帮助!
开心也是一天,不开心也是一天,何必不开心,来,笑^_^