从数据到信息
从信息到洞察

转换函数

DAX 根据运算符的需要执行数据类型的自动转换。即使这种行为是自动发生的,DAX 仍然提供了一组函数用来执行显示的类型转换。

CURRENCY

CURRENCY ( <Value> )

将表达式结果转换为货币类型

  1. CURRENCY 函数四舍五入第 5 位有效小数的值,返回第 4 位小数;如果第 5 位有效小数等于或大于 5,则进行舍入。例如,如果 value 是 3.6666666666666,那么转换成货币返回$3.6667;但是,如果值是 3.0123456789,那么转换成货币将返回 3.0123.
  2. 如果表达式的数据类型是 True/False,那么 CURRENCY()将为真值返回$1.0000,为假返回$0.0000。
  3. 如果表达式的数据类型是文本,则 CURRENCY()将尝试将文本转换为数字;如果转换成功,数字将被转换为货币,否则将返回一个错误。
  4. 如果表达式的数据类型是 DateTime,那么 CURRENCY()将把 DateTime 值转换为一个数字,然后将这个数字转换为货币。DateTime 值包含一个整数部分,表示给定日期到 1900-03-01 之间的天数,以及一个分数,表示一天的分数(其中 12 小时或中午是 0.5 天)。如果表达式的值不是正确的 DateTime 值,则返回一个错误。

INT

INT ( <Number> )

将表达式转换为整数(向下舍入)

INT ( - 2.9 ) = -3
INT ( - 2.1 ) = -3
INT ( - 0.9 ) = -1
INT ( - 0.1 ) = -1
INT ( 0.1 ) = 0
INT ( 0.9 ) = 0
INT ( 2.1 ) = 2
INT ( 2.9 ) = 2

TRUNC

TRUNC ( <Number>, [<NumberOfDigits>] )

通过删除数字的小数或小数部分将数字截断为整数

在截断负数的时候,TRUNCINT 函数的行为有所不同。

  • 如果 NumberOfDigits 大于 0,则 number 被截断为指定的小数位数。
  • 如果 NumberOfDigits 为 0,则该数字将被截断为整数部分。
  • 如果 NumberOfDigits 小于 0,则将该数字截断到小数点的左边。

ROUND

ROUND ( <Number>, <NumberOfDigits> )

将数字四舍五入为指定的数字

  • 如果 NumberOfDigits 大于 0,将数字四舍五入到指定的小数位数。
  • 如果 NumberOfDigits 是 0,将数字四舍五入到最近的整数。
  • 如果 NumberOfDigits 小于 0,将数字四舍五入到小数点的左边。

要更精确的控制舍入行为,可以参考向下舍入ROUNDDOWN、向上舍入ROUNDUP

MROUND

MROUND(<number>, <multiple>)

返回一个四舍五入到所需倍数 <multiple>的数字。若除以指定倍数后的余数大于或等于倍数的一半,则四舍五入。

MROUND(1.3,0.2)  => 1.4
MROUND(-10,-3) => -9
MROUND(5,-2) => error
当为小数值舍入时,最终的结果的位数是不确定的。这是 Excel 中一个已知 BUG,DAX 继承了此 BUG。例如 MROUND(6.05,0.1)返回 6.0,而 MROUND(7.05,0.1)返回 7.1。

日期和时间转换

DATE 和 TIME 将日期和时间部分作为参数并返回正确的日期时间格式。

DATE ( <Year>, <Month>, <Day> )

以 datetime 格式返回指定的日期

TIME ( <Hour>, <Minute>, <Second> )

将数字形式的小时、分钟和秒转换为 datetime 格式的时间

VALUE

VALUE ( <Text> )

将字符串转换为数字格式

作为文本参数传递的 text 可以是 DAX 识别的任何常量、数字、日期或时间格式。如果不是这些格式,则返回错误。

VALUE 函数并不常用,因为 DAX 根据需要隐式地将文本转换为数字。

