How to Use Power Query Number.Tanh Function
In this article
The Number.Tanh function in Power Query calculates the hyperbolic tangent of a number, returning a value between -1 and 1. It's primarily used in data modeling and statistical analysis, especially when transforming variables for machine learning or normalizing data. You need a numeric value as input—non-numeric or null values will cause an error. Power Query is available in Microsoft Excel (2016 and later with Power Query add-in) and Power BI Desktop.
What You'll Need
- Microsoft Excel 2016 or later (with Power Query enabled), or Power BI Desktop
- A column or value containing numeric data (integers or decimals)
- Access to the Power Query Editor (via Data > Get & Transform Data > Get Data or the Power Query tab)
Step-by-Step
- Open your Excel workbook or Power BI Desktop file containing the data you want to transform.
- Select any cell in your data table (for Excel) or load your table into Power Query (for Power BI), then click Data > Get Data > From Table/Range (Excel) or Home > Transform Data (Power BI) to open the Power Query Editor.
- In the Power Query Editor, locate the column you want to apply the hyperbolic tangent to—ensure it contains only numeric values (no text, blanks, or errors).
- Right-click the column header and choose Transform > Number > Hyperbolic Tangent. Alternatively, click the column header once to select it, go to the Add Column tab, click Scientific in the Formula group, and select Tanh.
- If prompted, confirm the new column name (e.g.,
Tanh([YourColumn])) and click OK. A new column appears in the query editor containing the hyperbolic tangent values. - Review the new column: values should range between -1 and 1. For example, 0 maps to 0, 1 maps to ~0.7616, and -1 maps to ~-0.7616.
- Click Close & Load (Excel) or Close & Apply (Power BI) to load the transformed data back into your workbook or report.
Common Mistakes
- Using non-numeric data: If the column contains text, blanks, or mixed types, Power Query throws an error like “Cannot convert value 'Text' to type Number.” Always clean or filter your data first.
- Applying to null values:
Number.Tanh(null)returns null, which may break downstream calculations. UseTable.TransformColumnswith error handling or replace nulls with 0 or a default numeric value beforehand. - Confusing Tanh with Tan:
Number.Tanhis hyperbolic (based on exponentials), not trigonometric. UsingNumber.Taninstead gives incorrect results for normalization tasks. - Overlooking data types: If the column is set to
AnyorText, convert it first using Transform > Data Type toDecimal NumberorDouble. - Not refreshing after edits: Changes to source data won’t update the Tanh column until you click Refresh in Excel or Apply Changes in Power BI.
Tips
- Combine with conditional logic: Use
if [Column] = null then 0 else Number.Tanh([Column])in a custom column to handle nulls gracefully. - Normalize outliers: Tanh compresses extreme values toward ±1, making it useful for scaling variables with heavy tails—ideal for neural network inputs.
- Compare to original: Keep both columns visible during development by right-clicking the original and choosing Pin Column to verify behavior.
- Use in DAX later: If loading to Power BI, the Tanh column can feed into measures like
AVERAGEX(Sheet1, Sheet1[TanhValue])for analysis. - Check precision: For very large inputs (e.g., >20), Tanh saturates at ±1; for small values (<0.1), it approximates the identity function (Tanh(x) ≈ x).
Troubleshooting
If you encounter issues, follow these checks in order:
- Verify data type: In Power Query, hover over the column header icon (e.g., ABC for text, # for number). If it’s not a number, click the icon and select
Decimal Number. - Filter for errors: Click the filter icon on the problematic column, uncheck Blanks, and look for error rows (e.g.,
#ERROR). Use Remove Errors or Replace Errors from the Home tab. - Test manually: Create a custom column with
= Number.Tanh(2)to confirm the function works in your environment (should return ~0.9640). - Check for mixed formats: In Excel, use
ISNUMBER()in a helper column to flag non-numeric entries before loading into Power Query. - Restart Power Query: Close and reopen Excel or Power BI Desktop—occasional memory glitches cause transient errors.
- Update Power Query: In Excel, ensure the Power Query add-in is current (File > Account > Update Options > Update Now). For Power BI, update the app via Microsoft Store or installer.
Frequently Asked Questions
What does the Number.Tanh function return for large numbers?
For inputs greater than ~20, Number.Tanh returns 1 (to 15 decimal places). For inputs less than ~-20, it returns -1. This saturation behavior is inherent to hyperbolic tangent and makes it useful for bounded normalization.
Can I use Number.Tanh in a calculated column in Power BI?
No—Number.Tanh is a Power Query function, not DAX. You must apply it during data transformation in Power Query before loading. In DAX, use TANH([Column]) instead.
Why does my Tanh column show #VALUE!?
This indicates non-numeric data (e.g., text, error, or blank) in the source column. Go back to the query editor, filter for errors, and either remove or replace those rows before reapplying Tanh.
How is Number.Tanh different from Number.Exp?
Number.Exp calculates Euler’s number *e* raised to a power (ex), while Number.Tanh computes (ex – e-x) / (ex + e-x). Tanh always returns a value between -1 and 1; Exp returns positive values starting at 1.
Does Number.Tanh work with negative numbers?
Yes—Number.Tanh is an odd function, meaning Tanh(-x) = -Tanh(x). For example, Tanh(-2) returns approximately -0.9640, mirroring Tanh(2).
Can I chain multiple transformations with Number.Tanh?
Absolutely. After applying Tanh, you can scale it (e.g., multiply by 10) or combine with other functions like Number.Round. Use Transform > Format > Round or add custom steps in the Advanced Editor to chain operations cleanly.