The M Code Behind the Power Query M function Trigonometry

What is Trigonometry?

Trigonometry is a branch of mathematics that deals with the relationships between the sides and angles of triangles. It is used to solve problems in fields such as physics, engineering, and navigation. Trigonometric functions, such as sine, cosine, and tangent, are used to calculate these relationships.

The M Code Behind the Trigonometry Function

In Power Query, the Trigonometry function is used to calculate trigonometric values of angles in radians. The function takes two arguments: the angle and the type of trigonometric function to be calculated. The angle must be in radians, and the type of function can be one of the following: Sine, Cosine, Tangent, Cotangent, Secant, or Cosecant.

The M code behind the Trigonometry function is based on the mathematical formulas for calculating trigonometric values. For example, the formula for calculating the sine of an angle is sin(x) = opposite/hypotenuse, where x is the angle in radians, and opposite and hypotenuse are the lengths of the two sides of a right triangle.

In M code, the formula for calculating the sine of an angle is:


sine = (x) => Number.Round(Number.Sin(x), 14),

result = sine(angle)



In this code, the sine function takes the angle in radians as its input and calculates the sine of that angle using the Number.Sin function. The result is then rounded to 14 decimal places using the Number.Round function.

The M code for the other trigonometric functions is similar, with slight variations based on the mathematical formulas for each function.

Using the Trigonometry Function in Power Query

To use the Trigonometry function in Power Query, you must first create a query that contains a column with the angle in radians. You can then use the Trigonometry function to calculate the trigonometric value of that angle.

For example, suppose you have a table with a column called “Angle” that contains angles in radians. To calculate the sine of each angle, you can create a new column using the following M code:


Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQwVNJRMgY6MIo2tDQ1MqbWJyQZgPx7K8OLUvPyc1MqFV4AUhJLSlJQrAwDQ0NDMy0xOTLUNTAz1g1MjGwMDCxMzywMjIz8jS11zU0tJQwNzQwNzQwNzQwNjXWtDS2sDCwNzQwNzQwMjSwNbEwMDs3MrW0tjS2tDCwNzQwNzQwMjSwNbEwMDS2tDQ1MqbCwNzQwNzQwMjSwNbEwMDQ1MrW0tDS2sDCwNzQwNzQwMjSwNbEwMDQ1MrW0sDCwNzQwNzQwMjSwNbEwMDCwMDCwMDCwMDCwMDCwMDCwMDCwMDCwMDCwMDCwMDCwMDCwMDCwMDCwMDCwMDCwMDCwMDCwMDCwMDA==", BinaryEncoding.Base64)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Angle = _t]),

#"Added Custom" = Table.AddColumn(Source, "Sine", each Trigonometry.Sin([Angle])),

#"Added Custom1" = Table.AddColumn(#"Added Custom", "Cosine", each Trigonometry.Cos([Angle])),

#"Added Custom2" = Table.AddColumn(#"Added Custom1", "Tangent", each Trigonometry.Tan([Angle])),

#"Added Custom3" = Table.AddColumn(#"Added Custom2", "Cotangent", each Trigonometry.Cot([Angle])),

#"Added Custom4" = Table.AddColumn(#"Added Custom3", "Secant", each Trigonometry.Sec([Angle])),

#"Added Custom5" = Table.AddColumn(#"Added Custom4", "Cosecant", each Trigonometry.Csc([Angle]))


#"Added Custom5"

This code creates a new column for each trigonometric function, using the Trigonometry function to calculate the value of each function for each angle in the “Angle” column.

The Trigonometry function in Power Query allows users to perform complex trigonometric calculations within their data transformations. The M code behind the function is based on the mathematical formulas for calculating trigonometric values and can be customized to suit specific needs. By understanding the M code behind the Trigonometry function, users can leverage its power to perform advanced calculations in their data transformations.

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

Upcoming Courses

10-12 Jul 23 (London or Online)
25-27 Sep 23 (London or Online)
20-22 Nov 23 (London or Online)

Contact Us


    Your Name (required)


    Email (required)


    Training Course(s)

    Your Message

    Upload Example Document(s) (Zip multiple files)