Obama is the first USA president to visit Spain in 15 years - and it's the largest European ally that Obama had yet to visit as president.
His visit was overshadowed, and abbreviated, by the wave of grief and anger convulsing the United States following the police shootings of black men in Louisiana and Minnesota, and the killing of five police officers by a black gunman in Dallas. Obama addressed USA and Spanish troops, saying, "We are going to keep standing together to meet the challenges of our time in the face of terrorists networks that seek to destroy".
Obama, in his remarks, urged protesters to recognize that police officers do a hard job and to refrain from physical and verbal attacks.
"Even rhetorically", Obama said, "if we paint police officers with a broad brush - without recognizing that the vast majority of police officers are doing a really good job and are trying to protect people, and do so fairly and without racial bias - if the rhetoric does not recognize that, then we're going to lose allies in the reform process".
In a statement announcing the change of plans, the White House said the president had accepted an invitation from Dallas Mayor Mike Rawlings to visit had been accepted.
In his remarks on Sunday, Obama repeated that most US police officers are doing a good job, and said rhetoric portraying them as doing otherwise does little to build support for overhauling a criminal justice system that is widely recognized as biased against minorities.
Obama also pledged again to bring together political leaders, civil-rights advocates, and law enforcement officials to try to devise solutions to this problem. I'm so grateful for the understanding not only of his majesty but the people of Spain.
Obama says the USA and Spain are working together in some hard places, and he's particularly appreciative of the work Spain is doing to train police in Iraq.
Obama's plea for a reasoned debate came on the last day of a trip to Spain and Poland. Tourists and curiosity seekers lined some streets in hopes of catching a glimpse of Obama, and local TV aired wall-to-wall coverage of his movements.
The White House has not yet announced which day Obama will visit Dallas, although it has said it will be early next week.
Obama met with King Felipe VI at the royal palace, where he reminisced about his last visit to Spain as a backpacking young college graduate contemplating law school. "Spain is a strong North Atlantic Treaty Organisation ally, we're grateful for Spain's many decades of hosting USA forces, and we're major trading partners".
Rajoy has been unable to garner the support he needs to form a new coalition government following elections in December and repeat balloting in late June.
"I want to thank you dearly for visiting us and very specially for maintaining this visit given the present particular circumstances", Spain's King Pelipe said during a visit in Madrid, The Hill reported.
And in a speech to troops that seemed to implicitly address tensions both at home and overseas, Obama proclaimed, "People of good will will ultimately overcome the forces that seek to divide us".
It's taken the White House more than seven years to lock in Spain on Obama's foreign travel schedule.