DAX Text - FORMAT function


Advertisements

Description

Converts a value to text according to the specified format.

Syntax

FORMAT (<value>, <format_string>) 

Parameters

Sr.No. Parameter & Description
1

value

A value or expression that evaluates to a single value.

2

format_string

A string representing a formatting style.

  • To format numbers, you can either use predefined numeric formats or create user-defined numeric formats.

Look at the sections given at the end of this chapter - Pre-Defined Numeric Formats and Custom Numeric Formats for the FORMAT function.

  • To format dates and times, you can use predefined date/time formats or create user defined date/time formats.

Refer to the tutorial – DAX in this tutorials library for details on formatting dates and times.

Return Value

A string containing value formatted as defined by format_string.

Remarks

  • If value is BLANK (), FORMAT function returns an empty string.

  • If format_string is BLANK (), the value is formatted with a "General Number" or "General Date" format (according to value data type).

Example

Following table shows the results of the FORMAT function with the first argument value given in the first row and format_string given in the first column. Refer to the sections given below this table to understand the format strings.

"5" "-5" "0.5" "0"
Zero-length string ("") 5 -5 0.5 0
0 5 -5 1 0
0.00 5.00 -5.00 0.50 0.00
#,##0 5 -5 1 0
$#,##0;($#,##0) $5 ($5) $1 $0
$#,##0.00;($#,##0.00) $5.00 ($5.00) $0.50 $0.00
0% 500% -500% 50% 0%
0.00% 500.00% -500.00% 50.00% 0.00%
0.00E+00 5.00E+00 -5.00E+00 5.00E-01 0.00E+00
0.00E-00 5.00E00 -5.00E00 5.00E-1 0.00E00
"$#,##0;;\Z\e\r\o" $5 $-5 $1 Zero

Pre-Defined Numeric Formats for the FORMAT function

The following table identifies the predefined numeric format names that can be used by name for the format style argument of the Format function.

Sr.No. Format String & Description
1

"General Number"

Displays number with no thousand separators.

2

"Currency"

Displays number with thousand separators, if appropriate.

Displays two digits to the right of the decimal separator.

Output is based on system locale settings.

3

"Fixed"

Displays at least one digit to the left and two digits to the right of the decimal separator.

4

"Standard"

Displays number with thousand separators, at least one digit to the left and two digits to the right of the decimal separator.

5

"Percent"

Displays number multiplied by 100 with a percent sign (%) appended immediately to the right.

Always displays two digits to the right of the decimal separator.

6

"Scientific"

Uses standard scientific notation, providing two significant digits.

7

"Yes/No"

Displays No if number is 0. Otherwise, displays Yes.

8

"True/False"

Displays False if number is 0. Otherwise, displays True.

9

"On/Off"

Displays Off if number is 0. Otherwise, displays On.

Custom Numeric Formats for the FORMAT Function

A user defined format expression for numbers can have from one to three sections separated by semicolons.

If the format_string argument of the Format function contains one of the predefined numeric formats, only one section is allowed.

The following table shows how the sections are applied while formatting.

Sr.No. Number of Section & Format Result
1

One section only

The format expression applies to all the values.

2

Two sections

The first section applies to positive values and zeros.

The second applies to negative values.

3

Three sections

The first section applies to positive values.

The second section applies to negative values.

The third section applies to zeros.

If you include semicolons with nothing between them, the missing section is printed using the format of the positive value.

The following table identifies the characters you can use to create user-defined number formats.

Sr.No. Format Specification & Description
1

None

Displays the number with no formatting.

2

0 (zero character)

Digit placeholder. Displays a digit or a zero.

If the expression has a digit in the position where the zero appears in the format string, displays the digit. Otherwise, displays a zero in that position.

If the number has fewer digits than there are zeros (on either side of the decimal) in the format expression, displays leading or trailing zeros.

If the number has more digits to the right of the decimal separator than there are zeros to the right of the decimal separator in the format expression, rounds the number to as many decimal places as there are zeros.

If the number has more digits to the left of the decimal separator than there are zeros to the left of the decimal separator in the format expression, displays the extra digits without modification.

  • If the expression has a digit in the position where the # character appears in the format string, displays the digit.

  • Otherwise, displays nothing in that position.

This symbol works like the 0-digit placeholder, except that leading and trailing zeros are not displayed if the number has fewer digits than there are # characters on either side of the decimal separator in the format expression.

3

. (dot character)

