0

我有一份按年龄和性别划分的人口统计报告。当我遇到没有女性员工的部门时,我的一个字段返回#Error。我已经尝试了我能想到的一切来避免这种情况,但还没有找到解决方案。

伪逻辑是,如果女性员工的数量 = 0,则返回“N/A”,否则给我该部门女性员工的平均年龄。我也尝试过返回零或空格。当计数为零时,所有努力都会返回#Error。

我最近的尝试如下:

=iif(sum(iif(Fields!CustGender.Value = "F",1D,0D),"ReportDataset") = 0D,"N/A",Int(Avg(IIF(Fields!CustGender.Value = "F", CDec(Fields!Age.Value),Nothing),"ReportDataset")))

=iif(sum(iif(Fields!CustGender.Value = "F",1,0),"ReportDataset") = 0,"N/A",Int(Avg(IIF(Fields!CustGender.Value = "F", CDec(Fields!Age.Value),Nothing),"ReportDataset")))

=iif(sum(iif(Fields!CustGender.Value = "F",1D,0D),"ReportDataset") = 0D," ",Int(Avg(IIF(Fields!CustGender.Value = "F", CDec(Fields!Age.Value),Nothing),"ReportDataset")))

=iif(sum(iif(Fields!CustGender.Value = "F",1,0),"ReportDataset") = 0," ",Int(Avg(IIF(Fields!CustGender.Value = "F", CDec(Fields!Age.Value),Nothing),"ReportDataset")))

新的消息:

将“Nothing”更改为 0D,错误消失,但计算不正确,因为我在我的 Avg 计算中添加了一堆零值,因此 37 岁的平均年龄变为 10 或类似的东西。

=iif(sum(iif(Fields!CustGender.Value = "F",1D,0D),"ReportDataset") = 0D,"N/A",Int(Avg(IIF(Fields!CustGender.Value = "F", CDec(Fields!Age.Value),Nothing),"ReportDataset")))  -- Fails

=iif(sum(iif(Fields!CustGender.Value = "F",1D,0D),"ReportDataset") = 0D,"N/A",Int(Avg(IIF(Fields!CustGender.Value = "F", CDec(Fields!Age.Value),0D),"ReportDataset")))  -- Works

伊恩的数据:

这是部门 05 of 26。第五个字段是性别,最后一个字段是我想要平均的年龄。

1142    B   Bellingham  WA  M   05 - Business Services  1948-03-06 00:00:00.000 65
2620    A   Ferndale    WA  F   05 - Business Services  1955-02-28 00:00:00.000 58
2626    A   Bellingham  WA  M   05 - Business Services  1948-04-09 00:00:00.000 65
3164    A   Bellingham  WA  M   05 - Business Services  1955-01-07 00:00:00.000 58
3376    A   Bellingham  WA  F   05 - Business Services  1960-04-02 00:00:00.000 53
3867    A   Maple Falls WA  F   05 - Business Services  1958-06-11 00:00:00.000 55
4294    A   Blaine      WA  F   05 - Business Services  1981-08-09 00:00:00.000 32
4580    A   Bellingham  WA  M   05 - Business Services  1956-02-04 00:00:00.000 57
4702    A   Bellingham  WA  M   05 - Business Services  1967-12-30 00:00:00.000 45
4709    A   Lynden      WA  M   05 - Business Services  1961-04-27 00:00:00.000 52
4764    A   Blaine      WA  F   05 - Business Services  1957-04-28 00:00:00.000 56
4892    A   Ferndale    WA  F   05 - Business Services  1976-10-19 00:00:00.000 36
4971    A   Bellingham  WA  F   05 - Business Services  1983-10-16 00:00:00.000 29
4986    C   Bellingham  WA  M   05 - Business Services  1974-03-31 00:00:00.000 39
5085    A   Bellingham  WA  F   05 - Business Services  1994-10-18 00:00:00.000 18
5094    A   Bellingham  WA  F   05 - Business Services  1986-04-22 00:00:00.000 27
4

1 回答 1

1

解决了!!

在询问“这是一位女性”时,我无法将零值作为“错误”评估的返回值,因为随后我的样本量随着十几名男性员工的增加而增加,然后 Avg 函数被抛弃了,因为我有所有这些年龄为 0 岁的男性。因此,我的女员工显示为 8 岁、11 岁、14 岁。关键是在我的 iif 的“真实”部分中,我正在转换为 Int,这意味着传回“Nothing”是一种无效的做法。通过删除我的 Int,我可以将“Nothing”放回原处,从而避免臃肿的样本量,并从 Avg 返回一个有效值。

=iif(sum(iif(Fields!CustGender.Value = "F",1D,0D),"ReportDataset") = 0D,"N/A",Avg(IIF(Fields!CustGender.Value = "F", CDec(Fields!Age.Value),Nothing),"ReportDataset"))
于 2013-09-19T20:40:16.130 回答