Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
8 years ago
Solved

Optimize Z Scores across multiple dimensions

I am trying to Optimize a query that I have that produces the correct results, however very slow.   What I want to do is to click on a visual for the 'Dimension' and then another visual shows the Z...
  • Anonymous's avatar
    Anonymous
    8 years ago

    If anyone is interested if they come across a similar problem. This is a much more effecient version.

     

    Make each Dimension as variables (This is One Dimension. I have duplicated this as variables for every dimension.

    VAR SelectedDimension = [SelectedDimension]
    
    VAR OutcomeYearCategory = "Outcome Year"
    VAR OutcomeYearIndicator = IF(SelectedDimension = OutcomeYearCategory, 1, BLANK())
    VAR OutcomeYearSimpleTable =
    	ADDCOLUMNS(ADDCOLUMNS(
        VALUES('Dates Outcome'[Financial Year]), 
            "Category", OutcomeYearCategory),
            "Encoded", TRIM(CONCATENATE([Category], CONCATENATE(" ", [Financial Year]))), 
            "Numerator" ,  [No Products] * OutcomeYearIndicator ,
    		"Denominator", [No Contact] * OutcomeYearIndicator
            )	
     
    VAR OutcomeYearTable = 
    	ADDCOLUMNS(ADDCOLUMNS(OutcomeYearSimpleTable,
    		"NumeratorTotal", CALCULATE([No Products] * OutcomeYearIndicator, ALL('Dates Outcome'[Financial Year])),
    	    "DenominatorTotal", CALCULATE([No Contact] * OutcomeYearIndicator, ALL('Dates Outcome'[Financial Year]))
    	),
    		"IndexOfProportion", DIVIDE ( [Numerator], [NumeratorTotal]) - DIVIDE([Denominator], [DenominatorTotal])
    	)	
    VAR OutcomeYearTableZ = 
    ADDCOLUMNS(
    	ADDCOLUMNS(OutcomeYearTable,
    		"Mean", AVERAGEX ( OutcomeYearTable, [IndexOfProportion] ),
    		"StdDev", STDEVX.P ( OutcomeYearTable, [IndexOfProportion] )
    	
    	),
    		"ZScore", DIVIDE (( [IndexOfProportion] - [Mean] ), [StdDev]) 
    	)

    Then I  pull it all together with a megaTable that holds every single dimension.

     

    VAR megaTable = UNION(OutcomeYearTableZ, OutcomeQuarterTableZ)
    
    
    RETURN 
    
    MINX(megaTable, if([Encoded] = MIN('Key Metrics Detail'[DimensionEncoded]), [ZScore], blank()))

    By using the 'indicators' and multiplying things by blank values it means only the relevant table is being calculated and drops the processing time to 100ms.

     

    This method should work across any multi dimension calculation, not just ZScores.