function getComplexValue(value, currencies) {
    if (typeof value === 'number') {
        return value;
    }

    return value.reduce((last, { value: numberValue, currency }) => {
        const currencyMatch = currencies.find(({ currency: name }) => name === currency);
        if (!currencyMatch) {
            return last;
        }

        return last + currencyMatch.rate * numberValue;
    }, 0);
}

const valueByCategory = (subcategories, categoryId) =>
    ({ subcategory: subcategoryId }) => {
        const subcategory = subcategories.find(({ id }) => id === subcategoryId);

        return subcategory && subcategory.categoryId === categoryId;
    };

export function sumByCategory(categoryName, { rows, categories, subcategories }) {
    if (!(rows.length && categories.length && subcategories.length)) {
        return 0;
    }

    const category = categories.find(({ category: name }) => name === categoryName);
    if (!category) {
        return 0;
    }

    const { currencies, values } = rows[rows.length - 1];

    return values
        .filter(valueByCategory(subcategories, category.id))
        .reduce((last, { value }) => last + getComplexValue(value, currencies), 0);
}

const valueByType = (categoryType, categories, subcategories) =>
    ({ subcategory }) => {
        const { categoryId } = subcategories.find(({ id }) => id === subcategory);

        return categories.some(({ id, type }) => id === categoryId && type === categoryType);
    };

export function sumByType(categoryType, { row, categories, subcategories }) {
    if (!(row && categories.length && subcategories.length)) {
        return 0;
    }

    const { currencies, values } = row;

    return values
        .filter(valueByType(categoryType, categories, subcategories))
        .reduce((last, { value }) => last + getComplexValue(value, currencies), 0);
}

@@ -327,24 +327,92 @@
 %0A        %7D%0A%0A
+        // converting from currency to GBX, but rate is against GBP%0A
         retu
@@ -449,24 +449,30 @@
  numberValue
+ * 100
 ;%0A    %7D, 0);

Multiply FX values by 100 when calculating for view, to convert to GBX