Number.Power

D

T

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

Understanding the Number.Power Function

The Number.Power function is used to raise a number to a specified power. The syntax for the function is as follows:


Number.Power(number, power)


The number argument is the base number that will be raised to the power. The power argument is the exponent to which the base number will be raised.

For example, if we want to raise the number 2 to the power of 3, we would use the following formula:


Number.Power(2, 3)


This would return the value 8, which is 2 raised to the power of 3.

The M Code Behind the Number.Power Function

Under the hood, the Number.Power function is implemented in M code. M is the language used by Power Query to perform data transformations and manipulations.

The M code for the Number.Power function is as follows:


let

Source = (number as number, power as number) =>

let

result = Number.Power(number, power)

in

result

in

Source


This code defines a function called Source which takes in two arguments, number and power. The function then calls the built-in Number.Power function and returns the result.

Examples of Using the Number.Power Function

The Number.Power function can be used in a variety of scenarios. Here are a few examples:

Example 1: Calculating Compound Interest

Suppose we want to calculate the future value of an investment based on an initial investment amount, an annual interest rate, and the number of years the investment will be held. We can use the Number.Power function to calculate the compound interest.

The formula for calculating compound interest is:


FV = PV (1 + r/n)^(nt)


Where:

- FV = future value

- PV = present value

- r = annual interest rate

- n = number of times per year the interest is compounded

- t = number of years

To use the Number.Power function in this scenario, we would use the following M code:


let

initialInvestment = 1000,

annualInterestRate = 0.05,

compoundingPeriodsPerYear = 12,

yearsInvested = 10,

futureValue = initialInvestment Number.Power((1 + annualInterestRate/compoundingPeriodsPerYear), (compoundingPeriodsPerYear yearsInvested))

in

futureValue


This code sets the initial investment amount, annual interest rate, compounding periods per year, and years invested as variables. It then calculates the future value using the Number.Power function to raise the expression `(1 + annualInterestRate/compoundingPeriodsPerYear)` to the power of `(compoundingPeriodsPerYear yearsInvested)`.

Example 2: Converting Units of Measurement

Suppose we have a dataset containing measurements in feet, but we need to convert them to meters. We can use the Number.Power function to perform the conversion.

The conversion factor from feet to meters is 0.3048. To convert a measurement in feet to meters, we can multiply the measurement by this conversion factor raised to the power of 1.

To use the Number.Power function in this scenario, we would use the following M code:


let

Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText(“i45WMjQyVtJRMlSK1YlWMgQF9gJilgGQaYugYh2ZQMtJLhKGgTYmJmakZOVsWJuYlFhcWwJYklmXopqck6hcXqCQZG1hRZpQJxg5WnpYILZaRqRSUF1UWprkq2S2U6cgXZJkVybCl2esJWKjKqSDI0jL8vMTy1KzS00sC8qzkzMS1JLzSvJLzSvJLzSvskhLzSvJLbS0tIyNyQxNApB2tE1NzU2NzExMDI0NzA3MjMwMzG0MzW0MzRiyMfKDAwMzE=”, BinaryEncoding.Base64)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#”Measurement (ft)” = _t]),

#”Changed Type” = Table.TransformColumnTypes(Source,{{“Measurement (ft)”, type number}}),

#”Added Custom” = Table.AddColumn(#”Changed Type”, “Measurement (m)”, each [#”Measurement (ft)”] Number.Power(0.3048, 1))

in

#”Added Custom”


This code reads in a table of measurements in feet, converts the measurement column to a number data type, and then adds a new column containing the measurements in meters. The new column is calculated by multiplying the measurement in feet by the conversion factor raised to the power of 1.

The Number.Power function is a powerful tool that can be used to perform calculations and conversions in Power Query. Understanding the M code behind the function can help users to better understand how it works and to use it more effectively in their data transformations.

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)