In my last post I showed lots of examples of how Power BI’s new custom format string feature can be used to format numbers. This post, looking at dates and times, will be a bit different for two reasons: there are a lot more useful examples of custom date and time formats built into Power BI Desktop, and some of the format placeholders listed in the VBA documentation aren’t supported in Power BI. As a result I’m going to concentrate on some useful formats that aren’t covered well by the examples and highlight a few things that aren’t possible right now.
Here’s the table containing the sample data, in the form of date/time values, for my examples:
[Note that the dates shown above are formatted in dd/mm/yyyy format]
The reason I’ve used date/time values for my examples is that they can be used to demonstrate formats for values of data type date and data type time, as well as data type date/time. As with my previous post I’m going to create a series of measures to show the effects of different format strings, each with the same DAX definition:
Eg1 = SELECTEDVALUE(Examples[DateAndTime])
With the default format of :
…applied, here’s what the output looks like in a Power BI report:
Let’s start by looking at date formats. The first thing to point out is that you can format a date/time so it only shows the date part and not the time and vice versa. For example, applying the custom format string:
…where dd is day number, mm is month number and yyyy is a four digit year, gives you:
If you’re American and want your months to come before your days you can simply swap the dd and the mm, for example with the format string:
You’re not forced to use a / as your separator in a date; in fact you can use any character. For example, the custom format:
If you don’t want a leading zero in front of your day or month number you can use a single d or m, and if you want a two-digit year you can use yy instead of yyyy. So, for example:
You can add full day names and month names using dddd and mmmm, so the format:
dddd dd mmmm yyyy
You can also get abbreviated day and month names using ddd and mmm, so:
ddd dd mmm yyyy
Last of all, for dates, if you want to make it clear that you’re not showing really, really, really old data you can put a g on the end of your date format like so:
…and you get the following:
Although the VBA documentation talks about showing day number of week, week number and quarter number, I haven’t found a way of making that work in Power BI (although I may have missed something and it may be possible).
Times are a bit more straightforward. The main placeholders are hh for hours, mm or nn for minutes and ss for seconds. So for example:
…both give you times formatted using the 24-hour clock as follows:
If you prefer to use the 12-hour clock you can add AM/PM onto the end of your format string, so:
The VBA documentation has several variants on AM/PM with slightly different outputs, but this is the only format that I could make work in Power BI.
The very last thing to mention is that, at the time of writing, although Power BI can store times with millisecond values there is no way to make milliseconds appear in a formatted time or date/time. For example in all the screenshots above there are two rows displaying 11/11/2019 15:15:15; they appear as different rows in the table because the millisecond values for each are different, but there’s no way of formatting these values to show that they are different. Hopefully this will be rectified soon; in the meantime you will need to store the millisecond part of any time or date/time separately in a different column in your dataset if you want to display it.
You can download the example .pbix file for this post here.