DAX:表值函数 ADDCOLUMNS 和 SELECTCOLUMNS

函数ADDCOLUMNS和SELECTCOLUMNS的功能相似,都是迭代函数,在行下文中计算表达式,并返回一个表。

从数据沿袭的角度来看:

  • ADDCOLUMNS函数:新添加的列不具有数据沿袭的能力。
  • SELECTCOLUMNS函数:SELECTCOLUMNS 保留直接对列引用的数据沿袭,对列执行的任何表达式都会破坏数据沿袭。

一,ADDCOLUMNS函数

ADDCOLUMNS是一个迭代函数,向<table>参数中添加额外的计算列。新增加的计算列是在ADDCOLUMNS的行上下文中计算的,如果需要,可以把行上下文转换为筛选上下文。

ADDCOLUMNS的返回值是一个表,包含第一个参数<table>的所有列,和新增加的计算列。注意,新增加的不具有数据沿袭。

ADDCOLUMNS(<table>, <name>, <expression>[, <name>, <expression>]…)

ADDCOLUMNS的执行过程是:

第一步:迭代<table>参数,创建行上下文。

第二步:添加列在行上下文中计算<expression>参数,如果需要上下文转换,可以调用上下文转换以生成筛选上下文。

第三步:把<table>参数的所有列和新添加的列都输出到结果中

举个例子,可以在行上下文与其他相关联表进行聚合计算:

ADDCOLUMNS(ProductCategory,
               , "Internet Sales", SUMX(RELATEDTABLE(InternetSales_USD), InternetSales_USD[SalesAmount_USD])  
               , "Reseller Sales", SUMX(RELATEDTABLE(ResellerSales_USD), ResellerSales_USD[SalesAmount_USD]))  

ADDCOLUMNS函数也可以调用度量,执行上下文转换,把行上下文转换为过滤器上下文,例如,对于列"@Quantity Correct",调用CALCULATE进行上下文转换。

--  ADDCOLUMNS is an iterator that returns its first argument after adding the column specified.
--  New columns are computed in the row context of ADDCOLUMNS,
--  you need to invoke context transition to generate a filter context, if needed.
FILTER (
    ADDCOLUMNS (
        VALUES ( 'Date'[Calendar Year] ),
        "@Year Number",         INT ( RIGHT ( 'Date'[Calendar Year], 4 ) ),
        "@Amount",              [Sales Amount],
        "@Quantity Wrong",      SUM ( Sales[Quantity] ),
        "@Quantity Correct",    CALCULATE ( SUM ( Sales[Quantity] ) )
    ),
    [@Amount] > 0
)

二,SELECTCOLUMNS函数

SELECTCOLUMNS是一个迭代函数,和ADDCOLUMNS的区别是:SELECTCOLUMNS不返回<table>参数的所有列,而是返回所有<name>和<expression>参数对指定的列。

SELECTCOLUMNS(<table>, <name>, <scalar_expression> [, <name>, <scalar_expression>]…) 

SELECTCOLUMNS函数的计算过程是:

step1:根据<table>参数创建行上下文,迭代<table>的每一行。

step2:添加的列在行上下文中计算<expression>参数,如果需要上下文转换,可以调用上下文转换以生成筛选上下文。

SELECTCOLUMNS函数的返回值是一个表:返回值跟<table>参数指定的表具有相同的行数,其列是由name和expression指定的,且每一个expression参数都是在<table>参数的行下文中计算的。

--  SELECTCOLUMNS has a row context that can be used to write
--  expressions that navigate through relationships.
CALCULATETABLE (
    SELECTCOLUMNS (
        Sales,
        "Order Number", Sales[Order Number],
        "Order Line Number", Sales[Order Line Number],
        "Customer", RELATED ( Customer[Name] ),
        "Product", RELATED ( 'Product'[Product Name] ),
        "Quantity", Sales[Quantity],
        "Line Amount", Sales[Quantity] * Sales[Net Price]
    ),
    'Date'[Date] = DATE ( 2007, 9, 19 ),
    Customer[Customer Type] = "Person"
)

三,用SQL来模拟

用SQL来模拟DAX 表值函数的功能

1,用SQL来模拟ADDCOLUMNS函数

ADDCOLUMNS (
    'Product',
    "Unit Margin", 'Product'[Unit Price] - 'Product'[Unit Cost]
)

用SQL来表示相同的查询:

SELECT *, [Unit Price] - [Unit Cost] AS [Unit Margin]
FROM Product

2,用SQL来模拟SELECTCOLUMNS函数

SELECTCOLUMNS (
    'Product',
    "ProductKey", 'Product'[ProductKey],
    "Product Name", 'Product'[Product Name],
    "Unit Price", 'Product'[Unit Price]
)

用SQL来表示相同的查询:

SELECT [ProductKey], [Product Name], [Unit Price]
FROM Product

3,用SQL来模拟SUMMARIZE

SUMMARIZE (
    'Product',
    'Product'[ProductKey],
    'Product'[Product Name],
    'Product'[Unit Price]
)

用SQL来表示相同的查询

SELECT DISTINCT [ProductKey], [Product Name], [Unit Price]
FROM Product

 

 

参考文档:

 

posted @ 2022-05-25 11:15  悦光阴  阅读(869)  评论(0编辑  收藏  举报