CONVERT

CONVERT ( <Expression>, <DataType> )

将表达式转换为指定的数据类型,可转换为整数,双精度,字符串,布尔值,货币,日期时间类型

FORMAT

FORMAT ( <Value>, <Format> )

将值转换为指定数字格式的文本

以数值作为第一个参数,以字符串格式作为第二个参数,FORMAT将数值转换为文本字符串。通常与 DateTime 格式一起使用。

例如,下面的表达式返回“2015 年 1 月 12 日”

FORMAT ( DATE ( 2015, 01, 12 ), "yyyy mmm dd" )

相反的操作(将字符串转换为 DateTime 值)是通过DATEVALUE函数执行的。

DAX 格式函数的参数所支持的格式字符串是基于 Visual Basic (OLE 自动化)使用的格式字符串,而不是. net 框架使用的格式字符串。因此,如果参数不匹配任何定义的格式字符串,可能会得到意外的结果或错误。例如,不支持“p”作为“Percent”的缩写。作为 FORMAT 函数的参数提供的字符串(不包括在预定义格式字符串列表中)将作为自定义格式字符串的一部分或字符串文本处理。

预定义的数字格式

参数类型 参数说明(注意:返回的都是文本)
"General Number" 显示没有千分符的数字
"Currency" 显示带千分符的数字,对于符合要求的数字,在十进制分隔符的右边显示两个数字。输出的内容基于系统区域设置。
"Fixed" 在十进制分隔符的左边显示至少一个数字,右边显示至少两个数字。
"Standard" 显示带有千分符的数字,小数点分隔符的左边至少有一个数字,右边至少有两个数字。
"Percent" 将显示的数字乘以 100,并在右侧添加百分号(%);始终在十进制分隔符的右边显示两个数字。
"Scientific" 使用标准科学符号,提供两个有效数字。
"Yes/No" 如果数字为 0,则显示 No;否则显示 Yes。
"True/False" 如果数字为 0,则显示 False;否则显示 True。
"On/Off" 如果数字为 0,则显示 Off,否则显示 On。
FORMAT( 12345.67, "General Number") = 12345.67
FORMAT( 12345.67, "Currency") = ¥12345.67
FORMAT( 12345.67, "Fixed") = 12345.67
FORMAT( 12345.67, "Standard") = 12,345.67
FORMAT( 12345.67, "Percent") = 1,234,567.00 %
FORMAT( 12345.67, "Scientific") = 1.23E+04

可自定义的数字格式

本文隐藏内容查看价格为2G币,请先
单独购买的内容长期有效,不受时间限制(购买前先刷新当前页面)。加入VIP会员可享受全站权益,性价比更高。

预定义的日期和时间格式

本文隐藏内容查看价格为2G币,请先
单独购买的内容长期有效,不受时间限制(购买前先刷新当前页面)。加入VIP会员可享受全站权益,性价比更高。

自定义的日期和时间格式

