The following topic is similar.
How to update variables while running a collection in a monitor - Help - Postman Community
It involves using an environment, and then calling the Postman API to update the environment.
It is a few years old, so there might be an easier way to achieve this now.