我想计算“没有”当前行的 DAX 度量。这是一个基于 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'))
我想计算没有“当前行”的“总毛利率”。我的第一次尝试产生了预期的结果,但取决于对 GrossMargin 背后公式的明确了解。这是“显式的”,因为我们在从分子和分母中减去“当前行”之后基本上重新实现了计算。我希望能够用我不知道公式的度量来做到这一点,或者公式可能比简单的除法更复杂。
, "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来定义您的度量,在计算时计算不同的度量并选择最适合的度量。或者,如果可能,使用调用函数通过“替换”方法定义度量。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句