参数类型 参数说明(注意:返回的都是文本)
(:) Time separator. In some locales, other characters may be used to represent the time separator. The time separator separates hours, minutes, and seconds when time values are formatted. The actual character that is used as the time separator in formatted output is determined by your application’s current culture value.
(/) Date separator. In some locales, other characters may be used to represent the date separator. The date separator separates the day, month, and year when date values are formatted. The actual character that is used as the date separator in formatted output is determined by your application’s current culture.
(%) Used to indicate that the following character should be read as a single-letter format without regard to any trailing letters. Also used to indicate that a single-letter format is read as a user-defined format. See what follows for additional details.
d Displays the day as a number without a leading zero (for example, 1). Use %d if this is the only character in your user-defined numeric format.
dd Displays the day as a number with a leading zero (for example, 01).
ddd Displays the day as an abbreviation (for example, Sun).
dddd Displays the day as a full name (for example, Sunday).
M Displays the month as a number without a leading zero (for example, January is represented as 1). Use %M if this is the only character in your user-defined numeric format.
MM Displays the month as a number with a leading zero (for example, 01/12/01).
MMM Displays the month as an abbreviation (for example, Jan).
MMMM Displays the month as a full month name (for example, January).
gg Displays the period/era string (for example, A.D.).
h Displays the hour as a number without leading zeros using the 12-hour clock (for example, 1:15:15 PM). Use %h if this is the only character in your user-defined numeric format.
hh Displays the hour as a number with leading zeros using the 12-hour clock (for example, 01:15:15 PM).
H Displays the hour as a number without leading zeros using the 24-hour clock (for example, 1:15:15). Use %H if this is the only character in your user-defined numeric format.
HH Displays the hour as a number with leading zeros using the 24-hour clock (for example, 01:15:15).
m Displays the minute as a number without leading zeros (for example, 12:1:15). Use %m if this is the only character in your user-defined numeric format.
mm Displays the minute as a number with leading zeros (for example, 12:01:15).
s Displays the second as a number without leading zeros (for example, 12:15:5). Use %s if this is the only character in your user-defined numeric format.
ss Displays the second as a number with leading zeros (for example, 12:15:05).
AM/PM Use the 12-hour clock and display an uppercase AM with any hour before noon; display an uppercase PM with any hour between noon and 11:59 P.M.
am/pm Use the 12-hour clock and display a lowercase AM with any hour before noon; display a lowercase PM with any hour between noon and 11:59 P.M.
A/P Use the 12-hour clock and display an uppercase A with any hour before noon; display an uppercase P with any hour between noon and 11:59 P.M.
a/p Use the 12-hour clock and display a lowercase A with any hour before noon; display a lowercase P with any hour between noon and 11:59 P.M.
AMPM Use the 12-hour clock and display the AM string literal as defined by your system with any hour before noon; display the PM string literal as defined by your system with any hour between noon and 11:59 P.M. AMPM can be either uppercase or lowercase, but the case of the string displayed matches the string as defined by your system settings. The default format is AM/PM.
y Displays the year number (0-9) without leading zeros. Use %y if this is the only character in your user-defined numeric format.
yy Displays the year in two-digit numeric format with a leading zero, if applicable.
yyy Displays the year in four-digit numeric format.
yyyy Displays the year in four-digit numeric format.
z Displays the timezone offset without a leading zero (for example, -8). Use %z if this is the only character in your user-defined numeric format.
zz Displays the timezone offset with a leading zero (for example, -08)
zzz Displays the full timezone offset (for example, -08:00)

格式化参数是区分大小写的。使用不同的大小写可以获得不同的格式。例如,当用字符串“D”格式化日期值时,你将得到长格式的日期(根据你当前的语言环境)。但是,如果将大小写改为“d”,则会得到短格式的日期。此外,如果预期的格式与任何已定义格式字符串的情况不匹配,可能会出现意外结果或错误。

 

2
说点什么

1000
 
鼓掌微笑开心憧憬爱你色并不觉得吃瓜doge二哈喵喵思考笑哭捂脸悲伤大哭抓狂汗偷笑打脸捂眼黑线问号晕拜拜闭嘴衰咒骂ok作揖
1 评论数
1 被回复的评论
1 订阅评论的人数
 
查看最近回复
查看最热评论
  订阅本文评论  
最新 最旧 得票最多
提醒
成员
bbzhdlp

高老师,类似这种毛利率的怎么设置格式哇?我如果 用Format设置的话,没有办法计算和作趋势分析。

微信图片_20200607082204.png
DAX 圣经

导读

初识 DAX

DAX 基础知识

DAX 原理

DAX 高级原理

基础函数类型

迭代函数

CALCULATE 函数

CALCULATE 调节器

基础表函数

条件判断函数

查找匹配函数

时间智能函数

统计类函数

投影函数

分组/连接函数

集合函数

其他函数