📜  Excel DAX-时间智能

📅  最后修改于: 2020-12-01 04:49:44             🧑  作者: Mango


DAX具有重要而强大的功能,称为时间智能。时间智能使您能够编写DAX公式,这些公式引用在数据透视表中使用的时间段。

DAX具有35种时间智能功能,专门用于随时间推移汇总和比较数据。但是,这些DAX函数对您需要理解和谨慎使用的数据有一些约束,以避免错误。

为什么时间智能使DAX功能强大?

时间智能功能可以处理不断变化的数据,具体取决于您在数据透视表和Power View可视化中选择的上下文。如您所知,大多数数据分析涉及时间段内的数据汇总,比较时间段内的数据值,了解趋势并根据未来的预测做出决策。

例如,您可能想要对过去一个月的产品总和进行总计,并将总和与该会计年度中其他月份的总和进行比较。这意味着,您必须使用日期作为一种方式来对特定时间段内的销售交易进行分组和汇总。

在这里您可以观察DAX的功能。您可以使用DAX时间智能功能定义计算字段,以帮助您分析随时间变化的数据,而无需更改数据透视表中的日期选择。这使您的工作更加轻松。此外,您可以构建其他任何方式都无法实现的数据透视表。

DAX时间智能功能的要求

DAX时间智能功能有一定要求。如果不满足这些要求,则可能会出错或无法正常工作。因此,您也可以将这些需求称为规则或约束。以下是某些DAX时间智能功能要求/规则/约束-

  • 您需要在数据模型中有一个日期表。

  • 日期表必须包含DAX视为日期列的列。您可以使用所需的方式来命名该列,但它应符合以下条件:o date列应包含一组连续的日期,这些日期应涵盖您正在分析数据的时间段中的每一天。

    • 每个日期在日期列中必须存在一次且只能存在一次。

    • 您不能跳过任何日期(例如,您不能跳过周末日期)。

  • DAX时间智能功能仅在标准日历上起作用,并假定年份的开始为1月1日,年份的结束为12月31日,其中年份中的月份和日历年中的每个月中的天。

但是,您可以自定义不同财政年度的标准日历。在任何时候使用智能函数之前,最好验证一下上述要求。

有关日期表及其在DAX公式中的用法的更多详细信息,请参阅本教程库中的教程=“使用DAX进行数据建模”。

DAX时间智能功能–类别

DAX时间智能功能可以分类如下-

  • 返回单个日期的DAX函数。
  • 返回日期表的DAX函数。
  • DAX函数,用于评估一段时间内的表达式。

返回单个日期的DAX函数

此类别中的DAX函数返回单个日期。

这个类别有10个DAX功能-

Sr.No. DAX Function & Return Value
1

FIRSTDATE (Date_Column)

Returns the first date in the Date_Column in the current context.

2

LASTDATE (Date_Column)

Returns the last date in the Date_Column in the current context.

3

FIRSTNONBLANK (Date_Column, Expression)

Returns the first date where an expression has a non-blank value.

4

LASTNONBLANK (Date_Column, Expression)

Returns the last date where an expression has a non-blank value.

5

STARTOFMONTH (Date_Column)

Returns the first date of a month in the current context.

6

ENDOFMONTH (Date_Column)

Returns the last date of a month in the current context.

7

STARTOFQUARTER (Date_Column)

Returns the first date of a quarter in the current context.

8

ENDOFQUARTER (Date_Column)

Returns the last date of a quarter in the current context.

9

STARTOFYEAR (Date_Column, [YE_Date])

Returns the first date of a year in the current context.

10

ENDOFYEAR (Date_Column, [YE_Date])

Returns the last date of a year in the current context.

返回日期表的DAX函数

此类别中的DAX函数返回日期表。这些函数将主要用作DAX函数CALCULATE的SetFilter参数。

此类别中有16个DAX功能。这些DAX功能中的八(8)个是“上一个”和“下一个”功能。

  • “上一个”和“下一个”功能从当前上下文中的日期列开始,并计算前一天或下一天,月份,季度或年份。

  • “上一个”功能从当前上下文中的第一个日期开始向后运行,“下一个”功能从当前上下文中的最后一个日期开始向后移动。

  • “上一个”和“下一个”函数以单列表的形式返回结果日期。

Sr.No. DAX Function & Return Value
1

PREVIOUSDAY (Date_Column)

Returns a table that contains a column of all dates representing the day that is previous to the first date in the Date_Column in the current context.

2

NEXTDAY (Date_Column)

Returns a table that contains a column of all dates from the next day, based on the first date specified in the Date_Column in the current context.

3

PREVIOUSMONTH (Date_Column)

Returns a table that contains a column of all dates from the previous month, based on the first date in the Date_Column in the current context.

4

NEXTMONTH (Date_Column)

Returns a table that contains a column of all dates from the next month, based on the first date in the Date_Column in the current context.

5

PREVIOUSQUARTER (Date_Column)

Returns a table that contains a column of all dates from the previous quarter, based on the first date in the Date_Column in the current context.

6

NEXTQUARTER (Date_Column)

