DAX 测试整数部分

huangapple go评论79阅读模式
英文:

DAX to Test for Whole Number

问题

我有一个如下所示的Actuals列:

ID | Airport
------------
A  | 98.4
B  | 98.0
C  | 95.3

我试图将上面的数字格式化为前端报告中的百分比。我已经在一个switch语句中编写了这个逻辑 - 为了方便,我将逻辑写成一个IF布尔值。

example_measure = 
VAR Nums = SELECTEDVALUES(Table[Actuals])
VAR FormatNums = IF(DIVIDE(ROUND(nums,1), nums) = 1,
                              format(nums,"0%"),format(nums,"0.0%"))
-
RETURN 
FormatNums

无论我做什么,这总是返回一个浮点值为1f的数字。

所以在我的原始数据中的98.0,我期望格式返回"98%"而不是"98.0%"

这些度量用于单独的卡片,并且经过筛选,所以它们只能显示一个值或空白,这意味着它们只会单独显示一个值。

我曾尝试使用if(nums = int(nums)),这应该对98.0求值为真,但我总是得到假。

英文:

I have a Actuals column like so:

ID | Airport
------------
A  | 98.4
B  | 98.0
C  | 95.3

I'm attempting to format the numbers above into percentages for a front-end report. I have this written in a switch statement - for ease I'll just write the logic as an IF boolean.

example_measure = 
VAR Nums = SELECTEDVALUES(Table[Actuals])
VAR FormatNums = IF(DIVIDE(ROUND(nums,1), nums) = 1,
                              format(nums,"0%"),format(nums,"0.0%")
-
RETURN 
FormatNums 

no matter what I do this always returns a number with a floating point value of 1f

so in my raw data of 98.0 I'm expecting the format to return "98%" rather than "98.0%"

the measures are used on individual cards, and are filtered so they can only ever show one value or blank, meaning they will only ever display one value on their own.

I've toyed with using if(nums = int(nums) which should evaluate to true for 98.0 but it I always get false.

答案1

得分: 2

要将一列/度量转换为百分比,您只需将该列除以100,然后将其格式化为百分比。请按照以下步骤操作:

  1. 创建一个新的列/度量:Perc_value = Table[Actuals]/100
  2. 然后进入建模选项卡,选择创建的列/度量,并将其格式化为百分比,将小数位数限制为0

这应该会得到您想要的视图。希望这有所帮助。

编辑:

您可以使用以下公式来实现所需的结果:

Column4 = IF('Table'[Column2]-ROUND('Table'[Column2],0)=0,FORMAT('Table'[Column2]/100,"0%"),FORMAT('Table'[Column2]/100,"0.0%"))

将Column2替换为您的字段,然后应该可以正常使用。

英文:

To convert a column/measure into percentage, you can simply divide the column by 100 and then format it as a percentage. Use the following steps:

  1. Create a new column/measure: Perc_value = Table[Actuals]/100
  2. Then go into the modelling tab, select the created column/measure and format it as a % and limit the number of decimal places to 0

This should give the view you are looking for. Hope this helps.

Edit:

You can use the below formula to achieve the desired result:

Column4 = IF('Table'[Column2]-ROUND('Table'[Column2],0)=0,FORMAT('Table'[Column2]/100,"0%"),FORMAT('Table'[Column2]/100,"0.0%"))

Replace Column2 withyour field and you should be good to go.

答案2

得分: 2

有一个更简单的方法 - 只需使用内置的格式化代码:

Formatted Actuals = 
  VAR x = SELECTEDVALUE(Data[Actuals])
  RETURN
  FORMAT(x, "General Number") & "%"

结果:

DAX 测试整数部分

内置样式 "General Number" 正确处理您的情况,您只需将百分比符号添加到格式化的字符串中。不需要测试是否为整数。

英文:

There is a simpler way - just use built-in formatting codes:

Formatted Actuals = 
  VAR x = SELECTEDVALUE(Data[Actuals])
  RETURN
  FORMAT(x, "General Number") & "%"

Result:

DAX 测试整数部分

Built-in style "General Number" handles your situation correctly, you just need to add % symbol to the formatted string. No need to test for whole numbers.

huangapple
  • 本文由 发表于 2020年1月7日 02:28:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/59617128.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定