/pigment:iterating-with-previous-and-cyclesfrom Pigment
Guides writing PREVIOUS, PREVIOUSOF, and PREVIOUSBASE formulas for iterative calculations, configuring calculation cycles, and debugging circular dependency errors in Pigment.
Ships in Pigment. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
Iterative calculations let a metric reference its own previous value along a dimension (typically time). Use them only for true metric-level circular dependencies; Pigment does not support cell-level circulars like Excel.
| Scope | Function | When to use |
|---|---|---|
| Single block | PREVIOUS(Dimension [, Offset]) | All circular-referencing formulas are in one block |
| Multiple blocks | PREVIOUSOF(Metric) | Circular references span different blocks; requires an iterative calculation cycle |
Rule: loop within a single metric with PREVIOUS; loop across multiple metrics with PREVIOUSOF. PREVIOUSBASE is deprecated; use PREVIOUSOF instead.
Syntax: PREVIOUS(IteratingDimension [, Offset])
The argument is the iterating dimension, not the metric. Offset defaults to 1; can be a positive integer or an Integer metric on the same dimensions.
Common error: PREVIOUS('My Metric Name') fails with Unknown List: My Metric Name. The fix: use PREVIOUS(Month) — the dimension, not the metric name.
// Cash balance roll-forward (single metric)
Cash = PREVIOUS(Month) + Income - Expense
Use PREVIOUS only when the entire roll-forward lives in one metric. When Beginning and Ending are separate metrics, use PREVIOUSOF and a cycle (see below).
The engine computes the entire formula (including everything before and after PREVIOUS) before moving to the next cell. Post-processing (filtering) should happen in a separate metric.
// BAD — returns blanks everywhere including where the filter is TRUE
IFBLANK('Source', PREVIOUS(Month))
['Filter Boolean']
Why: for earlier items where the filter is FALSE, the engine emits blanks. When TRUE, PREVIOUS points to a blank cell, propagating BLANK forward.
Fix: separate the PREVIOUS expression from the filter:
// Metric 1 — 'Filled Source'
IFBLANK('Source', PREVIOUS(Month))
// Metric 2 — 'Filtered Result'
'Filled Source'['Filter Boolean']
Before using PREVIOUSOF, create an iterative calculation cycle listing all participating metrics and the iterating dimension. PREVIOUSOF will not resolve without a cycle. Do not rewrite to PREVIOUS on itself; this changes the mathematical meaning.
tool:list_cycles to check if a cycle already exists.tool:create_cycle with cycleName, iterativeDimensionId, and all metricIds.tool:update_cycle to add or remove metrics from an existing cycle.When naming a cycle in a reply, use the mention syntax with its id (returned by tool:list_cycles or tool:create_cycle) and its application id: [<cycle name>](mention:cycle:<cycle-uuid>@<app-uuid>). It renders as a chip that opens the application's Calculations settings. Fall back to the backticked cycle name only when the ids are not available.
Use this when beginning and ending balances are separate metrics (inventory, cash, loan balances). Create the cycle first, then write PREVIOUSOF formulas.
// Beginning balance — after cycle exists
IFDEFINED(PREVIOUSOF('Ending Balance'), PREVIOUSOF('Ending Balance'), 'Opening Balance')
// Ending balance — inflows and outflows may be direct references or intermediate metrics
'Beginning Balance' - 'Outflows' + 'Inflows'
Do not substitute 'Ending Balance'[SELECT: Month - 1] for PREVIOUSOF; it does not participate in the cycle and breaks cross-metric roll-forwards.
// Seed with 0 when no prior period exists; add net income each month
IFDEFINED(PREVIOUS(Month), PREVIOUS(Month), 0) + IFDEFINED('Net Income', 'Net Income', 0)
Use IFDEFINED to handle the first period (no prior balance) and months with no data.
Before collapsing a PREVIOUSOF cycle into PREVIOUS, confirm that separate Beginning/Ending metrics are not required. Many planning models expose both metrics for reporting, reorder logic, or drill-down views; in those cases keep the two-metric PREVIOUSOF + cycle pattern even if the math could collapse.
Only collapse when all of the following are true:
// Collapsed version (single metric only — often more performant)
'Ending Balance' = PREVIOUS(Month) + 'Movements'
When in doubt, prefer PREVIOUSOF + cycle. Collapsing changes which metrics exist in the model and is harder to extend later.
When a cycle is active, Pigment builds a combined base formula. Three individually valid formulas can combine into an invalid base formula.
Key rule: modifiers (BY, SELECT, REMOVE, FILTER) are supported as long as they do not reference the iterating dimension. Window functions (CUMULATE, MOVINGAVERAGE, MOVINGSUM) are supported within cycle metrics.
// BAD — REMOVE on the iterating dimension inside a cycle metric
'Incoming' = 'Beginning'[REMOVE: Month]
Error: "A dimension modifier using the iterating dimension Month can't be applied to a metric in the iterative calculation."
FILLFORWARD is non-iterative and much faster. Use it when you only need to carry the last known value forward with no calculation at each step.
// BAD — iterative carry-forward
IFBLANK('Status Input', PREVIOUSOF('Current Status'))
// GOOD — non-iterative, 10-50x faster
FILLFORWARD('Status Input', Month)
Use PREVIOUS/PREVIOUSOF only when each step involves a computation (balance + inflows - outflows, conditional reorder logic, etc.).
Iterative calculations are inherently sequential (period N depends on 1..N-1). Cost drivers:
IFDEFINED(M, M, PREVIOUS(Month)) over IF(ISBLANK(M), PREVIOUS(Month), M).PREVIOUS(Month) * (A + B) is faster than PREVIOUS(Month) * A + PREVIOUS(Month) * B.| Granularity | Periods/year | 5-year horizon |
|---|---|---|
| Monthly | 12 | 60 (fast) |
| Weekly | 52 | 260 (moderate) |
| Daily | 365 | 1,825 (slow) |
Some patterns require iteration:
Optimize what you can (subsetting, dimensionality, granularity) and accept the remaining performance cost.
What SKILL.md sets; muted values are Claude Code's defaults
39 skills in this plugin
/aggregating-view-dataExecution skill. Use when a View needs totals or subtotals, when a value must roll up with something other than Sum (average, min, max, count, last), when a snapshot metric adds up across time instead of holding, or when a ratio, percentage, margin, growth or variance metric is added to a Table View or shows a wrong total (e.g. a row for margin as a percent of revenue). Covers metric default aggregators, pivot versus hidden-dimension aggregation, the aggregator reference per value type, and Advanced Aggregators on Table Views./architecting-multi-application-solutionsPlanning skill. Use when deciding how to split work across Pigment applications, designing Hub-and-spoke data flows, or planning Test & Deploy across environments./auditing-a-pigment-applicationExecution skill. Use when auditing, reviewing, or health-checking a Pigment application. Covers the structured audit checklist, severity classification, and output format. Also triggers on requests to clean up, find unused blocks, assess model quality, or find what is wrong with an application./building-a-full-applicationPlanning skill. Use when building a complete Pigment application from scratch or when the request spans multiple modeling phases. This skill, together with any other planning skill the request calls for, is sufficient to produce the full architecture spec; do not load execution skills until the spec is approved and you begin building blocks./building-pigment-framesUse this skill whenever you're building, updating, or troubleshooting a Pigment Frame — a full-page, custom-coded visualisation (JavaScript, canvas, or inline SVG) that reads Pigment data through the PigmentSDK. Reach for it any time a native View (Grid, Chart, KPI) or Board can't produce what's being asked for - a bespoke chart, a custom canvas drawing, an interactive widget, or a standalone page in the left navigation that behaves like a small app. Also use it if a Frame you've already built shows up blank or broken and needs debugging./building-versions-and-planning-cyclesExecution skill. Use when implementing versions, planning cycles, or actuals vs forecast separation, and whenever realized data is extended into a plan or budget — loading actuals then planning the remaining periods, forecasting forward from actuals, Actual/Budget/Plan layering, plan vs actual variance — even when the request never uses the word version. Covers Version Dimensions, Switchover patterns, Actual/Forecast layering, Native Scenarios, and Snapshots.npx claudepluginhub gopigment/ai-plugins --plugin pigmentOther plugins' skills for the same job