Returns a table that contains a column of all dates in the next quarter, based on the first date specified in the Date_Column in the current context.

7

PREVIOUSYEAR (Date_Column, [YE_Date])

Returns a table that contains a column of all dates from the previous year, given the last date in the Date_Column in the current context.

8

NEXTYEAR (Date_Column, [YE_Date])

Returns a table that contains a column of all dates in the next year, based on the first date in the Date_Column in the current context.

四(4)个DAX函数计算一个时间段中的一组日期。这些函数使用当前上下文中的最后一个日期执行计算。

Sr.No. DAX Function & Return Value
1

DATESMTD (Date_Column)

Returns a table that contains a column of the dates for the month to date, in the current context.

2

DATESQTD (Date_Column)

Returns a table that contains a column of the dates for the quarter to date, in the current context.

3

DATESYTD (Date_Column, [YE_Date])

Returns a table that contains a column of the dates for the year to date, in the current context.

4

SAMEPERIODLASTYEAR (Date_Column)

Returns a table that contains a column of dates shifted one year back in time from the dates in the specified Date_Column, in the current context.

Note− SAMEPERIODLASTYEAR requires that the current context contains a contiguous set of dates.

If the current context is not a contiguous set of dates, then SAMEPERIODLASTYEAR will return an error.

  • 四(4)个DAX函数用于将当前上下文中的日期集转换为新的日期集。

    这些DAX功能比以前的功能更强大。

    • DAX函数– DATEADD,DATESINPERIOD和PARALLELPERIOD从当前上下文移出一定数量的时间间隔。时间间隔可以是天,月,季度或年,分别由关键词DAY,MONTH,QUARTER和YEAR表示。

      例如:

  • 向后移2天。

  • 向前移动5个月。

  • 从今天起向前推进一个月。

  • 回到去年的同一季度。

      如果函数参数-间隔数(整数值)为正,则shift为正;如果为负,则shift为后。

    • DAX函数– DATESBETWEEN计算指定的开始日期和结束日期之间的日期集。

Sr.No. DAX Function & Return Value
1

DATEADD (Date_Column, Number_of_Intervals, Interval)

Returns a table that contains a column of dates, shifted either forward or backward in time by the specified number of intervals from the dates in the current context.

2

DATESINPERIOD (Date_Column, Start_Date, Number_of_Intervals, Interval)

Returns a table that contains a column of dates that begins with the start_date and continues for the specified number_of_intervals.

3

PARALLELPERIOD (Date_Column, Number_of_Intervals, Interval)

Returns a table that contains a column of dates that represents a period parallel to the dates in the specified Date_Column in the current context, with the dates shifted a number of intervals either forward or backward in time.

4

DATESBETWEEN (Date_Column, Start_Date, End_Date)

Returns a table that contains a column of dates that begins with the start_date and continues until the end_date.

DAX函数,用于评估一段时间内的表达式

此类别中的DAX函数在指定时间段内评估表达式。

此类别中有九(9)个DAX函数-

  • 此类别中的三(3)个DAX函数可用于评估指定时间段内的任何给定表达式。

Sr.No. DAX Function & Return Value
1

TOTALMTD (Expression, Date_Column, [SetFilter])

Evaluates the value of the expression for the dates in the month to date, in the current context.

2

TOTALQTD (Expression, Date_Column, [SetFilter])

Evaluates the value of the expression for the dates in the quarter to date, in the current context.

3

TOTALYTD (Expression, Date_Column, [SetFilter], [YE_Date])

Evaluates the value of the expression for the dates in the year to date, in the current context

  • 此类别中的六(6)个DAX函数可用于计算期初和期末余额。

    • 任何期间的期初余额与上一个期间的期末余额相同。

    • 期末余额包括该期间结束时的所有数据,而期末余额不包括该期间内的任何数据。

    • 这些DAX函数始终返回针对特定时间点评估的表达式的值。

  • 我们关心的时间点始终是日历周期中的最后一个可能的日期值。

  • 期初余额基于上一期间的最后一个日期,而期末余额基于当前期间中的最后一个日期。

  • 当前期间始终由当前日期上下文中的最后一个日期确定。

Sr.No. DAX Function & Return Value
1

OPENINGBALANCEMONTH (Expression, Date_Column, [SetFilter])

Evaluates the expression at the first date of the month in the current context.

2

CLOSINGBALANCEMONTH (Expression, Date_Column, [SetFilter])

Evaluates the expression at the last date of the month in the current context.

3

OPENINGBALANCEQUARTER (Expression, Date_Column, [SetFilter])

Evaluates the expression at the first date of the quarter, in the current context.

4

CLOSINGBALANCEQUARTER (Expression, Date_Column, [SetFilter])

Evaluates the expression at the last date of the quarter in the current context.

5

OPENINGBALANCEYEAR (Expression, Date_Column, [SetFilter], [YE_Date])

Evaluates the expression at the first date of the year in the current context.

6

CLOSINGBALANCEYEAR (Expression, Date_Column, [SetFilter], [YE_Date])

Evaluates the expression at the last date of the year in the current context.