For the regional half of IRPF, it does not matter where your company is: what matters is where you live.
The days rule
You are taxed in the region where you spent the most days of the year. If that cannot be determined, the place where your main interests are (for example, where you earn most of your income) is used and, as a last resort, your last declared residence.
If you move mid-year
The whole year counts: if you live in Madrid from January to August and in Valencia from September to December, you are taxed in Madrid that year. Your employer, however, may adjust withholding from the move onwards.
Registering is not enough
Moving your padrón (municipal registration) to a lower-tax region without living there is a fake move. The tax office spots it through utility use, card payments or your health centre, and there is a specific rule: if a move is made to pay less and you soon move back, it is ignored.
How much it can be worth
On €45,000 gross, the difference between regions can be hundreds or thousands of euros a year. See the table for €45,000 gross or the Madrid and Catalonia comparison.
Special cases
The Basque Country and Navarra have their own residence rules under their foral system, and Ceuta and Melilla set extra requirements for their deduction.
Choose your region of residence in the calculator to see your case.