How to Fix Excel PivotTable Calculated Field for Days Inventory Held
Question details
The user needs to accurately calculate Days Inventory Held in a PivotTable using a logarithmic formula and troubleshoot discrepancies in the results.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- Creating a calculated field in a PivotTable to determine inventory duration based on stock levels and withdrawal rates.
- Observed behavior
- The calculated formula using natural logarithms returns values that do not match the expected Days Inventory Held, likely due to data aggregation or format issues.
Ensure that your source data formats are correct, specifically verifying that the withdrawal rate is stored as a true decimal (e.g., 0.15) rather than a whole number. Also, keep in mind that PivotTable calculated fields perform operations on the sum of the base data rather than evaluating row-by-row.
Use the Correct Logarithmic Formula and Decimal Formats
Ensure the formula correctly references data fields and that percentages are strictly handled as decimals to allow the LN function to calculate properly.
When calculating Days Inventory Held, the mathematical logic relies on natural logarithms. If your 'Withdraw Rate' is formatted incorrectly in the source data, the LN function will either return an error or produce a wildly inaccurate number.
PivotTables evaluate calculated fields using the sum of the base fields. Therefore, the formula will aggregate the totals first and then apply the logarithm, which might differ from a row-level calculation.
Click any cell inside your existing PivotTable to reveal the PivotTable Analyze tab on the ribbon.
Navigate to the 'Fields, Items & Sets' button in the Calculations group and choose 'Calculated Field'.
In the Name box, type 'Days Inventory Held'. In the Formula box, carefully enter =LN('In Stock')/LN(1-'Withdraw Rate').
Check your original source data sheet to ensure the 'Withdraw Rate' column is formatted as a true decimal (e.g., 0.15) and not stored as text.
Click 'OK' to insert the calculated field into the PivotTable and cross-check the result against a manual row calculation.

Calculate Row-by-Row in the Source Table
Calculate the value in the raw data table before creating the PivotTable to completely avoid PivotTable aggregation errors.
Seamlessly Analyze Inventory Data with WPS Office
WPS Spreadsheet provides robust PivotTable functionalities, including advanced Calculated Fields and complex formulas like logarithms, allowing you to manage your inventory metrics effortlessly.
- 1. Open your file: Open your inventory data file directly in WPS Spreadsheet without format conversion.
- 2. Insert a PivotTable: Navigate to the 'Insert' tab on the top ribbon and click 'PivotTable' to generate your report.
- 3. Create Calculated Field: Under the 'PivotTable Tools' tab, select 'Calculated Field' from the menu.
- 4. Input formula: Input your LN formula referencing the stock and withdrawal fields, and click 'Add' to instantly view your calculated Days Inventory Held.

Frequently Asked Questions
Why does my Calculated Field return a #NUM! error?
The #NUM! error usually occurs in the LN function if the argument is zero or a negative number. This happens if 1 minus your Withdraw Rate results in a zero or negative value, which implies a withdrawal rate of 100% (1.0) or higher.
Can I format the result of a PivotTable calculated field?
Yes. Right-click any value inside your new Calculated Field within the PivotTable, select 'Value Field Settings', click the 'Number Format' button at the bottom, and choose your preferred format, such as a Number with two decimal places.
Why are my Grand Totals wrong for the Calculated Field?
Calculated fields do not sum the individual rows displayed above them. Instead, they apply the custom formula to the grand total of the underlying base fields. If your formula uses non-linear functions like logarithms, the formula applied to the grand total will not mathematically equal the sum of the individual row calculations.
How do I change an existing Calculated Field formula?
Click anywhere in the PivotTable, navigate to 'PivotTable Analyze' > 'Fields, Items & Sets' > 'Calculated Field'. In the dialog box, select your field's name from the 'Name' dropdown menu, modify the text in the Formula box, click 'Modify', and then click 'OK'.




