It should be noted that the 'when' condition on action sets are evaluated at time of render, any activites or changes made in the UI (without a refresh) won't impact the execution of that step. In some cases it may be better to wrap the desired action in something that tests the property/parameter server side and not conditionalise the actual action set.
When rules on UI control Action Sets are evaluated at time of render, if you need divergent behavior you will need to cater for it in the activity for the new window.
For instance, your activity could do a show-html of a 'no records to export' when no records, and downloadfile when there are.
Alternately the button itself can have an enabled/disabled visible/hidden condition on it based on if records exists; the dynamic layout where it resides could then have a conditional refresh on the sources pxResultCount so it is not clickable in the event there is nothing.
As described earlier the when rule evaluation is done at the time of screen rendering but not in the middle of the action set execution. Hence the when rule is not considered or calculated when the FLAG value is changed in Step 1. This calculation is already done and actions are determined.
However, as you are only making use of a single property for condition evaluation.
I would recommend using Other property option rather than When Rule.
When using this option, make sure to include a TextInput set to this property somewhere on the screen. You can set it's visibility to hidden. But this property should exist somewhere in the DOM for the Other Property condition to validate.
On the newly included property, set the visibility using display:none in the Advanced properties on presentation Tab.
Unfortunately there is nothing more helpful to offer on this problem at this time. This is a known issue and has been reported to Pega multiple times in the past.
I have no doubt that your issue is reproducible, the root cause goes back to what Shashi already referred to in his previous reply ----> the when rule evaluation is done at the time of screen rendering but not in the middle of the action set execution. Hence the when rule is not considered or calculated when the FLAG value is changed in Step 1. This calculation is already done and actions are determined.
If the use of Other Property does not workaround your situation, then you will need to look at another way in your setup maybe why way of custom code/design to achieve your requirement.