> Documentation index: https://www.gooddata.ai/docs/gooddata-ui/llms.txt lists every page of these docs as Markdown.

# Compare a measure with a previous period

> Create same-period-last-year and previous-period derived measures with newPopMeasure and newPreviousPeriodMeasure.

API reference: [newPopMeasure()](https://www.gooddata.ai/docs/gooddata-ui/latest/api-reference/sdk-model/sdk-model.newpopmeasure/index.md) (function), [newPreviousPeriodMeasure()](https://www.gooddata.ai/docs/gooddata-ui/latest/api-reference/sdk-model/sdk-model.newpreviousperiodmeasure/index.md) (function)

Use time-over-time comparison to add a measure with data from the past and compare it to another measure in a visualization. The visualization must contain at least one measure that the measure with the data from the past can reference.

The measure with the data from the past is called **derived measure**. The referenced measure is called **master measure**.

You can compare data to:
* The same period previous year
* The previous period

> **Warning:**
> Avoid creating a derived measure from an arithmetic measure that refers to another derived measure. The result is hard to interpret.

## [Compare to the same period (SP) previous year](https://www.gooddata.ai/docs/gooddata-ui/latest/data-layer/execution/time-over-time/#compare-to-the-same-period-sp-previous-year)

**Time shift**: -1 year

**Period**: 1 year

To add a SP derived measure to a visualization, use the following `newPopMeasure` factory function:

```ts
newPopMeasure(masterMeasureOrLocalId, overPeriodAttributeId, modifications)
```

where:

-  `masterMeasureOrLocalId` is the measure with data to compare; specify either the measure's `localIdentifier` or the measure itself.
-  `overPeriodAttributeId` is the identifier (or [`ObjRef`](https://www.gooddata.ai/docs/gooddata-ui/latest/api-reference/sdk-model/sdk-model.objref/index.md)) of the date dimension year attribute to use for the shift.
-  `modifications` is an optional function that receives a builder with functions to customize `format()`, `alias()`, `title()`, and `localId()`.

### Example

```tsx
import { PivotTable } from "@gooddata/sdk-ui-pivot";
import { newMeasure, newPopMeasure } from "@gooddata/sdk-model";

const masterMeasure = newMeasure("measureIdentifier", (m) => m.alias("Master Measure"));
const popMeasure = newPopMeasure(masterMeasure, "attributeYearIdentifier", (m) => m.alias("Same Period Previous Year"));

const measures = [masterMeasure, popMeasure];

export const SamePeriodPreviousYear = () => <PivotTable measures={measures} />;
```

## [Compare to the previous period (PP)](https://www.gooddata.ai/docs/gooddata-ui/latest/data-layer/execution/time-over-time/#compare-to-the-previous-period-pp)

**Time shift**: a specified number of periods

**Period**: defined by the global [date filter](https://www.gooddata.ai/docs/gooddata-ui/latest/filters/date-filter/index.md) on the date dataset that the derived measure definition references by identifier or [`ObjRef`](https://www.gooddata.ai/docs/gooddata-ui/latest/api-reference/sdk-model/sdk-model.objref/index.md)

* For an **absolute** date filter, the period is N days.
* For a **relative** date filter, the period can be N days, weeks, months, quarters, or years depending on the selected granularity.

If no global date filter is defined, the derived measure returns the same data as the master measure.

To add a PP derived measure to a visualization, use the following `newPreviousPeriodMeasure` factory function:

```ts
newPreviousPeriodMeasure(masterMeasureOrLocalId, dateDataSets, modifications);
```

where:

-  `masterMeasureOrLocalId` is the measure with data to compare; specify either the measure's `localIdentifier` or the measure itself.
-  `dateDataSets` is an array of period shifts to apply ([`IPreviousPeriodDateDataSetSimple[]`](https://www.gooddata.ai/docs/gooddata-ui/latest/api-reference/sdk-model/sdk-model.ipreviousperioddatedatasetsimple/index.md)); each item is an object with the `dataSet` prop for the date dataset identifier (or [`ObjRef`](https://www.gooddata.ai/docs/gooddata-ui/latest/api-reference/sdk-model/sdk-model.objref/index.md)) and the `periodsAgo` prop to specify the period shift.
-  `modifications` is an optional function that receives a builder with functions to customize `format()`, `alias()`, `title()`, and `localId()`.

### Example

```tsx
import { newMeasure, newPreviousPeriodMeasure, newRelativeDateFilter } from "@gooddata/sdk-model";
import { PivotTable } from "@gooddata/sdk-ui-pivot";

const filters = [newRelativeDateFilter("dateDatasetIdentifier", "GDC.time.date", -7, -1)];

// master - last 7 days measure
const masterMeasure = newMeasure("measureIdentifier", (m) => m.alias("Master Measure"));
// derived - previous 7 days measure
const previousPeriod = newPreviousPeriodMeasure(
    masterMeasure,
    [{ dataSet: "dateDatasetIdentifier", periodsAgo: 1 }],
    (m) => m.alias("Previous Period Measure"),
);

const measures = [masterMeasure, previousPeriod];

export const PreviousPeriod = () => <PivotTable measures={measures} filters={filters} />;
```

### [Comparison to the PP and absolute date filters](https://www.gooddata.ai/docs/gooddata-ui/latest/data-layer/execution/time-over-time/#comparison-to-the-pp-and-absolute-date-filters)

Be careful when combining comparison to the PP with an **absolute** date filter.
For example, when filtering from March 1 to March 31, the previous period is the previous 31 days and **not** the previous month.
For comparing over a period other than a day, use a **relative** date filter with the required granularity (month, quarter).

## [More examples](https://www.gooddata.ai/docs/gooddata-ui/latest/data-layer/execution/time-over-time/#more-examples)

See the [live examples](https://gdui-examples.herokuapp.com/time-over-time-comparison).
