Number.Log

D

T

The M Code Behind the Power Query M function Number.Log

What is the Number.Log Function?

The Number.Log function in Power Query is used to calculate the logarithm of a number to a specified base. The syntax of the function is as follows:


Number.Log(number as nullable number, base as nullable number) as nullable number


The function takes two arguments:

- number: the number for which you want to calculate the logarithm.

- base: the base of the logarithm. If the base is not specified, it defaults to the natural logarithm (e).

The function returns the logarithm of the number to the specified base.

The M Code Behind Number.Log

The M code behind the Number.Log function is relatively simple. Here is the M code for the function:


let

NumberLog = (number as nullable number, base as nullable number) as nullable number =>

if number = null or base = null then null

else if number <= 0 or base <= 0 or base = 1 then error "Invalid input"

else Number.Log(number, base)

in

NumberLog


Let's break down the code and see what each part does.

The let Expression

The let expression is used to define variables and functions that can be used later in the code. In this case, we define a variable called NumberLog and assign it the function that we want to create.

The Function Definition

The function definition starts with the name of the function (NumberLog) followed by the input parameters (number and base), and the return type (nullable number). The function returns null if either of the input parameters is null.

Handling Invalid Input

Next, we handle the case where the input parameters are invalid. If either the number or the base is less than or equal to 0 or the base is equal to 1, we return an error message stating that the input is invalid.

Calculating the Logarithm

Finally, if the input parameters are valid, we calculate the logarithm of the number to the specified base using the built-in Number.Log function in Power Query.

The Number.Log function in Power Query is a powerful tool that allows users to calculate the logarithm of a number to a specified base. The M code behind the function is relatively simple and easy to understand. By knowing the code behind the function, users can gain a deeper understanding of how the function works and how they can use it to manipulate their data.

Power Query and M Training Courses by G Com Solutions (0800 998 9248)

Upcoming Courses

Contact Us

    Subject

    Your Name (required)

    Company/Organisation

    Email (required)

    Telephone

    Training Course(s)

    Your Message

    Upload Example Document(s) (Zip multiple files)