【发布时间】:2020-12-18 07:54:55
【问题描述】:
我有两个表(Sales、Stock)都有一个 DateKey 和 ProductCode。要求是按自定义日期维度驱动的财务周计算两个表中的不同产品代码。如果选择了多个周而不是所有周的不同计数,则还需要获取每周不同计数的平均值。
下面的 DAX 在我的单元测试中工作,但是在实际数据量(导入 300+ 百万行)上它完全停止。如果我选择一周大约需要 5 秒,12 个月需要 60 秒。他们需要进行 YoY 和 WoW 计算,因此这种性能还不够好。不知道还有什么方法可以做到这一点。
这是我编写的 DAX:
Option Count =
// Get options at a week level for Sales
VAR SalesOptions =
SELECTCOLUMNS (
Sales,
"Week",
CALCULATE (
MAX ( Dates[Financial Year Week Number] ),
FILTER ( ALL ( Dates ), Sales[DimBilledDateSKey] = Dates[DimDateSKey] )
),
"ProductOptionCode", 'Sales'[ProductOptionCode]
)
// Get options at a week level for Stock
VAR StockOptions =
SELECTCOLUMNS (
Stock,
"Week",
CALCULATE (
MAX ( Dates[Financial Year Week Number] ),
FILTER ( ALL ( Dates ), Stock[DimDateSKey] = Dates[DimDateSKey] )
),
"ProductOptionCode", 'Stock'[ProductOptionCode]
)
// Union the options from both and then apply a distinct so that a count of the product code would be the same result as a distinct count
VAR CombinedOptions = DISTINCT ( UNION ( SalesOptions, StockOptions ) )
RETURN
// Average the weekly disitnct option count
AVERAGEX(
// Group the above union by week and do a count of the options for the week. They should now be distinct due to the distinct applied to the union
GROUPBY(CombinedOptions, [Week], "Week Option Count", COUNTX(CURRENTGROUP(), [ProductOptionCode] )),
[Week Option Count]
)
【问题讨论】: