Skip to main content
This gem runs in .

Overview

Use the WeightedAverage gem to calculate the weighted average of a numeric field by using another numeric field as the weight. You can also calculate weighted averages separately for groups of records. The output is an aggregated result set. If no grouping fields are selected, the gem returns a single row containing the weighted average. If grouping fields are selected, it returns one row per group with the grouping fields and the calculated weighted average.

Prerequisites

Add prophecy_basics package version 1.0.11 or higher to your project.

Parameters

ParameterDescription
Value Field (Numeric)Select the numeric field that contains the values to average.
Weight Field (Numeric)Select the numeric field that contains the weights used in the calculation. When all records have the same weight, the result is equivalent to a standard average.
Output Field NameEnter the name of the output field that stores the calculated weighted average.
Grouping Fields (Optional)Optionally select one or more fields to calculate the weighted average separately for each group. If you do not select grouping fields, the gem calculates a single weighted average across all input records.

How it works

The weighted average is calculated as: (value×weight)weight\frac{\sum (value \times weight)}{\sum weight} Each value is multiplied by its corresponding weight. The results are summed, and that sum is divided by the total weight. If you specify grouping fields, the gem performs this calculation independently for each group.

Output

If you do not select grouping fields, the gem returns a single row and column with the weighted average. If you select grouping fields, the gem returns one row per group with the weighted average. The output includes the selected grouping fields, if any, and the calculated weighted average using the specified output field name.

Notes

  • Both the Value column and the Weight column must be numeric.
  • When weights are equal across all records, the result is the same as a standard average.
  • Grouping fields are optional and allow per-group calculations.

Example

You want to calculate the weighted average price of items sold, where each price is weighted by total_sale value. Input:
ItemPricetotal_sale
Shirt202
Pants501
Jacket1003
Configuration:
  • Value Field: Price
  • Weight Field: Total_Sale
  • Output Field Name: weighted_avg_price
  • Grouping Fields: (none)
Calculation:
  • (20 × 2 + 50 × 1 + 100 × 3) / (2 + 1 + 3)
  • (40 + 50 + 300) / 6 = 390 / 6 = 65
Output:
weighted_avg_price
65

Example with grouping

If you group by Category:
CategoryPriceTotal_Sale
Tops202
Tops501
Outerwear1003
Configuration:
  • Grouping Fields: Category
Output:
Categoryweighted_avg_price
Tops30
Outerwear100