With the given information isn't much possible for transfering the calculation into the script but the following expression should be perform a lot better:
rangesum(avg({< [Measure] = {'E03'}>} [Weight]),
avg({< [Measure] = {'E04'}>} [Weight]),
avg({< [Measure] = {'E05'}>} [Weight]),
avg({< [Measure] = {'E06'}>} [Weight]),
avg({< [Measure] = {'E07'}>} [Weight]),
avg({< [Measure] = {'E08'}>} [Weight]))
Range-functions handle each not numeric values with 0 and set analysis is more performant the if-loop.