Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
2 years ago
Solved

DAX function to count rows, Group by Column and divide by total count of rows

Dax function for the below steps from table AAA

  1. Count the number of rows in column “coldef” using group by condition. This column has data type as Text
  2. Count the total rows in column  “coldef” which has data type as text
  3. Divide step 1 by step 2
  • Anonymous's avatar
    Anonymous
    2 years ago

    Hi Anonymous ,

     

    Please try:

    Count of Column = COUNTROWS('Table')
    
    - B% DAX = 
    VAR allCount = CALCULATE(COUNTROWS('Table'),ALL('Table'))
    RETURN
    DIVIDE([Count of Column],allCount)

     

    Once created, if you want to display percentages without decimals, you can check "%" in the Measure tool tab and then select the number of decimals to be followed by zero.

     

    If you use the Format function, it will default to two decimal places.

    - B% DAX = 
    VAR allCount = CALCULATE(COUNTROWS('Table'),ALL('Table'))
    RETURN
    FORMAT(DIVIDE([Count of Column],allCount), "Percent")

     

    Drag the created measures to the report page for display, the page effect is shown below:

     

    The pbix file is attached.

     

    If you have any other questions please feel free to contact me.

     

    Best Regards,
    Yang
    Community Support Team

     

    If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly.
    If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

6 Replies

  • Anonymous , Based on what I got. Try a measure like

     

    Divide( Countrows(Summarize(Table, Table[Column]) ), countrows(Table) )

  • Anonymous's avatar
    Anonymous
    Not applicable

    Thank you very much, Amit for your help. 

    This still needs some modification as the expected result should be something as below:

    Row 1 - 55/277

    Row 2 - 40/277 ..........

     

    Column 1Row Count (Grouped)Divide
    A5520%
    B4014%
    C207%
    D7828%
    E62%
    F7828%
     277100%
    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi Anonymous ,

       

      This is sample data:

       

      This is your expected result:

       

      Please try:

      TotalColumn1 = CALCULATE(SUM('Table'[Row Count (Grouped)]),ALL('Table'))
      
      Column = DIVIDE([Row Count (Grouped)],[TotalColumn1])

       

      You can do it with a measure, but it defaults to two decimals when converted to a percentage, and total is wrong, so it is better to use a New column.

       

      The pbix file is attached.

       

      If you have any other questions please feel free to contact me.

       

      Best Regards,
      Yang
      Community Support Team

       

      If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly.
      If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

  • Anonymous's avatar
    Anonymous
    Not applicable

    Thanks much for helping on this. Let me elaborate my question clearly. 

    Source Table
    Column - A
    (TEXT)
    Column - B
    (TEXT)
    FurnitureA01234
    FurnitureA12345
    AccessoriesC24564
    AccessoriesC22335
    AccessoriesD57845
    AccessoriesD65894
    AccessoriesE55882
    EquipmentK98562
    EquipmentK85857
    EquipmentK22335
    EquipmentL65875
    EquipmentL78548
    EquipmentL12548

     

    Required Output
    Column - A
    (TEXT)
    Count of Column - B% DAX
    Furniture215%
    Accessories538%
    Equipment646%
    Total13100%
    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi Anonymous ,

       

      Please try:

      Count of Column = COUNTROWS('Table')
      
      - B% DAX = 
      VAR allCount = CALCULATE(COUNTROWS('Table'),ALL('Table'))
      RETURN
      DIVIDE([Count of Column],allCount)

       

      Once created, if you want to display percentages without decimals, you can check "%" in the Measure tool tab and then select the number of decimals to be followed by zero.

       

      If you use the Format function, it will default to two decimal places.

      - B% DAX = 
      VAR allCount = CALCULATE(COUNTROWS('Table'),ALL('Table'))
      RETURN
      FORMAT(DIVIDE([Count of Column],allCount), "Percent")

       

      Drag the created measures to the report page for display, the page effect is shown below:

       

      The pbix file is attached.

       

      If you have any other questions please feel free to contact me.

       

      Best Regards,
      Yang
      Community Support Team

       

      If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly.
      If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

      • Anonymous's avatar
        Anonymous
        Not applicable

        Thanks a lot for your help.