Skip to main content Skip to complementary content

Declare

The Declare statement is used to create field and group definitions, where you can define relations between fields or functions. A set of field definitions can be used to automatically generate derived fields, which can be used as dimensions. For example, you can create a calendar definition, and use that to generate related dimensions, such as year, month, week and day, from a date field.

You can use Declare to either set up a new field definition, or to create a field definition based on an already existing definition.

Setting up a new field definition

Syntax:

definition_name:

Declare [Field[s]] Definition [Tagged tag_list ]

[Parameters parameter_list ]

Fields field_list 

Arguments:

Argument Description
definition_name

Name of the field definition, ended with a colon.

Example:  

Calendar:

tag_list

A comma separated list of tags to apply to fields derived from the field definition. Optional.

Example:  

'$date'

See: Field tags

parameter_list

A comma separated list of parameters. A parameter is defined in the form name=value and is assigned a start value, which can be overridden when a field definition is re-used. Optional.

Example:  

first_month_of_year = 1

field_list

A comma separated list of fields to generate when the field definition is used. A field is defined in the form <expression> As field_name tagged tag. Use $1 to reference the data field from which the derived fields should be generated.

Example:  

Year($1) As Year tagged '$year'

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.

DERIVE FIELDS FROM FIELDS OrderDate,ShippingDate 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.

Re-using an existing field definition

Syntax:

<definition name>:

Declare [Field][s] Definition

Using <existing_definition> 

[With <parameter_assignment> ]

Arguments:

Argument Description
definition_name

Name of the field definition, ended with a colon.

Example:  

MyCalendar:

existing_definition

The field definition to re-use when creating the new field definition. The new field definition will function the same way as the definition it is based on, with the exception if you use parameter_assignment to change a value used in the field expressions.

Example:  

Using Calendar

parameter_assignment

A comma separated list of parameter assignments. A parameter assignment is defined in the form name=value and overrides the parameter value that is set in the base field definition. Optional.

Example:  

first_month_of_year = 4

Example:

In this example we re-use the calendar definition that was created in the previous example. In this case we want to use a fiscal year that starts in April. This is achieved by assigning the value 4 to the first_month_of_year parameter, which will affect the DayNumberOfYear field that is defined.

The example assumes that you use the sample data and field definition from the previous example.

MyCalendar: DECLARE FIELD DEFINITION USING Calendar WITH first_month_of_year=4;
 
DERIVE FIELDS FROM FIELDS OrderDate,ShippingDate USING MyCalendar;
 

When you have reloaded the data script, the generated fields are available in the sheet editor, with names OrderDate.MyCalendar.* and ShippingDate.MyCalendar.*.

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!