How does the QlikSense AutoCalendar function work?

Chris J picture Chris J · Jun 9, 2016 · Viewed 11.3k times · Source

Taking apart the auto-generated code in one of my Sense Apps, I have come across the Auto Calendar function which is great and I have amended as below but I have no idea how it works! Can someone provide some documentation?

[autoCalendar]: 
  DECLARE FIELD DEFINITION Tagged ('$date')
FIELDS
  Dual(Year($1), YearStart($1)) AS [Year] Tagged ('$axis', '$year')
  ,Dual('Q'&Num(Ceil(Num(Month($1))/3)),Num(Ceil(NUM(Month($1))/3),00)) AS [Quarter] Tagged ('$quarter')
  ,Dual(Year($1)&'-Q'&Num(Ceil(Num(Month($1))/3)),QuarterStart($1)) AS [YearQuarter] Tagged ('$axis', '$yearquarter')
  ,Month($1) AS [Month] Tagged ('$month')
  ,Dual(Year($1)&'-'&Month($1), monthstart($1)) AS [YearMonth] Tagged ('$axis', '$yearmonth')
  ,Dual('W'&Num(Week($1),00), Num(Week($1),00)) AS [Week] Tagged ('$weeknumber')
  ,Date(Floor($1)) AS [Date] Tagged ('$date')
  /*User added date components*/
  ,Dual(Year($1), if(Year($1)=Year(today()),YearStart($1),null)) AS [ThisYear] Tagged ('$axis', '$thisyear')
  ,Dual(Year($1)&'-Q'&Num(Ceil(Num(Month($1))/3)), if(Year($1)=Year(today()),QuarterStart($1),null)) AS [ThisYearQuarter] Tagged ('$axis', '$thisyearquarter')
  ,Dual(Year($1)&'-'&Month($1), if(Year($1)=Year(today()), monthstart($1),null)) AS [ThisYearMonth] Tagged ('$axis', '$thisyearmonth')
  ,Dual(Year($1), if(Year($1)=(Year(today())-1),YearStart($1),null)) AS [LastYear] Tagged ('$axis', '$lastyear')
  ,Dual(Year($1)&'-Q'&Num(Ceil(Num(Month($1))/3)), if(Year($1)=(Year(today())-1),QuarterStart($1),null)) AS [LastYearQuarter] Tagged ('$axis', '$lastyearquarter')
  ,Dual(Year($1)&'-'&Month($1), if(Year($1)=(Year(today())-1), monthstart($1),null)) AS [LastYearMonth] Tagged ('$axis', '$lastyearmonth')
 ;


DERIVE FIELDS FROM FIELDS [StartDate] USING [autoCalendar] ;

Does this iterate through every entry or does it iterate through every day in the calendar? It looks to me like the former - can it be used for non-date functions?

Answer

Stefan Stoichev picture Stefan Stoichev · Jun 9, 2016

Please have a look at Qlik help about Derived fields

http://help.qlik.com/en-US/sense/2.2/Subsystems/Hub/Content/Scripting/derived-fields.htm

The details from the page are as bekiw

Derived fields

If you have a group of fields that are related, or if fields carry information that can be broken up into smaller parts that are relevant when creating dimensions or measures, you can create field definitions that can be used to generate derived fields. One example is a date field, from which you can derive several attributes, such as year, month, week number, or day name. All these attributes can be calculated in a dimension expression using Qlik Sense date functions, but an alternative is to create a calendar definition that is common for all fields of date type. Field definitions are stored in the data load script.

Declare the calendar field definitions

You use the Declare statement to create a definition of the derived fields. This is where you define the different attributes of the field, in this case date related attributes. Each field is described as <expression> As field_name tagged tag. Setting one or more tags is optional, but it can affect the sort order of the derived field. Use $1 to reference the data field from which the derived fields should be generated.

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

See: Declare

Map data fields to the calendar with Derive

The next step is to use the Derive statement to map existing data fields to the calendar. This will create the derived fields. You can do this in three alternative ways in the data load script:

Map specific fields by field name.

DERIVE FIELDS FROM FIELDS OrderDate,ShippingDate USING Calendar;

Map all fields with one or more specific field tags.

DERIVE FIELDS FROM EXPLICIT TAGS '$date' USING Calendar;

Map all fields that are tagged with one of the tags of the field definition ($date in the example above).

DERIVE FIELDS FROM IMPLICIT TAG USING Calendar;

In this case, you could use any of the three examples here.

See: Derive

Use the derived date fields in a visualization

Qlik Sense is prepared to recognize derived date fields if you have created a calendar definition and mapped the fields like in the example here. They are available in the Date & time fields section of the Fields asset panel. You will also find all derived fields in the expression editor and when you create or edit dimensions.