Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

Measure to Sum Joined Tables

I have my data model set up with a customer table and an eventtable. Multiple customers go to each event, and events have different durations. I want to sum up the total minutes that customers are at events. In SQL I could do:

 

select sum(duration)
from Events E
join CustomerEvents CE on E.id = CE.EventID

 

Because the join would duplicate rows for each event to show one record per combination of event and customer, this should work. 

 

How do I do the same thing in PBI? I tried sumx(CustomerEvents,sum(Events[Duration])) and it isn't giving me the correct number.

 

Thanks in advance!

Andy

  • Hi Anonymous ,

     

    You don't need to sum it twice. Please refer to the following measure:

    Measure =
    CALCULATE (
        SUM ( Events[Duration] ),
        NATURALINNERJOIN ( 'Events', 'CustomerEvents' )
    )
    

     

3 Replies

  • az38's avatar
    az38
    Icon for Community Champion rankCommunity Champion

    Anonymous hi

    you need set up relationships between tables by ID field, then just create a measure like CALCULATE(SUM(Events[Duration]))

    • Anonymous's avatar
      Anonymous
      Not applicable

      The relationships are already set up. They are 1-*, and only cross filter one direction for necessity. There are other relationships involved, so the actual calculate measure I am using has more to it than this, but I am currently using something like this:

      measure = 
      calculate(
        sumx(CustomerEvents,sum(Events[Duration]))
        ,crossfilter(CustomerEvents,Events,both)
      )

       

      This doesn't result in the correct answer. Here's why:

      Sum(Events[Duration]) = 392
      Countrows(CustomerEvents) = 38
      sumx(CustomerEvents,sum(Events[Duration])) = 14,896
      sumx(CustomerEvents,sumx(Events,[Duration])) = 14,896

       So what this is doing is giving me 392*38 as the answer, when that is not correct. I need the durations to be spread out / weighted by how many customers attended each event. The correct answer is 1,305 when calculated correctly using the join method above in SQL, but I can't get that to happen in PBI.

      • v-eachen-msft's avatar
        v-eachen-msft
        Icon for Community Support rankCommunity Support

        Hi Anonymous ,

         

        You don't need to sum it twice. Please refer to the following measure:

        Measure =
        CALCULATE (
            SUM ( Events[Duration] ),
            NATURALINNERJOIN ( 'Events', 'CustomerEvents' )
        )