# Save and mail a cost view

Save a cost explorer question under a name, with a range that moves along, share it or keep it private, and have it mailed every month with its CSV.

A saved view holds the range, the granularity, the grouping and the filters. Its range is relative
(**Last month**, **This month**, **Last 30 days**, …), so the view keeps its meaning, or fixed
dates. Replace _Shop by service_ with a name of your own; names are unique in the organization.

## Prerequisites

- Permission for `billing:CreateCostView`. Without it, the console offers no **Save this view**.
- `billing:GetCostAndUsage` to open **Cost explorer** and to see saved views.
- `billing:UpdateCostView` to change a view and `billing:DeleteCostView` to delete one.
- `iam:UpdateOrganization` for a view that groups or filters by where resources run.
- Room in the **Saved cost views** quota (see [Quotas](quotas.md)).

## Save a view

**Console**
1. Open **Cost explorer** and set up the view (see [Explore costs](explore-costs.md)).
2. Choose **Save this view**.
3. For **Name**, enter Shop by service.
4. For **Range**, choose a range that moves along, or the dates on screen (fixed).
5. (Optional) Clear **Shared with the organization** to keep the view to yourself.
6. (Optional) Select **Mail monthly with its CSV (on the 2nd, 08:00, for the month before)** and
   enter the recipients.
7. Choose **Save**.

The view is listed under **Open a saved view…**; opening it sets the explorer to its question.

**CLI**
```sh
dcloud billing cost-views create --name "Shop by service" \
  --state '{"range":{"type":"relative","preset":"last-month"},"groupBy":{"dimension":"service"}}'
```

**API**
```http
POST /billing/api/cost-views
{
  "name": "Shop by service",
  "state": { "range": { "type": "relative", "preset": "last-month" }, "groupBy": { "dimension": "service" } },
  "report": { "enabled": true, "recipients": ["jane.doe@example.com"] }
}
```

**Terraform**
```hcl
resource "dcloud_cost_view" "shop_by_service" {
  name = "Shop by service"
  state = jsonencode({
    range       = { type = "relative", preset = "last-month" }
    granularity = "day"
    groupBy     = { dimension = "service" }
    filters     = []
  })
  report = jsonencode({ enabled = true, recipients = ["jane.doe@example.com"] })
}
```

Write all four keys of `state`: the API fills in the ones left out.

The presets are `last-7-days`, `last-30-days`, `last-90-days`, `month-to-date`, `last-month`,
`last-3-months` and `year-to-date`; a fixed range is `{ "type": "fixed", "start", "end" }`.

## Result

A shared view is visible to everyone with `billing:GetCostAndUsage`; a private one only to you, and
answers `404` for everyone else. A view with its monthly mail on is sent on the 2nd for the month
before, with the view's grouping and filters and a CSV of the month's cost lines, to the view's
recipients. `GET /billing/api/cost-views/{id}/report` shows that mail for a month without sending
it.

## Change or delete a view

The console deletes the open view with **Delete view**, at once, without asking again; it does not
change views. Over the CLI and the API:

```sh
dcloud billing cost-views update view-id --no-shared
dcloud billing cost-views delete view-id
```

Deleting a view stops its monthly mail.

## When saving is refused

- `409` "A cost view named “…” already exists" when the name is taken.
- `409` "Quota exceeded: Saved cost views (billing.cost-views) — …" past the quota.
- `403` "Grouping or filtering by where resources run needs iam:UpdateOrganization (the org’s
  Infrastructure settings)".

## Related

- [Explore costs](explore-costs.md)
- [Get the monthly cost report](get-the-monthly-cost-report.md)
- `billing.cost-views.create` · `dcloud billing cost-views create` · `dcloud_cost_view`
