Pergunta

I have a query to pull clickthrough for a funnel, where if a user hit a page it records as "1", else NULL --

SELECT datestamp
    ,COUNT(visits) as Visits
    ,count([QE001]) as firstcount 
    ,count([QE002]) as secondcount
    ,count([QE004]) as thirdcount
    ,count([QE006]) as finalcount
    ,user_type
    ,user_loc
FROM
    dbname.dbo.loggingtable
GROUP BY user_type, user_loc

I want to have a column for each ratio, e.g. firstcount/Visits, secondcount/firstcount, etc. as well as a total (finalcount/Visits).

I know this can be done

  • in an Excel PivotTable by adding a "calculated field"
  • in SQL by grouping
  • in PowerPivot by adding a CalculatedColumn, e.g.

    =IFERROR(QueryName[finalcount]/QueryName[Visits],0)
    
  • BUT I need give the report consumer the option of slicing by just user_type or just user_loc, etc, and excel will tend to ADD the proportions, which won't work b/c

    SUM(A/B) != SUM(A)/SUM(B)
    

Is there a way in DAX/MDX/PowerPivot to add a calculated column/measure, so that it will be calculated as SUM(finalcount)/SUM(Visits), for any user-defined subset of the data (daterange, user type, location, etc.)?

Foi útil?

Solução

Yes, via calculated measures. calculated columns are for creating values that you want to see on rows/columns/report header...calculated measures are for creating values that you want to see in the values section of a pivot table and can slice/dice by the columns in the model.

The easiest way would be to create 3 calculated "measures" in the calculation area of the powerpivot sheet.

TotalVisits:=SUM(QueryName[visits])
TotalFinalCount:=SUM(QueryName[finalcount])
TotalFinalCount2VisitsRatio:=[TotalFinalCount]/[TotalVisits]

You can then slice the calculated measure [TotalFinalCount2VisitsRatio] by user_type or just user_loc (or whatever) and the value will be calculated correctly. The difference here is that you are explicitly telling the xVelocity engine to SUM-then-DIVIDE. If you create the calculated column, then the engine thinks you want to DIVIDE-then-SUM.

Also, you don't have to break down the measure into 3 separate measures...it's just good practice. If you're interested in learning more, I'd recommend this book...the author is the PowerPivot/DAX guru and the book is very straightforward.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top