I understand why the dative is used in this sentence, given the presence of the preposition по
"Я спала два раза по одному часу"
but I don't understand why it's not used in the following
"Я спала два раза по два часа"
I have become accustomed to speaking that way so it is not an issue in daily life, but I would like to know what the rule is that guides this apparent inconsistency, assuming there is such a rule. I struggled to even describe the problem, so if there is a more appropriate title please don't hesitate to edit.