如何在 Power BI DAX 上控制小数精度

How to control decimal precision on Power BI's DAX

假设我有一个 table "Table1" 字段 Field1,"Table2" 字段 2。我在 "Table1"、"CField" 中有另一个计算列,其公式为:

CField = ROUND(Table1[Field1] * RELATED(Table2[Field2]); 2)

该公式总体上运行良好,只是有时它没有给出正确的值。例如:

用计算器计算得出 3.26495。四舍五入到 2 位小数应该得到 3.26,但在这种情况下,PowerBI 给出 ​​3.27.

我最好的假设是 Field1 * Field2 的结果计算为 CURRENCY,因此在四舍五入到 3.27 之前四舍五入到 3.265。

我的问题是:有没有办法强制 Power BI 保持我需要的精度并获得我想要的 3.26 结果?

编辑:3.27 不仅仅是一个显示问题。如果我在显示值上加上小数,它会给出 3.27000.

检查您的列数据类型。在 Power BI 中,decimal 实际上是一个浮点数,因此不应与 SQL Server.

等其他数据库产品中的 decimal 数据类型混淆。

我可以通过某种特定的数据类型设置重现这种情况。您可以检查一下是否与您的情况相同。

如您所说,这与Format(即显示问题)无关。问题出在Data type。当所有三列都设置为 Decimal Number 时,Power BI returns 3.26.

但是,当Field1更改为Fixed decimal number时,Power BI returns 3.27.

根据Power BI documentation

Fixed Decimal Number – Has a fixed location for the decimal separator. The decimal separator always has four digits to its right and allows for 19 digits of significance. The largest value it can represent is 922,337,203,685,477.5807 (positive or negative). The Fixed Decimal Number type is useful in cases where rounding might introduce errors.

因此,对此的解释是结果3.26495四舍五入为3.2650(四位小数),然后四舍五入为3.27(DAX函数四舍五入)。

结论: 将所有 Data type 更改为 Decimal Number 应该可以解决舍入错误问题。


尽管我仍然不清楚为什么当 Field1 的数据类型为 Fixed decimal number 而不是 CField 时会发生这种情况。欢迎补充意见。