Byte.From

Updated on

Byte.From is a Power Query M function that returns an 8-bit integer number value from a given value. It handles null, numbers within the 8-bit integer range, and other types by converting them to numbers using Number.FromText. An optional culture parameter can be provided.

Compatible with: Power BI Service Power BI Desktop Excel Microsoft 365

Syntax

Byte.From(
   value as any,
   optional culture as nullable text,
   optional roundingMode as nullable number,
) as nullable number
ArgumentAttributeDescription
value
cultureoptionalThe culture argument enables the specification of a Culture code (e.g., “nl-NL” or “en-US”) to align transformations with local formatting conventions. If this argument is omitted, functions default to Culture.Current, which reflects the system’s regional settings.
roundingModeoptionalThe RoundingMode.Type argument controls the rounding method applied. When omitted, the function uses RoundingMode.ToEven by default, which rounds to the nearest even number in tie situations. Other options include:
RoundingMode.Up rounds up in such cases.
RoundingMode.Down rounds down.
RoundingMode.TowardZero rounds toward zero.
RoundingMode.AwayFromZero rounds away from zero.

Description

The Byte.From function converts a given value into an 8-bit integer. If the value is null, it returns null. For numbers within the 8-bit range, it returns the number directly. If the number has a fractional part, it rounds it using the specified rounding mode, defaulting to RoundingMode.Even.

For other types of input, the function first converts them to numbers using Number.FromText. You can also provide an optional culture parameter, like “en-US” or “nl-NL”, for locale-specific formatting.

Examples

Converting a Text Number

Convert the text “4” to an 8-bit integer. Since “4” is a whole number within the 8-bit range, it will be directly converted without any rounding.

// Output: 5
Byte.From( "5" )

Converting a Fractional Text Number with Default Rounding

Now, suppose you have “6.5” as text and want to convert it. By default, the function uses a rounding method called RoundingMode.Even, which rounds to the nearest even number. So, in this case, 6.5 gets rounded to 6.

// Output: 6
Byte.From( "6,5" )

Converting a Fractional Text Number with Specific Rounding

What if you need to convert “7.5” and want it rounded up instead? You can use the RoundingMode.AwayFromZero. This mode always rounds away from zero, so 7.5 becomes 8.

// Output: 8
Byte.From( "7,5", null, RoundingMode.AwayFromZero )

These examples show how you can easily convert different types of text numbers into 8-bit integers using the Byte.From function.

Other functions related to Byte.From are:

Contribute » | Contributors: Rick de Groot
Microsoft documentation: https://learn.microsoft.com/en-us/powerquery-m/byte-from

2023-2024 © BI Gorilla. All rights are reserved. Information from Microsoft docs is property of Microsoft Corp. | Privacy Policy