Skip to content

bigfunctions > weighted_average

weighted_average

Call or Deploy weighted_average ?

✅ You can call this weighted_average bigfunction directly from your Google Cloud Project (no install required).

  • This weighted_average function is deployed in bigfunctions GCP project in 39 datasets for all of the 39 BigQuery regions. You need to use the dataset in the same region as your datasets (otherwise you may have a function not found error).
  • Function is public, so it can be called by anyone. Just copy / paste examples below in your BigQuery console. It just works!
  • You may prefer to deploy the BigFunction in your own project if you want to build and manage your own catalog of functions --> Read Getting Started. This is particularly useful if you want to create private functions (for example calling your internal APIs).
  • For any question or difficulties, please read Getting Started.
  • Found a bug? Please raise an issue here

Public BigFunctions Datasets are like:

Region Dataset
eu bigfunctions.eu
us bigfunctions.us
europe-west1 bigfunctions.europe_west1
asia-east1 bigfunctions.asia_east1
... ...

Description

Signature

weighted_average(element, weight)

Description

Returns the weigthed average elements.

Examples

Calculate average unit price

with sample_data as (

  select 10 as grade, 1 as ponderation
  union all
  select 13 as grade, 2 as ponderation

)


select bigfunctions.eu.weighted_average(grade, ponderation)
from sample_data
with sample_data as (

  select 10 as grade, 1 as ponderation
  union all
  select 13 as grade, 2 as ponderation

)


select bigfunctions.us.weighted_average(grade, ponderation)
from sample_data
with sample_data as (

  select 10 as grade, 1 as ponderation
  union all
  select 13 as grade, 2 as ponderation

)


select bigfunctions.europe_west1.weighted_average(grade, ponderation)
from sample_data
+------------------+
| weighted_average |
+------------------+
| 12               |
+------------------+

Use cases

A teacher wants to calculate the weighted average grade for a student. The student has two grades:

  • Quiz: Grade = 10, Weight = 1 (Quizzes are worth less)
  • Exam: Grade = 13, Weight = 2 (Exams are worth more)

Using the weighted_average function, the calculation would be:

SELECT bigfunctions.{region}.weighted_average(grade, weight) AS weighted_average
FROM (
    SELECT 10 AS grade, 1 AS weight UNION ALL
    SELECT 13 AS grade, 2 AS weight
);

This would return 12, as shown in the example. The exam grade (13) contributes more to the final average because it has a higher weight.

Other use cases:

  • Calculating average stock prices: Where element is the price of the stock and weight is the number of shares held at that price.
  • Determining the weighted average cost of capital: Where element is the cost of each type of capital (debt, equity, etc.) and weight is the proportion of each type of capital in the company's capital structure.
  • Computing the weighted average of customer satisfaction scores: Where element is the satisfaction score and weight is the number of customers who gave that score.
  • Creating a composite index from multiple indicators: Where element is the value of each indicator and weight reflects the importance of each indicator in the overall index. For instance, a happiness index could be created weighting factors like GDP per capita, life expectancy, and social support.

In essence, anytime you need an average where some elements contribute more than others, the weighted_average function is useful.