DAX:计算度量 "without" 当前行
DAX: calculate measure "without" current row
我想计算当前行的 DAX 度量 "without"。这是基于 AdventureWorksDW2014 的示例。
evaluate
summarize(
'FactInternetSales'
, 'DimProduct'[ProductSubcategoryName]
单元格级别的度量,这些在 https://docs.microsoft.com/en-us/sql/analysis-services/lesson-5-create-calculated-columns
中定义
, "Margin", [InternetTotalMargin] -- InternetTotalMargin := SUM([Margin])
, "Cost", [InternetTotalProductCost] -- InternetTotalProductCost := SUM([TotalProductCost])
我定义了一个新的度量,它是现有度量的复合(比率):
, "Gross Margin", [GrossMargin] -- GrossMargin := DIVIDE([InternetTotalMargin], [InternetTotalProductCost])
列总数:
, "Total Margin", calculate([InternetTotalMargin], allselected('DimProduct'))
, "Total Cost", calculate([InternetTotalProductCost], allselected('DimProduct'))
没有当前行的列总计,通过列总值表达式减去单元格值表达式得到:
, "Total Margin w/o Subcat", calculate([InternetTotalMargin], allselected('DimProduct')) - [InternetTotalMargin]
, "Total Cost w/o Subcat", calculate([InternetTotalProductCost], allselected('DimProduct')) - [InternetTotalProductCost]
这会在应用公式之前执行所有聚合:
, "Total Gross Margin", calculate([GrossMargin], allselected('DimProduct'))
我想计算没有 "current row" 的 "Total Gross Margin"。
我的第一次尝试产生了预期的结果,但取决于对 GrossMargin 背后公式的明确了解。
这是 "explicit" 因为我们本质上是在分子和分母减去 "current row" 之后重新实现计算。
我希望能够在我不知道公式或公式可能比简单除法更复杂的情况下使用度量来执行此操作。
, "Total Gross Margin w/o Subcat (Explicit)",
-- explicitly repeat the division formula
divide(
-- explicitly recompute the numerator
calculate([InternetTotalMargin], allselected('DimProduct')) - [InternetTotalMargin]
-- explicitly recompute the denominator
, calculate([InternetTotalProductCost], allselected('DimProduct')) - [InternetTotalProductCost]
)
-- next step/where this is meant to be used:
, "Gross Margin Impact (Explicit)", calculate([GrossMargin], allselected('DimProduct')) - divide(calculate([InternetTotalMargin], allselected('DimProduct')) - [InternetTotalMargin], calculate([InternetTotalProductCost], allselected('DimProduct')) - [InternetTotalProductCost])
我想要一个不需要计算知识的更通用的解决方案,如下所示:
/*
, "Gross Margin w/o Subcat", calculate([GrossMargin], allselected('DimProduct' Except Current Row)
, "Gross Margin Impact, calculate([GrossMargin], allselected('DimProduct')) - calculate([GrossMargin], allselected('DimProduct' Except Current Row)
*/
不同的失败尝试(使用 Margin 而不是 GrossMargin,因为它更容易检查):
, "Trial 1", calculate([InternetTotalMargin], except(all('DimProduct'), 'DimProduct')) -- incorrect value
, "Trial 2", calculate([InternetTotalMargin], allexcept('DimProduct', 'DimProduct'[ProductSubcategoryName])) -- incorrect value
, "Trial 3", calculate([InternetTotalMargin], allexcept('DimProduct', 'DimProduct'[ProductCategoryName])) -- incorrect value
, "Trial 4", calculate([InternetTotalMargin], allexcept('DimProduct', 'DimProduct'[ProductKey])) -- incorrect value
, "Trial 5", calculate([InternetTotalMargin], 'DimProduct') -- incorrect
, "Trial 6", calculate([InternetTotalMargin], 'DimProduct'[ProductSubcategoryName] <> 'DimProduct'[ProductSubcategoryName]) -- illogical, and blank result
-- , "Trial 7", calculate([InternetTotalMargin], 'DimProduct'[ProductSubcategoryName] <> [ProductSubcategoryName]) -- invalid syntax
-- running out of ideas here
)
;
在 DAX 中这甚至可能吗?
不,这在 DAX 中是不可能的。
如果你想计算一个度量,你需要先定义它。您不能创建占位符/在此处插入公式/并希望一切顺利。
你可以做的是使用关键字 VAR 来定义你的度量,在计算的时候计算不同的度量并选择最适合的。或者,如果可能,使用调用函数通过 "replace" 方法定义度量。
我想计算当前行的 DAX 度量 "without"。这是基于 AdventureWorksDW2014 的示例。
evaluate
summarize(
'FactInternetSales'
, 'DimProduct'[ProductSubcategoryName]
单元格级别的度量,这些在 https://docs.microsoft.com/en-us/sql/analysis-services/lesson-5-create-calculated-columns
中定义 , "Margin", [InternetTotalMargin] -- InternetTotalMargin := SUM([Margin])
, "Cost", [InternetTotalProductCost] -- InternetTotalProductCost := SUM([TotalProductCost])
我定义了一个新的度量,它是现有度量的复合(比率):
, "Gross Margin", [GrossMargin] -- GrossMargin := DIVIDE([InternetTotalMargin], [InternetTotalProductCost])
列总数:
, "Total Margin", calculate([InternetTotalMargin], allselected('DimProduct'))
, "Total Cost", calculate([InternetTotalProductCost], allselected('DimProduct'))
没有当前行的列总计,通过列总值表达式减去单元格值表达式得到:
, "Total Margin w/o Subcat", calculate([InternetTotalMargin], allselected('DimProduct')) - [InternetTotalMargin]
, "Total Cost w/o Subcat", calculate([InternetTotalProductCost], allselected('DimProduct')) - [InternetTotalProductCost]
这会在应用公式之前执行所有聚合:
, "Total Gross Margin", calculate([GrossMargin], allselected('DimProduct'))
我想计算没有 "current row" 的 "Total Gross Margin"。 我的第一次尝试产生了预期的结果,但取决于对 GrossMargin 背后公式的明确了解。 这是 "explicit" 因为我们本质上是在分子和分母减去 "current row" 之后重新实现计算。 我希望能够在我不知道公式或公式可能比简单除法更复杂的情况下使用度量来执行此操作。
, "Total Gross Margin w/o Subcat (Explicit)",
-- explicitly repeat the division formula
divide(
-- explicitly recompute the numerator
calculate([InternetTotalMargin], allselected('DimProduct')) - [InternetTotalMargin]
-- explicitly recompute the denominator
, calculate([InternetTotalProductCost], allselected('DimProduct')) - [InternetTotalProductCost]
)
-- next step/where this is meant to be used:
, "Gross Margin Impact (Explicit)", calculate([GrossMargin], allselected('DimProduct')) - divide(calculate([InternetTotalMargin], allselected('DimProduct')) - [InternetTotalMargin], calculate([InternetTotalProductCost], allselected('DimProduct')) - [InternetTotalProductCost])
我想要一个不需要计算知识的更通用的解决方案,如下所示:
/*
, "Gross Margin w/o Subcat", calculate([GrossMargin], allselected('DimProduct' Except Current Row)
, "Gross Margin Impact, calculate([GrossMargin], allselected('DimProduct')) - calculate([GrossMargin], allselected('DimProduct' Except Current Row)
*/
不同的失败尝试(使用 Margin 而不是 GrossMargin,因为它更容易检查):
, "Trial 1", calculate([InternetTotalMargin], except(all('DimProduct'), 'DimProduct')) -- incorrect value
, "Trial 2", calculate([InternetTotalMargin], allexcept('DimProduct', 'DimProduct'[ProductSubcategoryName])) -- incorrect value
, "Trial 3", calculate([InternetTotalMargin], allexcept('DimProduct', 'DimProduct'[ProductCategoryName])) -- incorrect value
, "Trial 4", calculate([InternetTotalMargin], allexcept('DimProduct', 'DimProduct'[ProductKey])) -- incorrect value
, "Trial 5", calculate([InternetTotalMargin], 'DimProduct') -- incorrect
, "Trial 6", calculate([InternetTotalMargin], 'DimProduct'[ProductSubcategoryName] <> 'DimProduct'[ProductSubcategoryName]) -- illogical, and blank result
-- , "Trial 7", calculate([InternetTotalMargin], 'DimProduct'[ProductSubcategoryName] <> [ProductSubcategoryName]) -- invalid syntax
-- running out of ideas here
)
;
在 DAX 中这甚至可能吗?
不,这在 DAX 中是不可能的。 如果你想计算一个度量,你需要先定义它。您不能创建占位符/在此处插入公式/并希望一切顺利。 你可以做的是使用关键字 VAR 来定义你的度量,在计算的时候计算不同的度量并选择最适合的。或者,如果可能,使用调用函数通过 "replace" 方法定义度量。