Transforms: clean and reshape data (Power Query-style)

Transform steps are plain objects applied in order to a table's rows. They're stored with the model, so they replay whenever the data is refreshed or appended to, and the same steps run in the browser, a Web Worker or on a server.

model.transformTable('Sales', [
  { op: 'removeDuplicates' },
  { op: 'transformText', columns: ['Customer'], fn: 'clean' },
  { op: 'changeType', column: 'Amount', type: 'number' },     // "1,200" → 1200, "n/a" → blank
  { op: 'filterRows', column: 'Region', test: 'notBlank' },
  { op: 'splitColumn', column: 'Order', delimiter: '-', into: ['Series', 'Number'] },
]);

In the Studio, open the Data view and press Transform on a table. You can add steps from a form, reorder or remove them, and watch a live preview of the first rows. The steps are saved in the Studio document and undo like any other edit.

Steps #

The Studio transform editor: steps applied to a table, with a preview

StepExample
filterRows{ op: 'filterRows', column: 'Amount', test: 'gt', value: 100 }. Tests: eq, neq, gt, gte, lt, lte, contains, notContains, startsWith, in, notIn, isBlank, notBlank
removeColumns, keepColumns{ op: 'keepColumns', columns: ['Date', 'Amount'] }
renameColumns{ op: 'renameColumns', names: { Cust: 'Customer' } }
changeTypenumber (strips , $ € £ %), string, date (ISO dates become local dates), boolean
replaceValues{ op: 'replaceValues', column: 'Region', find: null, replace: 'Unknown' }
transformTexttrim, clean (collapse spaces), upper, lower, title
splitColumn, mergeColumnssplit by a delimiter into named columns; merge with a separator
addColumn{ op: 'addColumn', name: 'Label', template: '{First} {Last}' }
fillDownfill blanks with the value above
unpivot, pivotcolumns to attribute/value rows and back (pivot aggregates: sum, count, first, min, max, avg)
groupBy{ op: 'groupBy', by: ['Region'], aggregations: [{ name: 'Orders', agg: 'count' }, { name: 'Units', column: 'Qty', agg: 'sum' }] }
sort, removeDuplicates, keepTopordering, de-duplication (whole row or chosen columns), first N rows
merge, appendjoin (left or inner) or stack another table of the model

How replay works #

Try it #

▶ Run in playground
import { applySteps } from '@prismbi/prismbi';

return applySteps(
  [
    { Customer: '  ana lopez ', Q1: 10, Q2: 12 },
    { Customer: 'ben ito', Q1: 5, Q2: null },
  ],
  [
    { op: 'transformText', columns: ['Customer'], fn: 'clean' },
    { op: 'transformText', columns: ['Customer'], fn: 'title' },
    { op: 'unpivot', columns: ['Q1', 'Q2'], attribute: 'Quarter', value: 'Units' },
  ],
);

On the docs site (npm run docs), ▶ Run in playground opens it in an editor next to its output.