Search HoldMyTwist

Browse all posts

Make Numeric aggregator: sum quantities by category

Compare raw and cleaned category labels. Then sum up the quantity from a single grouped array. Also get a total count of rows.

Loose stationery sorted into two trays beside packing boxes

Use Make Numeric aggregator to summarize imaginary order quantities per category. It's done in Make Free, without any external app integrations.

Before you start

For a one off table? Better to use a spreadsheet. Use this step if the totals should be in a repeated Make workflow. You'll need to have a make account to do this tutorial. You can either get started with Make Free (opens in a new tab) or log into your Make account. (Be aware of the current plan limits (opens in a new tab) before you start working on a more complicated scenario).

1. Create three practice records

Click on Create scenario, enter Category quantity totals as the name, then insert JSON > Parse JSON. Set Data structure as blank. In the JSON string, copy and paste the given JSON array.

Click on Save, and then Run once. If you get an unused-output warning, Run anyway (this is practice!). Look up in the output bubble, right above JSON.

Text to copy

[
{"category":"Stationery","quantity":2},
{"category":"stationery ","quantity":3},
{"category":"Packaging","quantity":4}
]

The JSON array up above returns three output bundles. Each bundle is a record. There is a trailing space in the second category value. Keep this in mind when comparing grouping values (raw vs cleaned). Use Enlarge image followed by Open full-size image to see smaller controls in the screenshot.

Parse JSON output showing three fictional records with quantities 2, 3 and 4

Parse JSON output showing three fictional records with quantities 2, 3 and 4
Open full-size image

2. Sum quantities by category

  1. add (by click the plus next to JSON) Tools > Numeric aggregator
  2. Source Module = JSON - Parse JSON [1] Aggregate function = SUM
  3. Click Value - pick the purple quantity element from JSON mapping panel
  4. Enable Show advanced settings In Group by pick JSON's category element.
  5. Click Save then Run once and Run anyway (if asked)
  6. Open the bubble above Tools

With the raw labels we get three different sums

  • The exact key (capital S at beginning) Stationery = 2
  • The different key (all small with a space inside the key's ending) stationery = 3
  • The key Packaging = 4

Numeric aggregator output split into three groups by inconsistent category labels

Numeric aggregator output split into three groups by inconsistent category labels
Open full-size image

The original tags just divided your stationery into 2 and 3 totals.

Watch out

Group by will be based on the value of the category after evaluation. Different capitalisations or edge spaces may cause one category to be split into separate sums. So only "clean" the grouping value if you want these to be ignored. Now, lets combine the two stationery entries.

3. Clean the grouping value

  1. Open the Numeric aggregator. Grab all in Group by. Paste the following expression. Assume that JSON is module 1.
Text to copy

{{lower(trim(1.category))}}

trim strips both leading spaces AND trailing spaces off category text. lower will make all category text lower case. Group by use these functions on category. The separate Value field is mapped to quantity as before. Stop processing after empty aggregation is left unchecked for this practice.

Numeric aggregator settings using SUM, quantity and lower trim category grouping

Numeric aggregator settings using SUM, quantity and lower trim category grouping
Open full-size image

The settings add by SUM of quantity. The Group by is set as lower(trim(1.category)).

  1. After pressing Save - press Run once. If you get unused output warning, select Run anyway.
  2. Check result bubble of Tools. Two bundles of outputs should be shown: Key: stationery, result: 5. Key: packaging, result: 4.

Numeric aggregator output showing stationery total 5 and packaging total 4

Numeric aggregator output showing stationery total 5 and packaging total 4
Open full-size image

The cleaned-up grouping of the labels gives us two bundles of output: stationery with result 5 and packaging with result 4.

4. Save the practice scenario

  1. Use Save on bottom tool bar. If Make gives you a warning that final output is not used, then select Save anyway.
  2. Scenario remains Inactive. This will stop at result inspector and does not send anything out.

Note that these totals are for the records passed in for this run. These totals are not summed up between different runs. In a larger flow you will need to know which records are in the aggregator. Pass the aggregator's Key and result to the next module.

If the totals look wrong

If you are not getting quantiy or category in the mapping panel, try JSON by itself first. If you have a formula that is referencing the wrong module number, sub 1 with your JSON module number. If they are still seperate, try and make sure they are spelled right. Neither trim or lower will fix spelling errors. Check your numeric quanities for accuracy before you trust the totals.

If you also need a row count

Don't change Numeric aggregator practice. Add one more scenario. Let's call it Grouped totals and row count practice. It should give both quantity sum and input row count from the same grouped array. If quantity is 0 - this is still 1 input row.

  1. Now add JSON > Parse JSON. Paste initial 3 records. Invoke it once. Add Flow Control > Array aggregator. Use Source Module as JSON. Target structure type should remain Custom. Choose quantity in Aggregated fields. Click Show advanced settings. Write {{lower(trim(1.category))}} into Group by. Save module.
  2. Put in Tools > Set variables. Create a variable category. Assign as value the Key token from aggregator. Add 2 variables below.

For quantity_total set:

Text to copy

{{sum(map(2.array; "quantity"))}}

For row_count, simply do:

Text to copy

{{length(2.array)}}

Formulas will assume that your Array aggregator is module 2. If it's a different number, use that module's Array[] token in each of the formulas. Check the docs: map and length (opens in a new tab) & sum (opens in a new tab).

  1. Save the module and scenario. Run once. Check both Tools ops. See stationery: quantity_total 5 row_count 2 and packaging: quantity_total 4 row_count 1.
  2. Switch JSON input to 1 record (below) then save & run. Now it should just be packaging: quantity_total 0, row_count 1. Switch back to 3 records input. Save & run again. Should see both groups back. Schedule Inactive.
Text to copy

[{"category":"Packaging","quantity":0}]

row_count counts input row within that group (including a zero quantity row). This is not the count of units or orders (unique).

row_count and other values are based on input records in this run.

Full Make editor showing the stationery group with quantity_total 5 and row_count 2 from the same grouped array.

Full Make editor showing the stationery group with quantity_total 5 and row_count 2 from the same grouped array.
Open full-size image

For category totals (fictive records) start with Make Free (opens in a new tab) and see the group label before and after cleaning. Inspect both output bundles. Don't forget to turn off the scenario.

Sources: Make Academy: Numeric aggregators (opens in a new tab); aggregation scope and Group by (opens in a new tab); trim and lower (opens in a new tab). Screenshots show the fictional practice scenario.

Share this post