Certainly! Let's work together to optimize your Excel formula. We'll start by understanding what your current formula does and then see how we can simplify it while ensuring it meets your requirements for adjusting the subscriber counts when the API gives unrealistic values.
1. Understanding Your Current Formula
Your original formula is:
excel Copy code
=ЕСЛИ(ОСТАТ(B20;100000)<>0; B20; ЕСЛИ(ЛЕВСИМВ(D20;3)<>ЛЕВСИМВ(B20;3); ЕСЛИ(ЛЕВСИМВ(B20;3)=ЛЕВСИМВ(B21;3); D20+(B20-D20)+СЛУЧМЕЖДУ(894; 5678); ЕСЛИ(B21-B20>10^(ДЛСТР(B20)-3); B20+СЛУЧМЕЖДУ(0; 82322); ЕСЛИ(B21-B19<>10^(ДЛСТР(B20)-3); B20+СЛУЧМЕЖДУ(20322; 67655); ЕСЛИ(B21-B19<=2*10^(ДЛСТР(B20)-3); (B19+B21)/2+СЛУЧМЕЖДУ(-4332; 7021); D20+(B20-D20)+СЛУЧМЕЖДУ(-3894; 15678))))); D20)))
Let's translate and break this down step by step. (Note: I've translated the Russian Excel function names to their English equivalents for clarity.)
excel Copy code
=IF(
MOD(B20, 100000) <> 0,
B20,
IF(
LEFT(D20, 3) <> LEFT(B20, 3),
IF(
LEFT(B20, 3) = LEFT(B21, 3),
D20 + (B20 - D20) + RANDBETWEEN(894, 5678),
IF(
B21 - B20 > 10^(LEN(B20) - 3),
B20 + RANDBETWEEN(0, 82322),
IF(
B21 - B19 <> 10^(LEN(B20) - 3),
B20 + RANDBETWEEN(20322, 67655),
IF(
B21 - B19 <= 2 * 10^(LEN(B20) - 3),
(B19 + B21) / 2 + RANDBETWEEN(-4332, 7021),
D20 + (B20 - D20) + RANDBETWEEN(-3894, 15678)
)
)
)
),
D20
)
)
2. Purpose of the Formula
From your description, the formula is designed to adjust daily subscriber counts when:
The API provides unrealistic values (e.g., sudden large jumps or drops).
You want to generate more realistic figures by adding some randomness within certain bounds.
3. Identifying Key Components
Condition 1: MOD(B20, 100000) <> 0
If the subscriber count (B20) isn't a multiple of 100,000, use B20 as is.
Condition 2: LEFT(D20, 3) <> LEFT(B20, 3)
Comparing the first three digits of previous adjusted value (D20) and current value (B20).
Random Adjustments:
Various RANDBETWEEN functions add randomness to smooth out unrealistic spikes.
4. Optimization Strategy
Given the complexity, let's aim to simplify the formula by:
Reducing nested IF statements.
Using helper columns or intermediate calculations if needed.
Clarifying conditions and ensuring they cover all necessary cases.
5. Simplifying the Formula
Let's start by setting up variables to make the formula more readable:
CurrentValue = B20
PreviousAdjusted = D20
NextValue = B21
PreviousValue = B19
Simplified Pseudocode Logic:
Check if CurrentValue is not a multiple of 100,000:
If so, use CurrentValue as is.
Else, compare the first three digits of PreviousAdjusted and CurrentValue:
If they are different:
If the first three digits of CurrentValue and NextValue are the same:
Adjust using PreviousAdjusted + (CurrentValue - PreviousAdjusted) + small random number
Else:
Check differences between NextValue, CurrentValue, and PreviousValue to decide on adjustments.
Else:
Use PreviousAdjusted as the adjusted value.
6. Proposed Optimized Formula
Now, let's attempt to create an optimized formula that captures your intended logic but is easier to read and maintain.
excel Copy code
=IF(
MOD(B20, 100000) <> 0,
B20,
IF(
LEFT(D20, 3) <> LEFT(B20, 3),
IF(
LEFT(B20, 3) = LEFT(B21, 3),
D20 + (B20 - D20) + RANDBETWEEN(894, 5678),
IF(
B21 - B20 > POWER(10, LEN(B20) - 3),
B20 + RANDBETWEEN(0, 82322),
IF(
B21 - B19 <> POWER(10, LEN(B20) - 3),
B20 + RANDBETWEEN(20322, 67655),
IF(
B21 - B19 <= 2 * POWER(10, LEN(B20) - 3),
(B19 + B21) / 2 + RANDBETWEEN(-4332, 7021),
D20 + (B20 - D20) + RANDBETWEEN(-3894, 15678)
)
)
)
),
D20
)
)
7. Simplification Using Helper Columns
To further optimize, we can use helper columns to store intermediate calculations. This will make the main formula less complex.
Helper Column E (e.g., E20): Calculate POWER(10, LEN(B20) - 3) once.
Helper Column F (F20): Calculate FirstThreeDigitsCurrent = LEFT(B20, 3)
Helper Column G (G20): Calculate FirstThreeDigitsPreviousAdjusted = LEFT(D20, 3)
Helper Column H (H20): Calculate FirstThreeDigitsNext = LEFT(B21, 3)
Updated Formula with Helpers:
Assuming you have:
E20 = POWER(10, LEN(B20) - 3)
F20 = LEFT(B20, 3)
G20 = LEFT(D20, 3)
H20 = LEFT(B21, 3)
Then the main formula becomes:
excel Copy code
=IF(
MOD(B20, 100000) <> 0,
B20,
IF(
G20 <> F20,
IF(
F20 = H20,
D20 + (B20 - D20) + RANDBETWEEN(894, 5678),
IF(
B21 - B20 > E20,
B20 + RANDBETWEEN(0, 82322),
IF(
B21 - B19 <> E20,
B20 + RANDBETWEEN(20322, 67655),
IF(
B21 - B19 <= 2 * E20,
(B19 + B21) / 2 + RANDBETWEEN(-4332, 7021),
D20 + (B20 - D20) + RANDBETWEEN(-3894, 15678)
)
)
)
),
D20
)
)
This approach reduces the complexity within the formula by moving repeated calculations out of the main formula.
8. Alternative Simplified Approach
Alternatively, consider if we can make the logic even simpler by defining clear conditions for when adjustments should happen.
For instance:
If the daily change is unrealistic (e.g., a spike or drop beyond a certain threshold), adjust it.
Otherwise, use the API value or a smoothed average.
Example Simplified Formula:
Assuming:
Threshold = Define a maximum acceptable daily change (e.g., 100,000 subscribers).
DailyChange = B20 - B19
excel Copy code
=IF(
ABS(B20 - B19) > Threshold,
D19 + AVERAGE(B19:B21) + RANDBETWEEN(-5000, 5000),
B20
)
This formula:
Checks if the daily change exceeds the threshold.
If it does, it adjusts the current value by adding a random fluctuation around the average of the surrounding days.
If not, it uses the API value as is.
9. Recommendations
Define Clear Adjustment Rules: Simplify your conditions by setting clear rules for what constitutes an unrealistic value.
Use Moving Averages: Incorporate moving averages to smooth out spikes.
Limit Randomness: Use smaller ranges in RANDBETWEEN to prevent introducing large fluctuations.
10. Final Optimized Formula
Based on the above, here's a potential optimized formula:
excel Copy code
=IF(
ABS(B20 - B19) > Threshold,
D19 + ((B19 + B21) / 2 - D19) + RANDBETWEEN(-5000, 5000),
B20
)
Where:
Threshold is a defined name or cell where you set the maximum acceptable daily change.
D19 is the previous adjusted value.
11. Setting the Threshold
You can set Threshold based on historical data. For example:
excel Copy code
Threshold = 1.5 * AVERAGE(ABS(B2:B31 - B1:B30))
This calculates the average daily change and multiplies it by 1.5 to set a reasonable threshold.
12. Conclusion
By simplifying the formula, you:
Make it easier to maintain and adjust in the future.
Reduce the chance of errors due to complex nested conditions.
Improve calculation performance in Excel.
Let me know if this meets your needs or if you'd like further assistance refining the formula!