changed

changed, process

def process changed(value: any): boolean

Returns true if the current value is different from the previous value; always returns true for the first element. This process function must be used with either scan or sort ordering option.

Examples

Basic usage with sorting

table T = with
  [| as Id, as Category |]
  [| 1,    "A"          |]
  [| 2,    "A"          |]
  [| 3,    "B"          |]
  [| 4,    "B"          |]
  [| 5,    "A"          |]

T.HasChanged = changed(T.Category) scan T.Id

show table "Category changes" with
  T.Id
  T.Category
  T.HasChanged

This produces:

Id Category HasChanged
1 A True
2 A False
3 B True
4 B False
5 A True

Detecting day changes in a date series

table Sales = with
  [| as Date,            as Amount |]
  [| date(2023, 1, 1),   100       |]
  [| date(2023, 1, 1),   150       |]
  [| date(2023, 1, 2),   200       |]
  [| date(2023, 1, 2),   250       |]
  [| date(2023, 1, 3),   300       |]

Sales.NewDay = changed(Sales.Date) scan Sales.Date

show table "Daily sales breakdown" with
  Sales.Date
  Sales.Amount
  Sales.NewDay

This produces the following table of daily sales:

Date Amount NewDay
Jan 1, 2023 100 True
Jan 1, 2023 150 False
Jan 2, 2023 200 True
Jan 2, 2023 250 False
Jan 3, 2023 300 True

Detecting stock level changes by product

table StockLevels = with
  [| as Date,           as Product,   as Stock |]
  [| date(2023, 1, 1),  "Widget A",   120      |]
  [| date(2023, 1, 2),  "Widget A",   85       |]
  [| date(2023, 1, 3),  "Widget A",   85       |]
  [| date(2023, 1, 1),  "Widget B",   50       |]
  [| date(2023, 1, 2),  "Widget B",   50       |]
  [| date(2023, 1, 3),  "Widget B",   75       |]

StockLevels.StockChanged = changed(StockLevels.Stock)
  by StockLevels.Product
  sort StockLevels.Date

show table "Stock Level Changes" with
  StockLevels.Date
  StockLevels.Product
  StockLevels.Stock
  StockLevels.StockChanged

This examples produces the following table of stock level changes:

Date Product Stock StockChanged
Jan 1, 2023 Widget A 120 True
Jan 2, 2023 Widget A 85 True
Jan 3, 2023 Widget A 85 False
Jan 1, 2023 Widget B 50 True
Jan 2, 2023 Widget B 50 False
Jan 3, 2023 Widget B 75 True

Remarks

The changed process function requires a scan/sort option to specify the order in which values will be compared. Without a specified order, your code will not compile.

This process can be used with any data type that supports equality comparison, including numbers, text, dates, and booleans.

Recipes and best practices

  1. Segmenting data by changes: Use changed to identify natural boundaries in sorted data:
    Dates.NewMonth = changed(month(Dates.Date)) scan Dates.Date
  2. Counting transitions: Combine with sum to count how many times a value changes:
    categoryChanges = sum(if changed(Items.Category) scan Items.Id then 1 else 0)
  3. Track delivery status changes for notifications: Identify when a package’s delivery status changes (e.g., to trigger customer notifications):
    DeliveryLog.StatusChanged = changed(DeliveryLog.Status) by DeliveryLog.TrackingNumber scan DeliveryLog.Timestamp

See also

User Contributed Notes
0 notes + add a note