Decimal placeholder. The decimal placeholder determines how many digits are displayed to the left and right of the decimal separator.

  • If the format expression contains only # characters to the left of this symbol, then the numbers smaller than 1 begin with a decimal separator.

  • To display a leading zero displayed with fractional numbers, use zero as the first digit placeholder to the left of the decimal separator.

In some locales, a comma is used as the decimal separator. The actual character used as a decimal placeholder in the formatted output depends on the number format recognized by your system. Thus, you should use the period as the decimal placeholder in your formats even if you are in a locale that uses a comma as a decimal placeholder. The formatted string will appear in the format correct for the locale.

4

%

Percent placeholder. Multiplies the expression by 100. The percent character (%) is inserted in the position where it appears in the format string.

5

, (comma character)

Thousand separator. The thousand separator separates thousands from hundreds within a number that has four or more places to the left of the decimal separator.

Standard use of the thousand separator is specified if the format contains a thousand separator surrounded by digit placeholders (0 or #). A thousand separator immediately to the left of the decimal separator (whether or not a decimal is specified) or as the rightmost character in the string means "scale the number by dividing it by 1,000, rounding as needed."

  • Numbers smaller than 1,000 but greater or equal to 500 are displayed as 1, and numbers smaller than 500 are displayed as 0.

  • Two adjacent thousand separators in this position scale by a factor of 1 million, and an additional factor of 1,000 for each additional separator.

  • Multiple separators in any position other than immediately to the left of the decimal separator or the rightmost position in the string are treated simply as specifying the use of a thousand separator.

In some locales, a period is used as a thousand separator. The actual character used as the thousand separator in the formatted output depends on the Number Format recognized by your system. Thus, you should use the comma as the thousand separator in your formats, even if you are in a locale that uses a period as a thousand separator. The formatted string will appear in the format correct for the locale.

Examples −

  • "#,0." Uses the thousands separator to format the number 100 million as the string "100,000,000".

  • "#0,." Uses scaling by a factor of one thousand to format the number 100 million as the string "100000".

  • "#,0,." Uses the thousands separator and scaling by one thousand to format the number 100 million as the string "100,000".

6

: (colon character)

Time separator. The time separator separates hours, minutes, and seconds when time values are formatted.

In some locales, other characters may be used to represent the time separator. The actual character used as the time separator in formatted output is determined by your system settings.

7

/ (forward slash character)

Date separator. The date separator separates the day, month, and year when the date values are formatted.

In some locales, other characters may be used to represent the date separator. The actual character used as the date separator in formatted output is determined by your system settings.

8

E- , E+ , e- , e+

Scientific format.

If the format expression contains at least one-digit placeholder (0 or #) to the left of E-, E+, e-, or e+, the number is displayed in scientific format and E or e is inserted between the number and its exponent.

  • The number of digit placeholders to the left determines the number of digits in the exponent.
  • Use E- or e- to place a minus sign next to negative exponents.
  • Use E+ or e+ to place a minus sign next to negative exponents and a plus sign next to positive exponents.
  • You must also include digit placeholders to the right of this symbol to get correct formatting.
9

-+$()

Literal characters. These characters are displayed exactly as typed in the format string.

To display a character other than one of those listed, precede it with a backslash (\) or enclose it in double quotation marks (" ").

10

\ (backward slash character)

Displays the next character in the format string. To display a character that has special meaning as a literal character, precede it with a backslash (\).

  • The backslash itself is not displayed.
  • Using a backslash is the same as enclosing the next character in double quotation marks.
  • To display a backslash, use two backslashes.

However, some characters cannot be displayed as literal characters. For example,

  • The date-formatting and time-formatting characters (a, c, d, h, m, n, p, q, s, t, w, y, /, and :)
  • The numeric-formatting characters (#, 0, %, E, e, comma, and period)
  • The string-formatting characters (@, &, <, >, and !)
11

"ABC"

Displays the string inside the double quotation marks (" "). To include a string in the style argument from within code, you must use Chr(34) to enclose the text (34 is the character code for a quotation mark (")).

dax_functions_text.htm

Useful Video Courses


Video

Mastering DAX and Data Models in Power BI Desktop

53 Lectures 5.5 hours

Abhay Gadiya

Video

Mastering DAX Studio

Featured

24 Lectures 2 hours

Randy Minder

Video

DAX / Power BI - Customer and Sales Analysis Deep Dive

26 Lectures 4.5 hours

Randy Minder

Advertisements