So I created this retirement calculator here http://www.abrandao.com/retire
And the idea is if you save enough you can so when you withdraw say 4% you don't eat too much into your savings.
But based on my calculation 4% of 500000 is $20k but next year it will be 4% of 480000 and so on.. how can I determine what % of withdrawal will keep the withdrawn value the same over a time period say 30 years.
If you are not assuming any return on the balance, you can just divide $100\%$ by the number of payments to get the percentage of the original balance you can withdraw. In your example you can withdraw $\frac 1{30}=3\frac 13\%$ of the original balance each year. The rule of thumb $4\%$ number you may have seen assumes the withdrawals increase with inflation and there is some return in excess of inflation.