Lewis Hamilton says he had to work hard to beat team-mate Valtteri Bottas and take his 97th pole position in Portimao.