I am suspecting that your query does the normal thing and bring back 100 items at a time. If your filter brings back less than 2,000 items, you can sum the same filter as the gallery, but beyond that Sum() is not delegable directly on a dataset.
Please click Accept as solution if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider giving it Thumbs Up.