Compare a measure with a previous period
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
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:
newPopMeasure(masterMeasureOrLocalId, overPeriodAttributeId, modifications)
where:
masterMeasureOrLocalIdis the measure with data to compare; specify either the measure’slocalIdentifieror the measure itself.overPeriodAttributeIdis the identifier (orObjRef) of the date dimension year attribute to use for the shift.modificationsis an optional function that receives a builder with functions to customizeformat(),alias(),title(), andlocalId().
Example
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)
Time shift: a specified number of periods
Period: defined by the global date filter on the date dataset that the derived measure definition references by identifier or ObjRef
- 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:
newPreviousPeriodMeasure(masterMeasureOrLocalId, dateDataSets, modifications);
where:
masterMeasureOrLocalIdis the measure with data to compare; specify either the measure’slocalIdentifieror the measure itself.dateDataSetsis an array of period shifts to apply (IPreviousPeriodDateDataSetSimple[]); each item is an object with thedataSetprop for the date dataset identifier (orObjRef) and theperiodsAgoprop to specify the period shift.modificationsis an optional function that receives a builder with functions to customizeformat(),alias(),title(), andlocalId().
Example
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
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
See the live examples.
Related
Create simple measures with newMeasure, arithmetic measures from two operands with newArithmeticMeasure, and inline MAQL measures with newInlineMeasure.
Configure DateFilter with filter options, granularities, and a date format, and handle the applied selection.
Create attribute, date, measure-value, and ranking filters and apply them to charts, InsightView, or individual measures.
Display a single KPI value or comparison with the Headline component.
Create, inspect, and modify execution attributes, filters, measures, and sort items with SDK model factories, builders, and accessors.