Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
2 years ago
Solved

How to count by two categories with DAX ?

Hey everyone, I have my table one, who look like this :

LocationXY

Location_1

AB
Location_1BC
Location_2AC
Location_4CA
.........

 

I've created another table manually to which I'd like to add the count of each note by Location in another column, but I can't manage to code it in DAX to do this, can someone help me?

Expected output :

 

LocationRankCount_XCount_Y

Location_1

A5610
Location_2A3816
Location_3A2363
Location_4A1265
Location_1B7526
Location_2B8337
Location_3B9474
Location_4B3573
Location_1C6415
Location_2C19464
Location_3C28525
Location_4C186241

(All numbers are here for example)


Thanks all for any help !

  • Unpivot your "Table one"  to bring it into usable format.

     

    Then you can use implicit measures to achieve your result, no need for DAX.

     

     

     

     

  • Anonymous's avatar
    Anonymous
    2 years ago

    Hi Anonymous 

     

    lbendlin Thank you very much for your prompt reply, and allow me to add something here.

     

    As lbendlin mentioned, you need to do unpivot columns for column "X" and column "Y" and then do the calculations.

     

    Here's some dummy data

     

    “Table”

     

    Select two columns in the power query and click unpivot columns.

     

     

     

     

    Create measures.

    Count_Y = 
    CALCULATE(
        COUNTROWS('Table'), 
        FILTER(
            ALL('Table'), 
            'Table'[Location] = MAX('Table'[Location]) 
            && 
            'Table'[Attribute] = "Y" 
            && 
            'Table'[Rank] = MAX('Table'[Rank])
        )
    )

     

    Count_X = 
    CALCULATE(
        COUNTROWS('Table'), 
        FILTER(
            ALL('Table'), 
            'Table'[Location] = MAX('Table'[Location]) 
            && 
            'Table'[Attribute] = "X" 
            && 
            'Table'[Rank] = MAX('Table'[Rank])
        )
    )
    

     

    Here is the result.

     

    Regards,

    Nono Chen

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

     

     

     

     

     

     

2 Replies

  • Unpivot your "Table one"  to bring it into usable format.

     

    Then you can use implicit measures to achieve your result, no need for DAX.

     

     

     

     

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi Anonymous 

     

    lbendlin Thank you very much for your prompt reply, and allow me to add something here.

     

    As lbendlin mentioned, you need to do unpivot columns for column "X" and column "Y" and then do the calculations.

     

    Here's some dummy data

     

    “Table”

     

    Select two columns in the power query and click unpivot columns.

     

     

     

     

    Create measures.

    Count_Y = 
    CALCULATE(
        COUNTROWS('Table'), 
        FILTER(
            ALL('Table'), 
            'Table'[Location] = MAX('Table'[Location]) 
            && 
            'Table'[Attribute] = "Y" 
            && 
            'Table'[Rank] = MAX('Table'[Rank])
        )
    )

     

    Count_X = 
    CALCULATE(
        COUNTROWS('Table'), 
        FILTER(
            ALL('Table'), 
            'Table'[Location] = MAX('Table'[Location]) 
            && 
            'Table'[Attribute] = "X" 
            && 
            'Table'[Rank] = MAX('Table'[Rank])
        )
    )
    

     

    Here is the result.

     

    Regards,

    Nono Chen

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.