I have two tables (Sales,Stock) both have a DateKey and ProductCode. The requirement is to count the Distinct ProductCodes across both tables by Financial week driven by a custom Date Dimension. There is a further requirement to take the Average of the Weekly Distinct counts if multiple weeks are selected not the distinct count for all weeks.
The below DAX works in my unit tests, however on real data volumes (300+ million rows imported) it grinds to a complete standstill. If I select a week it takes about 5 sec, for 12 months its 60 seconds. They need to do YoY and WoW calcs so this perfomance will not be good enough. Not sure how else to achieve this.
Here is the DAX I've written:
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]
)