- Microsoft SQL Server 2014 Business Intelligence Development Beginner’s Guide
- Reza Rad
- 639字
- 2021-08-13 17:55:28
Time for action – using time intelligence functions in DAX
In this example, we will create some measures such as year-to-date, quarter-to-date, fiscal year-to-date, and running total, in order to show you how to use DAX functions and create expressions as you wish. As part of this example, we will use the CALCULATE
function, which calculates an expression on a filter context. So, perform the following steps that show the use of time intelligence functions:
- Go to Grid View of the InternetSales table and add a new measure using the following expression, and change the format string of this measure to
Currency
:Sales Amount YTD:=TOTALYTD(SUM(InternetSales[SalesAmount]),'Date'[FullDateAlternateKey])
- Save the changes and analyze the model in Excel. Select Calendar under Date, and Sales Amount YTD and Sum of SalesAmount under measures. You will see that the new measure is shown but it is not correct (look at the next screenshot). The problem is that the YTD is not calculated correctly; it is because tabular needs to set a date table to time intelligence functions so that it works, as shown in the following screenshot:
- Go to Grid View of the Date table, and then go to the Table menu, and under Date, click on Mark as Date Table. In the Mark as Date Table dialog box, choose FullDateAlternateKey column.
- Save the changes and refresh Excel PivotTable; now, you will see the correct YTD shown under the Sales Amount YTD measure, as shown in the following screenshot:
- Go back to Grid View of the InternetSales table and add a new measure for Sales Amount QTD, and change the format string of this measure to
Currency
:Sales Amount QTD:=TOTALQTD(SUM(InternetSales[SalesAmount]),'Date'[FullDateAlternateKey])
- View the model in Excel; you will see that QTD and YTD are shown correctly; QTD restarts at the first of each quarter and YTD restarts at the first of each year.
- Add another measure in InternetSales for YTD based on the fiscal year using the below expression:
Sales Amount YTD Fiscal:=TOTALYTD(SUM(InternetSales[SalesAmount]),'Date'[FullDateAlternateKey],"06-30")
- Add another measure in InternetSales for calculating Running Total based on the following expression:
Running Total:=CALCULATE(SUM(InternetSales[SalesAmount]),FILTER(ALL('Date'),'Date'[FullDateAlternateKey]<=MAX('Date'[FullDateAlternateKey])))
- Review the results in Excel as shown in the following screenshot:
What just happened?
You saw a DAX time intelligence expression used in this example. First and foremost, we need to set a Date table in order to use time intelligence functions. We set a Date table in step 3 and selected a full date column as a date column. Note that you cannot use a column with the YYYYMMDD format; it should be a full date type column. Another important note about the date column is that it should contain all the dates from the beginning of the period to the end date of the period; if you have one day missed, you will see incorrect results in the time intelligence functions.
The TotalYTD
function is used to calculate the YTD sales amount. Using this function is very simple; you just need to set the expression (SUM(InternetSales[SalesAmount]
in this example) and then set the date column (Date[FullDateAlternateKey]
), according to step 1. TotalQTD
is very similar to YTD
but with different usage.
TotalYTD
can be used for the fiscal year as well; the only thing you need to do is to pass the last day of the year as the third parameter. In this example (step 7), June 30 is passed as the last day of the year, which means that TotalYTD will restart at July 1 each year.
Calculate
is a function that works with this signature: Calculate
(expression, filter1, filter2, and so on). This function will calculate the expression based on the filter context as a result of filters. In this example, the Calculate
function is used to calculate the running total. There are many usages for the Calculate
function; you can refer to some MSDN articles for more information about this function at http://technet.microsoft.com/en-us/library/ee634825.aspx.
- Go Web編程
- Redis Applied Design Patterns
- Visual C++串口通信開發入門與編程實踐
- 趣學Python算法100例
- INSTANT Sencha Touch
- iOS應用逆向工程(第2版)
- Extending Unity with Editor Scripting
- Android系統下Java編程詳解
- Android移動應用開發項目教程
- Python Projects for Kids
- SQL Server 2014 Development Essentials
- Mastering JavaScript Promises
- Spring MVC Blueprints
- Getting Started with SpriteKit
- 活動中臺:揭秘vivo的千萬級DAU活動中臺