-2

My question builds on this previous post: Excel TextSplit a range and countif

I have a very similar use case, except that the values I'm trying to match may have numbers at the end, like A1, A2, CR3, etc. Solution screenshot posted for previous problem

I want them to be added up such that all A* are counted together, all CR* are counted together, etc.

I'm using the solution provided by @VBasic2008 , and I tried adding the * in the Code list, but it doesn't work. How do I enable this for wildcards? Thanks in advance!

1
  • 3
    Post sample data as text table and show your desired output. What your are trying to achieve by using wild card? Commented Feb 26 at 8:10

2 Answers 2

1

I would suggest a better tool like for example Power Query, one which is better suited for manipulating tables and lists:

let
    // Load source table
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Filter = Table.SelectRows(Source, each ([Codes] <> null)),

    // Split "Codes" column into a list
    ExpandedCodes = Table.TransformColumns(Filter, {"Codes", each Text.Split(_, ","), type list}),

    // Expand "Codes" column into multiple rows
    ExpandRows = Table.ExpandListColumn(ExpandedCodes, "Codes"),

    // Remove empty values in "Codes" column
    RemoveBlanks = Table.SelectRows(ExpandRows, each [Codes] <> ""),

    // Rename column for clarity
    RenamedColumns = Table.RenameColumns(RemoveBlanks, {{"Codes", "Code"}}),

    // Group by Code and count occurrences
    GroupedTable = Table.Group(RenamedColumns, {"Code"}, {{"Total", each Table.RowCount(_), Int64.Type}})
    
in
    GroupedTable

enter image description here

Edit: you could also easily do it with Python:

import pandas as pd

# Hardcoded input data
data = {
    "Name": ["James", "Beth", "Holly", "Jimmy", "Peter", "Charlie", "Sue", "", "Sofie"],
    "Codes": ["1,2", "2,3,4", "6,8,9", "2,A,CR", "", "1,3,5", "", "", "1,CR"]
}

# Convert to DataFrame
df = pd.DataFrame(data)

# Split "Codes" column into multiple rows
df_exploded = df.assign(Codes=df['Codes'].str.split(',')).explode('Codes')

# Remove empty values
df_exploded = df_exploded[df_exploded['Codes'].notna() & (df_exploded['Codes'] != "")]

# Rename "Codes" column to "Code"
df_exploded.rename(columns={'Codes': 'Code'}, inplace=True)

# Count occurrences of each unique "Code"
df_grouped = df_exploded.groupby("Code").size().reset_index(name="Total")

# Display the final table
print(df_grouped)
Sign up to request clarification or add additional context in comments.

3 Comments

I assume OP have different data like 1,CR2,2,A,CR3 but he want to count CR using wild card match.
@Harun24hr - well, I actually clicked on the link with the screenshot so I didn't have to assume anything
@OP's I assume from OP's description and screenshot name describe that, it was for previous problem. however, lets see what OP says.
1

Give a try to the following formula-

=LET(
x,TEXTSPLIT(TEXTJOIN(",",1,B2:B9),,","),
y,TOCOL(REGEXEXTRACT(x,"[^\d]+"),3),
z,VSTACK(y,FILTER(x,ISNUMBER(--x))),
GROUPBY(z,z,COUNTA,0,0))

Input Data:

Name Codes
James 1,2
Beth 2,3,4
Charlie 1,3,5
Holly 6,8,9
Sofie 1,CR3
Jimmy 2,A1,CR2
Excel 2,A2,CR4
M365 2,A3,CR5

Output Result:

Name Codes
1 3
2 5
3 2
4 1
5 1
6 1
8 1
9 1
A 3
CR 4

enter image description here

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.