It is impermissible for a person to start praying while being prompted by urination or defecation, for the saying of the Prophet (peace be upon him):
No prayer should be (performed) when food is present (before the person) or when they are prompted by the call of nature (urination or defecation)
(Part No. 7; Page No. 26)
Muslim in his Sahih.
The wisdom behind this -and Allah knows best- is that this prohibits complete submissiveness in Salah. However, if a person prays in this state (prompted by urination or defecation), their Salah is valid but incomplete, due to the mentioned Hadith. Yet, they should not repeat their Salah. Whereas if Salah starts without being prompted by urination or defecation, but this urge appears during Salah, it (the Salah) will be valid and there will not be any reprehensibility regarding this, as long as it does not prevent the person from completing their Salah.May Allah grant us success. May peace and blessings be upon our Prophet Muhammad, his family, and Companions.