One thing I know can cause this type of error, thought they may well be others:
When text-merging a global field, if that global field is in a separate table, be sure that there is an actual record count of 1 (or more) in that table. In some cases, global values appear as expected even without an existing record, but they can also disappear, and calculations based on them can fail if the global record is zero.
I recently built a small solution which used a globals table for things such as dialog-box fields, and then tested the contents. The dialog appeared with the fields, took my entries just fine, but the calculation-function tests on the field contents failed until I created a single record for that table, then all was OK. It took me a few minutes to realize that the record was missing, as the fields were actually enterable and editable, even without a record, when accessed via the custom dialog!
After the 3rd re-applying of the merge fields - its working properly. Solved.
Globals were all fine. From a UI table, 1 record. Fields and data were all established before I hosted the file. I removed all of the merge fields from the layout, put them in again, and after a couple of times doing this they showed up.