HMRC guidance in this area:
Self-employment – business commencement and cessation provisions
4.7 As a non-UK resident you will pay tax on profits:
from a trade, profession or vocation (business) you carry on in the UK
from the UK part of a business carried on partly in the UK and partly elsewhere
4.8 There are special rules which apply when you start or end your business (‘commencement’ and ‘cessation’ rules). These rules may apply when you cease to be resident in the UK even though you’ve not started or ended your business. The commencement and cessation rules will apply if you:
have been carrying on a business
cease being resident in the UK
And you continue to carry on that business. You’re ‘deemed’ to have ceased one business and started another from the date of the change in your UK residence status or, if split year treatment applies, from the split year date.
4.9 Unless split year treatment applies, the cessation of your residence in the UK takes place at the end of the last tax year of UK residence. Your business is deemed to have ceased and a new business to have commenced at that time. This may affect the amount of tax you’ve to pay.
4.10 Where the ‘deemed’ cessation of one business and commencement of another applies, for UK tax purposes any unused losses in the UK business, or in the UK part of the business, before the change in your UK residence status can be carried forward and set against the profits of the ‘deemed’ commencing business that arise in the UK.
4.11 Most trades and professions are carried out in a particular location such as a shop or factory. Therefore, ceasing to be UK resident usually means the location of the business changes (for example from one country to another). The fact that the business is in a completely new location is likely to mean that it has:
a different structure
a different customer base
And that the business has in fact ceased and a new business has commenced. In these circumstances you will not be able to carry forward losses from the old business to set against profits of the new one.
4.12 Some businesses are not carried out from a single location in this way. These businesses are mainly carried on by professional people, wherever in the world the person happens to be. Examples of this would include:
sportsmen or women
Effect of split year treatment when you carry on a trade, profession or vocation
4.13 If split year treatment applies to you, the deemed cessation and commencement takes place at the end of the UK part of the year. For the year of leaving the UK, you will be liable to UK tax on:
the proportion of your profits which reflect the profits you made in the UK part of the tax year
the profits from a UK trade, or profits from the part of the trade carried on in the UK, during the overseas part of the tax year - if the deeming rule in paragraph 4.9 apply, any unused losses in the UK trade or the UK part of the trade before the deemed cessation can be carried forward and used against profit of the deemed commencing business
The following link may help on the Spanish side of the equation: