Count items within a cell

Looking for a formula that can count the number of items in a cell, that are separated by commas and I am stumped. So in the example below, row 2 has 3 items, row 5 has 0, the other rows have XX items. Any suggestions?

Best Answer

  • GrantSmith
    GrantSmith Coach
    Answer ✓
    CASE WHEN `field` = '[]' THEN 0
    ELSE (LENGTH(`field`) - LENGTH(REPLACE(`field`, ',', ''))) + 1
    

    Some string manipulation options to assist.

    If it's an empty array list, return 0, otherwise determine the length of the string and subtract the length of the string with the commas removed (count the number of commas). Add one because it's always one more field than the number of commas.

    **Was this post helpful? Click Agree or Like below**
    **Did this solve your problem? Accept it as a solution!**

Answers

  • GrantSmith
    GrantSmith Coach
    Answer ✓
    CASE WHEN `field` = '[]' THEN 0
    ELSE (LENGTH(`field`) - LENGTH(REPLACE(`field`, ',', ''))) + 1
    

    Some string manipulation options to assist.

    If it's an empty array list, return 0, otherwise determine the length of the string and subtract the length of the string with the commas removed (count the number of commas). Add one because it's always one more field than the number of commas.

    **Was this post helpful? Click Agree or Like below**
    **Did this solve your problem? Accept it as a solution!**