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

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.

主站蜘蛛池模板: 太和县| 东海县| 苏州市| 宜州市| 安图县| 邯郸市| 安多县| 建阳市| 资中县| 信宜市| 搜索| 茌平县| 云南省| 神木县| 抚顺市| 宁明县| 当阳市| 镇康县| 伊吾县| 沽源县| 泽库县| 航空| 沁水县| 唐海县| 焦作市| 长沙市| 突泉县| 西青区| 聂荣县| 盐源县| 定远县| 正镶白旗| 前郭尔| 县级市| 海口市| 鹿泉市| 盐源县| 肇源县| 墨江| 阜南县| 太白县|