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

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.

主站蜘蛛池模板: 内丘县| 香港 | 女性| 开鲁县| 金沙县| 汉中市| 阿克苏市| 瑞昌市| 白山市| 阿克陶县| 阿坝县| 涟源市| 确山县| 红安县| 昭觉县| 武冈市| 汤原县| 峨山| 白水县| 广元市| 双牌县| 铜陵市| 绥中县| 武定县| 固阳县| 昂仁县| 新巴尔虎右旗| 桦甸市| 绩溪县| 山东省| 平凉市| 社会| 蓬安县| 昌图县| 通城县| 志丹县| 塔城市| 景德镇市| 政和县| 琼海市| 西盟|