- 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.
- 極簡算法史:從數學到機器的故事
- HTML5+CSS3+JavaScript從入門到精通:上冊(微課精編版·第2版)
- OpenStack Cloud Computing Cookbook(Third Edition)
- HTML5移動Web開發技術
- Game Programming Using Qt Beginner's Guide
- NativeScript for Angular Mobile Development
- Julia機器學習核心編程:人人可用的高性能科學計算
- Practical Game Design
- 零基礎學MQL:基于EA的自動化交易編程
- AutoCAD VBA參數化繪圖程序開發與實戰編碼
- Apache Spark 2.x for Java Developers
- Scala Data Analysis Cookbook
- HTML5+CSS3+JavaScript 從入門到項目實踐(超值版)
- 計算機組裝與維護(第二版)
- Java程序設計