ID 26 - Group by occupation and count the occurrences

Max: 45
Try code in DAX.do

Syntax

COUNT COUNTROWS ROW SUM SUMMARIZE

Related

45 - 3-Year Moving Average of Sales 27 - Group by occupation and count the occurrences AND Filter occupations with count greater than 5000 37 - Ranks customers by total sales grouped product 34 - Split and Group By

SQL

SELECT occupation, COUNT(*) 
FROM customer 
GROUP BY occupation;

DAX

EVALUATE
SUMMARIZE(
    customer,
    customer[occupation],
    "Count", COUNTROWS(customer)
)

Pandas

import pandas as pd

# Sample DataFrame
data = {
    'occupation': ['Engineer', 'Doctor', 'Engineer', 'Artist', 'Doctor']
}

df = pd.DataFrame(data)

# Group by occupation and count the occurrences
df_result = df.groupby('occupation').size().reset_index(name='Count')

# Convert to list of lists
result = df_result.values.tolist()
print(result)

Output

Artist 1
Doctor 2
Engineer 2

Explanation

SUMMARIZE: Groups the customer table by the occupation column. COUNTROWS(customer): Counts the number of rows for each occupation.

Leave a Comment