Forum Discussion

bhalicki's avatar
bhalicki
Helper V
3 years ago
Solved

COUNTROWS total rows by group

I have the following tables and relationships:
User

UserIdUserName
1Bill
2John
3Fred

 

License

LicenseIdLicenseDescUserId
1License A1
2License B1
3License C2
4License D2
5License E2
6License F3

 

I want to write a measure that counts the number of rows in the license table (based on the username grouping) and results in the following table:

LicenseIdLicenseDescUserNameLicensesAllocated
1License ABill2
2License BBill2
3License CJohn3
4License DJohn3
5License EJohn3
6License FFred1


I assume I need to use COUNTROWS, but the combinations I have tried either end up with '1' for every value in LicenseAllocated, or a cartesian product.

Any help would be greatly appreciated!

  • bhalicki ,

    You can try this measure:

    LicensesAllocated = 
    CALCULATE(COUNTROWS(License), FILTER(ALL(License), License[UserId] = SELECTEDVALUE(License[UserId])))

     

6 Replies

  • Hi bhalicki ,

    As per your requirement of getting count of Licenses per User, you should create a column in the User Table with below DAX expression.

    License Allocated = CALCULATE(COUNTROWS(License), FILTER(License, License[UserId] = User[UserId]))


    Add columns in the table and you can see the expected result will include the License Allocated by each user.

     

    If this answer helps, please mark it as an Accepted Solution so it would help others to find the solution.


    Thanks!

    Inogic Professional Service Division

    An expert technical extension for your techno-functional business needs

    Power Platform/Dynamics 365 CRM

    Drop an email at [email protected]

    Service:  http://www.inogic.com/services/ 

    Power Platform/Dynamics 365 CRM Tips and Tricks:  http://www.inogic.com/blog/

    • bhalicki's avatar
      bhalicki
      Helper V

      Hi SamInogic ,

      Thanks for your quick reply!  I tried your solution, but I get:

      This is the measure (note the RELATED function):

      LicenseAllocated = CALCULATE(COUNTROWS(License), FILTER(License, License[UserId] = RELATED(User[UserId])))

      These are the relationships:
      Users table:

      License table:

      Any ideas?


       

      • SamInogic's avatar
        SamInogic
        Super User

        Hi bhalicki 

        It seems that you followed the steps for the solution but created measure instead of the Column in the table.

        License Allocated = CALCULATE(COUNTROWS(License), FILTER(License, License[UserId] = User[UserId]))


        Can you check the above DAX as a column in the table and add on the report to check output?

        Thanks!

        Inogic Professional Service Division

        An expert technical extension for your techno-functional business needs

        Power Platform/Dynamics 365 CRM

        Drop an email at [email protected]

        Service:  http://www.inogic.com/services/ 

        Power Platform/Dynamics 365 CRM Tips and Tricks:  http://www.inogic.com/blog/

  • ERD's avatar
    ERD
    Community Champion

    bhalicki ,

    You can try this measure:

    LicensesAllocated = 
    CALCULATE(COUNTROWS(License), FILTER(ALL(License), License[UserId] = SELECTEDVALUE(License[UserId])))

     

    • bhalicki's avatar
      bhalicki
      Helper V

      HI ERD , thankyou that worked perfectly!

      Out of interest, do you know why it wouldn't calculate the total for this measure?


      Kind regards,

      Ben.

  • Yes, sorry I need a measure not a calculated column on a table.