Power Query Number.Cos Function: Guide and Examples
In this article
The Number.Cos function in Power Query is used to calculate the cosine of an angle provided in radians. To use it, you simply wrap the numeric value (the angle) inside the function, such as Number.Cos(0), which returns 1. This function is essential for data analysts performing trigonometric calculations, signal processing, or coordinate geometry within Power BI or Excel.
What You'll Need
- Microsoft Excel or Power BI Desktop installed on your machine.
- A dataset containing numeric values that represent angles.
- Access to the Power Query Editor (Get Data > Launch Power Query Editor).
- Basic knowledge of the difference between degrees and radians, as Power Query only accepts radians.
Step-by-Step
- Open the Power Query Editor: In Excel, go to the Data tab and select Get Data > Launch Power Query Editor. In Power BI, click on Transform Data from the Home ribbon.
- Prepare Your Angle Column: Ensure the column containing your angles is set to a numeric data type (Decimal Number). Right-click the column header, select Change Type, and choose Decimal Number.
- Convert Degrees to Radians: Because
Number.Cosrequires radians, you must convert your data if it is currently in degrees. Go to the Add Column tab and select Custom Column. Name the column "Radians" and use the formula:[YourDegreeColumn] * (Number.PI / 180). Click OK. - Apply the Number.Cos Function: With your radians column ready, go back to the Add Column tab and select Custom Column again. Name this column "Cosine Result". In the formula box, enter:
Number.Cos([Radians]). - Verify the Calculation: Check a few rows to ensure accuracy. For example, if your original angle was 0 degrees (0 radians), the result should be 1. If it was 90 degrees (PI/2 radians), the result should be very close to 0 (though it may be a tiny scientific notation number due to floating-point precision).
- Set the Data Type: Right-click your new "Cosine Result" column, select Change Type, and choose Decimal Number to ensure the output is treated as a number for further calculations.
- Close and Apply: Once the calculations are verified, go to the Home tab and click Close & Apply to load the results back into your data model or spreadsheet.
Common Mistakes
The most frequent error when using Number.Cos is passing degrees directly into the function. Many users assume that if they enter Number.Cos(90), they will get 0. However, Power Query interprets "90" as 90 radians, not 90 degrees. This leads to mathematically incorrect results that can be difficult to spot if you are not checking against a known value.
Another common mistake is ignoring the data type of the input column. If the column is set to "Any" or "Text", the Number.Cos function will return an error for every row. You must explicitly cast the column as a Decimal Number before applying the function.
Some users also struggle with the case sensitivity of the M language. Writing number.cos or NUMBER.COS will result in a "Token Literal expected" or "The name cannot be found" error. The function must be written exactly as Number.Cos, with a capital N and a capital C.
Tips
To make your workflow more efficient, you can combine the degree-to-radian conversion and the cosine calculation into a single step. Instead of creating two separate columns, use the formula: Number.Cos([YourDegreeColumn] * (Number.PI / 180)). This keeps your table cleaner and reduces the number of steps in your Applied Steps pane.
When dealing with very small numbers (like the result of cos(90 degrees)), Power Query might display the result in scientific notation (e.g., 6.123E-17). This is effectively zero. To make this more readable for a report, you can use the Number.Round function to round the result to a specific number of decimal places, such as Number.Round(Number.Cos([Radians]), 4).
If you are creating a complex trigonometric model, consider creating a separate "Constants" table or using a Parameter for the value of PI, although Number.PI is built-in and highly accurate, making it the preferred choice for most users.
Troubleshooting
If you encounter an "Expression.Error: We cannot convert the value to type Number" message, check your source data for nulls or non-numeric characters. A single cell containing a space or a letter in your angle column will cause the entire custom column calculation to fail for that row. Use the Replace Errors or Replace Values feature to clean your data before applying the function.
If the results seem completely wrong, double-check your conversion math. Remember that 180 degrees equals PI radians. If you accidentally multiply by 180 instead of dividing, your results will be wildly inaccurate. The correct multiplier is (Number.PI / 180).
If the Power Query editor becomes sluggish after adding the cosine column to a very large dataset (millions of rows), try to perform the calculation as late as possible in your query steps. Ensure you have filtered out unnecessary rows first to reduce the number of calculations the engine has to perform.
Frequently Asked Questions
Does Number.Cos support degrees?
No, the Number.Cos function only accepts values in radians. You must convert degrees to radians by multiplying the degree value by PI divided by 180.
What is the difference between Number.Cos and other trig functions?
Number.Cos calculates the cosine (the x-coordinate on a unit circle), while Number.Sin calculates the sine (the y-coordinate) and Number.Tan calculates the tangent.
Why is cos(90 degrees) not exactly 0?
This is due to floating-point precision. Because Number.PI is an approximation of an irrational number, the result of Number.Cos(Number.PI / 2) is a number extremely close to zero, but not exactly zero.
Can I use Number.Cos in a conditional column?
You cannot use it directly in the standard "Conditional Column" dialog box. You must use a "Custom Column" to write the M code formula required for the function.
Is Number.Cos available in both Excel and Power BI?
Yes, because both applications use the same Power Query (M) engine, the Number.Cos function works identically in both platforms.
How do I handle null values in my angle column?
If the input is null, Number.Cos will return null. To avoid this, you can use an if statement in your custom column: if [Angle] = null then 0 else Number.Cos([Angle]).