Forum Discussion

Vitaliy's avatar
Vitaliy
Helper I
5 years ago
Solved

Calculate for missing category (DAX)

Hi !

Please, help with this task:

I have 4 simple tables with some relations like in screenshot. How can I build result table?
Result description:
For C1 category we have 2 Contacts Co1, Co2. This Contacts took part into 2 Activities Call1, Call2. So, result for C1 category is 2.
For C2 category we don't have any Contacts. Due to this fact, we don't have any Activities.
For Non Seg category we don't have any Contacts too. Due to this fact, we don't have any Activities too. But, only in this case, we must calculate result like "All Activities - Activities for Categories". So, result for Non Seg Category is 3-2=1
Also, result table is matrix, which have categories from Category table in columns!



  • Hi, Vitaliy 

    Please check the below picture and the sample pbix file's link down below, whether it is what you are looking for.

     

     

     

    Activity Count =
    IF (
    SELECTEDVALUE ( Catetory[ID] ) = "Non Seg",
    CALCULATE ( COUNTROWS ( ContactActivity ), ALLSELECTED ( ContactActivity ) )
    - CALCULATE (
    DISTINCTCOUNT ( ContactActivity[Callid] ),
    ALLSELECTED ( Catetory )
    ),
    IF (
    NOT ISBLANK ( SELECTEDVALUE ( Catetory[ID] ) ),
    COALESCE ( DISTINCTCOUNT ( ContactActivity[Callid] ), 0 ),
    BLANK ()
    )
    )

     

     

    https://www.dropbox.com/s/o3665ejobsuhyhe/vitaliy.pbix?dl=0 

     

     

    Hi, My name is Jihwan Kim.

    If this post helps, then please consider accept it as the solution to help other members find it faster, and give a big thumbs up.

    Linkedin: https://www.linkedin.com/in/jihwankim1975/

3 Replies

  • selimovd's avatar
    selimovd
    Most Valuable Professional

    Hey Vitaliy ,

     

    please try with the following measure:

    Acitvities =
    VAR vActivities = IF( MAX( Category[Id] ) <> BLANK(), COUNTROWS( ContactActivity ) )
    VAR vAllActivities =
        CALCULATE(
            COUNTROWS( ContactActivity ),
            ALL( Category[Id] )
        )
    VAR vActivitiesForCategories =
        CALCULATE(
            COUNTROWS( ContactActivity ),
            Category[Id] <> BLANK()
        )
    RETURN
        IF(
            ISINSCOPE( Category[Id] ) && MAX( Category[Id] ) = "Non seg",
            vAllActivities - vActivitiesForCategories,
            vActivities
        )

     

    This will produce the following result:

    You can check my file here:

    https://www.swisstransfer.com/d/e3eeb216-2ce4-491a-bd3d-0cb4f39794d6

     

    If you need any help please let me know.
    If I answered your question I would be happy if you could mark my post as a solution ✔️ and give it a thumbs up 👍
     
    Best regards
    Denis
     

     

    • Vitaliy's avatar
      Vitaliy
      Helper I

      Thanks for your response!

      I think this is not quite what I need, cause if we add some new calls into Activity table (for example Call4, Call5) and this calls would be without contacts (no rows in ContactActivity table for this new calls) - result would be the same as you wrote. But, for my case, result must be: C1 == 2, C2 == 0, Non Seg == 3. Or I'm wrong ?

  • Hi, Vitaliy 

    Please check the below picture and the sample pbix file's link down below, whether it is what you are looking for.

     

     

     

    Activity Count =
    IF (
    SELECTEDVALUE ( Catetory[ID] ) = "Non Seg",
    CALCULATE ( COUNTROWS ( ContactActivity ), ALLSELECTED ( ContactActivity ) )
    - CALCULATE (
    DISTINCTCOUNT ( ContactActivity[Callid] ),
    ALLSELECTED ( Catetory )
    ),
    IF (
    NOT ISBLANK ( SELECTEDVALUE ( Catetory[ID] ) ),
    COALESCE ( DISTINCTCOUNT ( ContactActivity[Callid] ), 0 ),
    BLANK ()
    )
    )

     

     

    https://www.dropbox.com/s/o3665ejobsuhyhe/vitaliy.pbix?dl=0 

     

     

    Hi, My name is Jihwan Kim.

    If this post helps, then please consider accept it as the solution to help other members find it faster, and give a big thumbs up.

    Linkedin: https://www.linkedin.com/in/jihwankim1975/