Skip to main content Skip to complementary content

Derive

The Derive statement is used to generate derived fields based on a field definition created with a Declare statement. You can either specify which data fields to derive fields for, or derive them explicitly or implicitly based on field tags.

Syntax:

Derive [Field[s]] From [Field[s]] field_list Using definition

Derive [Field[s]] From Explicit [Tag[s]] tag_list Using definition

Derive [Field[s]] From Implicit [Tag[s]] Using definition

Arguments:

Argument Description
definition

Name of the field definition to use when deriving fields.

Example: Calendar

field_list

A comma separated list of data fields from which the derived fields should be generated, based on the field definition. The data fields should be fields you have already loaded in the script.

Example: OrderDate, ShippingDate

tag_list

A comma separated list of tags. Derived fields will be generated for all data fields with any of the listed tags.

Example: '$date'

See: Field tags

Example:

In this example we create a calendar definition that can be used to generate date related fields, such as year and month, that can be used in dimensions.

To try out the calendar we need some source data with date fields.

Load * Inline [ OrderID, Terminal, OrderDate, ShippingDate 100001, London, 2012-03-12, 2012-03-14 100002, Berlin, 2012-03-12, 2012-03-16 100003, London, 2012-03-12, 2012-03-14 100004, Paris, 2012-03-12, 2012-03-16 100005, Paris, 2012-03-12, 2012-03-14 100006, Paris, 2012-04-11, 2012-04-22 100007, London, 2012-04-12, 2012-04-14 100008, Paris, 2012-05-12, 2012-05-13 100009, London, 2013-01-12, 2013-03-14 100010, Berlin, 2013-01-12, 2013-03-16 100011, London, 2013-01-12, 2013-03-14 100012, Paris, 2013-01-12, 2013-03-16 100013, Paris, 2013-01-12, 2013-03-14 100014, Berlin, 2013-03-12, 2013-03-16 100015, London, 2013-03-12, 2013-03-14 100016, Berlin, 2013-03-12, 2013-03-16 100017, Paris, 2013-09-12, 2013-10-14 100018, Berlin, 2013-09-12, 2013-09-16 ];
 

Now we can define a calendar that creates fields for year, month, week, weekday and day number.

Calendar: DECLARE FIELD DEFINITION TAGGED '$date' Parameters first_month_of_year = 1 Fields Year($1) As Year Tagged '$year', Month($1) as Month Tagged '$month', Date($1) as Date Tagged ('$date', '$day'), Week($1) as Week Tagged '$week', Weekday($1) as Weekday Tagged '$weekday', DayNumberOfYear($1, first_month_of_year) as DayNumberOfYear Tagged ('$numeric');
 

The calendar is now defined, and you can apply it to the date fields that have been loaded, in this case OrderDate and ShippingDate, using a Derive clause. There are three alternative ways of doing this.

  • Derive fields for specific data fields.

    In this case we specify the OrderDate and ShippingDate fields.

    DERIVE FIELDS FROM FIELDS OrderDate,ShippingDate USING Calendar;
  • Derive fields for all fields with a specific tag.

    In this case we derive fields based on Calendar for all fields with a $date tag.

    DERIVE FIELDS FROM EXPLICIT TAGS '$date' USING Calendar;
  • Derive fields for all fields with the field definition tag.

    In this case we derive fields for all data fields with the same tag as the Calendar field definition, which in this case is $date.

    DERIVE FIELDS FROM IMPLICIT TAG USING Calendar;

When you have reloaded the data script, you can find the generated fields, for example OrderDate.Calendar.Weekday under Date & time fields in the Fields asset panel. They are also available in the expression editor and when you create dimensions, and can be used as any other field in visualizations.

Learn more

 

Did this page help you?

If you find any issues with this page or its content – a typo, a missing step, or a technical error – let us know how we can improve!