---
sourceDocument: Xanadu API Reference
sourceDocumentLink: https://servicenow-prod.fluidtopics.net/r/xanadu/api-reference

 Release :

    - xanadu

ft:locale :

    - en-US

ft:publication_title :

    - Xanadu API Reference

ft:clusterId :

    - crapiref

bundleId :

    - crapiref

workflow :

    - Creator


---

# Transformer - Scoped, Global

# Transformer - Scoped, Global {#ariaid-title1}

* Release version: Xanadu
* 
* Updated August 1, 2024
* 
* ![](https://www.servicenow.com/docs/portal-asset/ico-clock) 2 minutes to read

The Transformer API manipulates time-series data to prepare the data for evaluation and analysis.

You can call this API in scoped and global server scripts. When using the Transformer class, use the `sn_clotho` namespace identifier.  
The general use case is to determine the period to be evaluated, select the records from the table with the metric field, define the type of transform to run, and then execute the transform.

    // create the start and end time
           var start = new GlideDateTime();
    	start.addSeconds(-1 * 60 * 60); 
    	var end = new GlideDateTime();
    	
    	//mb_demo_drone is a table with metric fields. 
    	var drones = new GlideRecord("mb_demo_drone");
    	drones.addQuery("model", "Kingfisher Phantom");
    	drones.query();

    	//build a transform that returns a simple average
    	var builder = new sn_clotho.Transformer(drones);
    	builder.metric("mb_demo_mt_rem_battery").avg().label("Original");

    	//execute transform and return result for visualization
    	var result = builder.execute(start,end);

This class is part of the MetricBase application.

## Transformer - Transformer( GlideRecord sourceRecords) {#ariaid-title2}

Create a Transformer object.
{#TFMR-transformer_GR__table_w4x_4qr_mbb__entry__3}

| Name | Type | Description |
|-|-|-|
| sourceRecords | GlideRecord | Contains the records for which metrics are to be evaluated. Can be one record or many. |
[Table 1. Parameters]

{#TFMR-transformer_GR__table_w4x_4qr_mbb}  

    //where drones is a GlideRecord created from a table with a metric field
    	var builder = new sn_clotho.Transformer(drones);

## Transformer - execute(GlideDateTime start, GlideDateTime end) {#ariaid-title3}

Run the transform.
Use the metric() and groupBy() methods before calling
execute(). The execute() method can only be called
once for each Transformer object.

Actions performed as part of the transform do not change the data in the MetricBase database.
{#TFRM-execute_GDT_GDT__table_b2l_4xr_mbb__entry__3}

| Name | Type | Description |
|-|-|-|
| start | GlideDateTime | The beginning of the period to be evaluated. |
| end | GlideDateTime | The end of the period to be evaluated. |
[Table 2. Parameters]

{#TFRM-execute_GDT_GDT__table_b2l_4xr_mbb} {#TFRM-execute_GDT_GDT__table_c2l_4xr_mbb__entry__2}

| Type | Description |
|-|-|
| TransformResult | The transformed data. |
[Table 3. Returns]

{#TFRM-execute_GDT_GDT__table_c2l_4xr_mbb}  

    var minutesAgoStart = 60;
    	var end = new GlideDateTime();
    	var start = new GlideDateTime(end);
    	start.addSeconds(-1 * 60 * minutesAgoStart);
    	
    	// query subject records
    	var grDrone = new GlideRecord('mb_demo_drone');
    	grDrone.query();
    	
    	// building transform; get the average transforms of a metric, grouping by model
    	var transformer = new sn_clotho.Transformer(grDrone);
    	transformer.groupBy("fleet").metric("mb_demo_mt_altitude").avg().label('avg - %g:fleet:');

    	// execute and return result for visualization
    	var tfrmResult = transformer.execute(start, end);

## Transformer - groupBy(String field) {#ariaid-title4}

Specifies the field to use to group the data.
If you are going to use the groupBy() method, you must call it must before calling the execute() method.
{#TFMR-groupBy_S__table_brg_q5r_mbb__entry__3}

| Name | Type | Description |
|-|-|-|
| field | String | The field in the table to use to group the transform results. |
[Table 4. Parameters]

{#TFMR-groupBy_S__table_brg_q5r_mbb} {#TFMR-groupBy_S__table_crg_q5r_mbb__entry__2}

| Type | Description |
|-|-|
| TransformPart | A TransformPart object that you can use to specify the transform characteristics. |
[Table 5. Returns]

{#TFMR-groupBy_S__table_crg_q5r_mbb}  

    var transformer = new sn_clotho.Transformer(grDrone);
    var trnsfrm = transformer.groupBy("fleet");

## Transformer - metric(String metricName) {#ariaid-title5}

Specify the metric field to be used in the transform.
You can specify multiple metrics to be used in the transform. The
metric() method cannot be called after the execute()
method is called.
{#TFRM-metric_S__table_g3m_yvr_mbb__entry__3}

| Name | Type | Description |
|-|-|-|
| metricName | String | Name of the metric field. |
[Table 6. Parameters]

{#TFRM-metric_S__table_g3m_yvr_mbb} {#TFRM-metric_S__table_h3m_yvr_mbb__entry__2}

| Type | Description |
|-|-|
| TransformPart | A TransformPart object that can be used to specify the transform characteristics. |
[Table 7. Returns]

{#TFRM-metric_S__table_h3m_yvr_mbb}  

    var transformer = new sn_clotho.Transformer(grDrone);
    var trnsfrm = transformer.metric("mb_demo_mt_altitude");


