SQLServerCentral Article

Power BI Report Builder Expressions - Date Functions

,

Using Power BI Report Builder expressions is an advanced topic that requires a little programming knowledge. You can use if clauses, cases, functions, etc.

In this tutorial, we will introduce the use of Expressions in Power BI Report Builder. We will show especially the Date functions.

Requirements for Power BI Report Builder expressions

  1. First of all, you need Power BI Report Builder.
  2. Secondly, the internet to follow this tutorial.

Getting started with Power BI Report Builder expressions

First, we will work with some basic dates. In a report, in the title right-click and select Expressions.

Power BI Report Builder expressions

The first example will be a Hello world.

=”Hello world”

 First expression

Secondly, run the report to check the results.

Power BI Report Builder expressions - Run report

Also, you will be able to see the results.

Power BI Report Builder expressions - Hello world

Finally, you can check some simple examples.

Expression

Result

Explanation

=”Hello world”Hello worldFirst, we have a simple string example that shows a hello world.
=“Hello”+space(1)+”world”Hello worldSecondly, we are showing how to use a space. It is a good practice to use the space function instead of a simple space with quotes because the SPACE function is more visible.
=“Hello”+  vbcrlf +”world”Hello

world

Finally, we are showing how to display the second line using the vbcrlf. In this example, the word world is in the second line.

Functions in Power BI Report Builder Expressions

In order to check the date functions, go to Common Functions>Date and time. You can see a description and examples here.

Globals!Executetime expression

Let’s take a look to some examples. These examples were run on 10 Mar 2022.

Expression

Result

Explanation

=TODAY()5/10/2022 12:00:00 AMFirst, we have this function that shows the current date.
=NOW()5/10/2022 5:52:33 AMSecondly, the now function shows the current date and time.
=YEAR(TODAY())2022Thirdly, the year function shows the year of a specified date.
=Month(TODAY())5Also, the month function shows the month of a date.
=DAY(TODAY())10In addition, the day function will show the day of a specified date.
=HOUR(NOW())17Another function is the hour which shows the hour of a time value.
=MINUTE(NOW())52Also, we have the minute function to get the minutes from a time value.
=”This is the current date ”+CStr(TODAY())This is the current date 5/10/2022In this example, we are concatenating a string with a date. We use the function CString to convert from date to string.
=”This is the current date ”+DateString ()This is the current date 05/10/2022In addition, DateString converts the date into a string. In this example, we are concatenating a string with a date.
=Cdate(“5-2-20”)5/02/2020 12:00:00 AMCdate converts the data into a date.
=Globals!ExecutionTime5/10/2022 5:52:33 AMFinally, Globals!ExecutionTime displays the date and time of the Report execution. .

The FormatDateTime function used as a Power BI Report Builder expression

The formatDateTime function has several options. We will explain each of them.

Expression

Result

Explanation

=FormatDateTime(Now, DateFormat.ShortDate5/10/2022First, we have a short date format.
=FormatDateTime(Now, DateFormat.LongDate)Tuesday, May 10, 2022Secondly, we have the long date format.
=FormatDateTime(Now, DateFormat.GeneralDate)5/10/2022 5:52:33 AMThirdly, we have the general date format which includes the time.
=FormatDateTime(Now, DateFormat.LongTime)5:52:33 AMAlso, we can specify just the time in a long format.
=FormatDateTime(Now, DateFormat.ShortTime)5:52Finally, the function can provide a short time format.

Day of week function in Power BI Report Builder expression

Expression

Result

Explanation

=WeekdayName(1,False)SundayFirst, we will show the WeekdayName number 1 which is Monday.
=WeekdayName(2,False)MondayThe second-week day's name is Monday.
=WeekdayName(3,False)TuesdayAlso, the third day is Tuesday and so on.
=WeekdayName(2,True)MonIn addition, the second parameter shows if it is abbreviated (True) or not (false).
=WeekdayName(2,True,3)WedFinally, the third parameter is the first day of the week. 1 is Sunday, 2 is Monday, and so on. The default is 0.

MonthName function in Power BI Report Builder Expressions

In addition, we have the MonthName function

Expression

Result

Explanation

=MonthName(Month(Cdate("8/10/2022")),False))AugustFirst, we have the MonthName function that returns the name of a month. This function requires the month number.
=MonthName(Month(Cdate("8/10/2022")),True)AugSecondly, this example shows the month name in abbreviated format.
=UCase(MonthName(Month(Cdate("8/10/2022")),True))AUGThirdly, this example shows the month name uppercased.
=LCase(MonthName(Month(Cdate("8/10/2022")),True))augFinally, this example shows the month name lowercased.

Datediff and Dateadd

Finally, we will show some examples of the Datediff and DateAdd functions.

Expression

Result

Explanation

=DateAdd("d",3,"5/10/2022")5/13/2020 12:00:00 AMFirst, we have the DateAdd function. In this example, we add 3 days to the 5/10/2022 date.
=DateAdd("m",3,"5/10/2022")8/10/2020 12:00:00 AMSecondly, we add 3 months to the 5/10/2022 date.
=DateAdd("yyyy",3,"5/10/2022")5/10/2025 12:00:00 AMThirdly we add 3 years to the 5/10/2022 date.
=DateAdd("q",3,"5/10/2022")2/10/2023 12:00:00 AMAlso, we add 3 quarters to the 5/10/2022 date.
=DateDiff("h","5:10:22","3:10:22")-2In addition, we have the DateDiff. This example shows the difference in hours between 2 times.
=DateDiff("n","5:10:22","5:08:22")-2The next example, shows the difference in minutes between 2 times.
=DateDiff("s","5:10:22","5:10:32")10Finally, we have the difference in seconds between 2 times.

Conclusion about the Power BI Report Builder Expressions

In this article, we learn the main Power BI Report Builder expressions. We focused on date functions, but we will have more examples in the next articles.

Rate

5 (1)

You rated this post out of 5. Change rating

Share

Share

Rate

5 (1)

You rated this post out of 5. Change rating