In some situations, you might like to change the way Looker formats dates.
For example, if you're building Explores and dashboards for users in Europe, any dates in all-numeric format should appear in the order Day-Month-Year. June 7th, 2019 should be formatted as 07/06/2019
(or Jun 07, 2019
), but Looker's default format would render it as 2019-06-07
. Of course, making such a change is possible using SQL functions like DATE_FORMAT()
, but you might not want to write everything in plain SQL.
Liquid to the Rescue
We need only apply some Liquid formatting on the html
parameter of a dimension to easily change the date format. Since Liquid is expecting a valid date, it is best to use the timeframes from an original dimension group.
For example: ${created_date}
, ${created_week}
, and so on:
dimension: date_formatted {
sql: ${created_date} ;;
html: {{ rendered_value | date: "%b %d, %y" }} ;;
}
The formatting string "%b %d, %y"
is in the syntax of strftime
.
Because we are using Liquid formatting on the html
parameter, we are not changing the original value, only the way it is rendered to the user. This ensures that the ordering does not change.
Below are some other examples you can use. The full list of available formats is further down.
Preset | Example |
%m/%d/%Y
|
06/05/2013 |
%A, %B %e, %Y
|
Wednesday, June 5, 2013 |
%b %e %a
|
Jun 5 Wed |
Customizing Date Formats Even Further
With these Liquid formats, we can pretty much change the format of anything, as long as it's a valid date format. (We can't change the week or month numbers, because those are returned as integers.) In the example below, we are formatting day, week, and month differently, and we add the created group_label
to simulate a date dimension group.
Result
LookML
# My original dimension group
dimension_group: created {
hidden: yes
type: time
timeframes: [date,week,month]
sql: ${TABLE}.created_at ;;
}
# My customized timeframes, added under the group "Created"
dimension: date_formatted {
group_label: "Created" label: "Date"
sql: ${created_date} ;;
html: {{ rendered_value | date: "%b %d, %y" }};;
}
dimension: week_formatted {
group_label: "Created" label: "Week"
sql: ${created_week} ;;
html: {{ rendered_value | date: "Week %U (%b %d)" }};;
}
dimension: month_formatted {
group_label: "Created" label: "Month"
sql: ${created_month} ;;
html: {{ rendered_value | append: "-01" | date: "%B %Y" }};;
}
strftime Reference
specifier | Replaced by | Example |
%a | Abbreviated weekday name * | Thu |
%A | Full weekday name * | Thursday |
%b | Abbreviated month name * | Aug |
%B | Full month name * | August |
%c | Date and time representation * | Thu Aug 23 14:55:02 2001 |
%C | Year divided by 100 and truncated to integer (00-99) | 20 |
%d | Day of the month, zero-padded (01-31) | 23 |
%D | Short MM/DD/YY date, equivalent to %m/%d/%y | 08/23/01 |
%e | Day of the month, space-padded ( 1-31) | 23 |
%F | Short YYYY-MM-DD date, equivalent to %Y-%m-%d | 2001-08-23 |
%g | Week-based year, last two digits (00-99) | 1 |
%G | Week-based year | 2001 |
%h | Abbreviated month name * (same as %b) | Aug |
%H | Hour in 24h format (00-23) | 14 |
%I | Hour in 12h format (01-12) | 02 |
%j | Day of the year (001-366) | 235 |
%k | Hour in 24h format, single digits are proceeded by a blank space (0-23) | 14 |
%l | Hour in 12h format, single digits are proceeded by a blank space (0-12) | 2 |
%m | Month as a decimal number (01-12) | 8 |
%M | Minute (00-59) | 55 |
%n | New-line character ('\n') | |
%P | am or pm designation in lowercase | pm |
%r | 12-hour clock time * | 2:55:02 PM |
%R | 24-hour HH:MM time, equivalent to %H:%M | 14:55 |
%s | Number of seconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC) | 1566236649 |
%S | Second (00-61) | 2 |
%t | Horizontal-tab character ('\t') | |
%T | ISO 8601 time format (HH:MM:SS), equivalent to %H:%M:%S | 14:55:02 |
%u | ISO 8601 weekday as number with Monday as 1 (1-7) | 4 |
%U | Week number with the first Sunday as the first day of week one (00-53) | 33 |
%V | ISO 8601 week number (01-53) | 34 |
%w | Weekday as a decimal number with Sunday as 0 (0-6) | 4 |
%W | Week number with the first Monday as the first day of week one (00-53) | 34 |
%x | Date representation * | 08/23/01 |
%X | Time representation * | 14:55:02 |
%y | Year, last two digits (00-99) | 1 |
%Y | Year | 2001 |
%z | ISO 8601 offset from UTC in timezone (1 minute=1, 1 hour=100)If timezone cannot be determined, no characters | 100 |
%Z | Timezone name or abbreviation *If timezone cannot be determined, no characters | CDT |
%% | A % sign | % |