> ## Documentation Index
> Fetch the complete documentation index at: https://honeydew.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# What share of revenue comes from a specific category?

> Ratio metric that computes a specific category's share of total revenue

## Prerequisite

This recipe builds on the filtered category revenue metric from
[Revenue for a specific category](/docs/recipes/category-revenue).
Complete that recipe first.

## When would you use this?

* A stakeholder asks *"What percentage of our revenue comes from
  beverages?"* and you need a ratio they can slice by time,
  location, or truck.
* You want a governed share metric so every dashboard shows the
  same number without anyone hand-rolling a division formula.

## What this recipe builds

A ratio metric that calculates the filtered category share out of
the total revenue. Because both sides are named metrics, a change
to the filter in the underlying metric automatically flows through
to the share.

The example below computes the share of **beverages** from a
food-truck menu dataset, but the same pattern works for any
category.

Honeydew offers two flavors of share metric depending on how you
want the denominator to behave when users add grouping dimensions.

<Tip>
  This recipe uses the [filtered metrics](/docs/calculations/metrics#filtered-metrics) concept.
  See the documentation for the full technical reference.
</Tip>

## Use case 1 - Share within the current grouping

When a user slices this metric by a dimension - for example,
country - the denominator becomes total revenue *for that
country*. The share answers: *"Of this country's revenue, how
much came from beverages?"*

<Tabs>
  <Tab title="SQL">
    ```sql theme={null}
    order_detail.revenue_from_beverages / order_detail.revenue
    ```
  </Tab>

  <Tab title="YAML">
    ```yaml theme={null}
    type: metric
    entity: order_detail
    name: beverage_revenue_share
    display_name: Beverage Revenue Share
    description: |-
      Beverage items as a share of total revenue.
      Ratio metric built from revenue_from_beverages / revenue.
    datatype: float
    format_string: "0.00%"
    folder: Revenue Metrics
    sql: |-
      order_detail.revenue_from_beverages / order_detail.revenue
    ```
  </Tab>
</Tabs>

### Example - sliced by country

Each country's denominator is that country's own total revenue:

| Country       | Beverage revenue | Country total | Beverage share |
| ------------- | ---------------- | ------------- | -------------- |
| India         | \$51.8M          | \$1,093.4M    | 4.7%           |
| Germany       | \$50.1M          | \$1,030.8M    | 4.9%           |
| Brazil        | \$49.8M          | \$991.3M      | 5.0%           |
| Australia     | \$46.8M          | \$951.0M      | 4.9%           |
| United States | \$49.2M          | \$947.7M      | 5.2%           |

## Use case 2 - Share of the global total

When a user slices this metric by any dimension, the denominator
stays the same - total revenue across the entire dataset. The
share answers: *"Of all our revenue, how much came from beverages
in this slice?"*

The `group by ()` clause tells Honeydew to remove all dimensions
from the denominator's grouping context. Filters still apply,
but grouping dimensions do not change the denominator.

<Tabs>
  <Tab title="SQL">
    ```sql theme={null}
    order_detail.revenue_from_beverages / order_detail.revenue group by ()
    ```
  </Tab>

  <Tab title="YAML">
    ```yaml theme={null}
    type: metric
    entity: order_detail
    name: beverage_out_of_all
    description: |-
      The denominator is a metric and will be affected by filters,
      but not groups.
    datatype: number
    sql: |-
      order_detail.revenue_from_beverages / order_detail.revenue group by ()
    ```
  </Tab>
</Tabs>

### Example - sliced by country

Every country's denominator is the grand total (\$10.1B), so the
values represent each country's contribution to the overall
beverage revenue:

| Country       | Beverage revenue | Grand total | Beverage share |
| ------------- | ---------------- | ----------- | -------------- |
| India         | \$51.8M          | \$10.1B     | 0.51%          |
| Germany       | \$50.1M          | \$10.1B     | 0.50%          |
| Brazil        | \$49.8M          | \$10.1B     | 0.49%          |
| Australia     | \$46.8M          | \$10.1B     | 0.46%          |
| United States | \$49.2M          | \$10.1B     | 0.49%          |

The shares across all countries sum to \~5.0% - the global
beverage share. Each row tells you how much of the *entire*
company's revenue that country's beverages represent.

## How the two behave differently

Same data, side by side:

| Country | Beverage revenue | Use case 1 (per-country) | Use case 2 (global) |
| ------- | ---------------- | ------------------------ | ------------------- |
| India   | \$51.8M          | 4.7%                     | 0.51%               |
| Germany | \$50.1M          | 4.9%                     | 0.50%               |
| Brazil  | \$49.8M          | 5.0%                     | 0.49%               |

* **Use case 1** - each country's denominator is its own total.
  Answers *"what share of this country's revenue is beverages?"*
* **Use case 2** - every row divides by the grand total.
  Answers *"what share of all revenue is this country's beverages?"*

## Related reading

* [Revenue for a specific category](/docs/recipes/category-revenue) -
  prerequisite recipe
* [Filters](/docs/calculations/filters) -
  reference on `FILTER (WHERE ...)`
* [Ratio metrics](/docs/calculations/metrics#filtered-metrics-in-ratios) -
  using filtered metrics inside ratio expressions
* [Fixed empty grouping](/docs/calculations/metrics#fixed-empty-grouping) -
  locking the denominator with `group by ()`
