官术网_书友最值得收藏!

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:

  1. 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])
  2. 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:
    Time for action – using time intelligence functions in DAX
  3. 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.
  4. 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:
    Time for action – using time intelligence functions in DAX
  5. 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])
  6. 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.
  7. 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")
  8. 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])))
  9. Review the results in Excel as shown in the following screenshot:
    Time for action – using time intelligence functions in DAX

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.

主站蜘蛛池模板: 宽甸| 龙游县| 浑源县| 博野县| 萝北县| 句容市| 湘阴县| 沙河市| 西贡区| 荔波县| 镇江市| 苍南县| 巴林左旗| 台州市| 玛纳斯县| 玛纳斯县| 乐昌市| 敦煌市| 彝良县| 莲花县| 中宁县| 顺昌县| 克拉玛依市| 共和县| 上杭县| 广安市| 根河市| 鹤壁市| 长子县| 瑞安市| 都匀市| 乌苏市| 刚察县| 七台河市| 汕尾市| 岚皋县| 德清县| 乳山市| 手游| 丰县| 长乐市|