There are no rules that say exactly at what distance you should dip the headlights, it depends on the situation. However, you must dip the headlights just before the oncoming vehicle, or vehicle in front, is so close that the driver can be dazzled.
It can be difficult to determine at what distance to dip the headlights. One way of thinking is to wait until your car's headlights and the oncoming vehicle's headlights together illuminate the road section between you.
You also know that it is time to dip the headlights if you are dazzled by the oncoming vehicle's headlights or if the driver of oncoming vehicle dips their headlights.