Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Getting occurence of multiple values in a single column

ok so the idea of what I'm trying to do is:

-----------------------------------------

Name             animals

 

Dan                 dog,cat

doug              cat

jim                  cat,sheep,dog

 

-----------------------------------

then use that table to create this one

-----------------------------------

animals          amount

 

dog                2

cat                  3

sheep             1

------------------------------

 

I know best practice would be to put them in seperate columns but this was the data I was given

1 ACCEPTED SOLUTION
dm-p
Super User
Super User

Hi @Anonymous,

Your ideal solution is getting close, although you should have one row per animal per name for greater flexibility. You can make some changes by editing the query for your data.

I've mocked up your data and will walk-through for you.

  • From the query editor, select the animals column.
  • In the ribbon, select TransformSplit ColumnBy Delimiter
  • Ensure that comma is specified, and Each occurrence of the delimeter, e.g.:
     

    image.png
  • Click OK. You'll now get separate columns for each occurrence, e.g.:

    image.png
  • Now, we need to ensure we get the right number of records and consolidated values. Right-click on the Name column and choose Unpivot Other Columns, e.g.:

    image.png
    The reason we do other columns rather than selected columns is that later on you might have more than three animals per row, and doing it on the Name column just works on whatever's left after splitting by delimiter.
  • Now, you'll have a row per name and value, e.g.:

    image.png
  • Now, we just clean-up the artifacts. Delete the Attribute column and rename the Value column to Animal, e.g.:

    image.png
  • Now, you can Close & Apply your query and the data will be loaded.

At this point, you shoud be able to just add the Animal and a count to the report canvas, e.g.:

image.png

And you should be good to go. Good luck!

Daniel


If my post helps, then please consider accepting as a solution to help other forum members find the answer more quickly 🙂





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!


My course: Introduction to Developing Power BI Visuals


On how to ask a technical question, if you really want an answer (courtesy of SQLBI)




View solution in original post

1 REPLY 1
dm-p
Super User
Super User

Hi @Anonymous,

Your ideal solution is getting close, although you should have one row per animal per name for greater flexibility. You can make some changes by editing the query for your data.

I've mocked up your data and will walk-through for you.

  • From the query editor, select the animals column.
  • In the ribbon, select TransformSplit ColumnBy Delimiter
  • Ensure that comma is specified, and Each occurrence of the delimeter, e.g.:
     

    image.png
  • Click OK. You'll now get separate columns for each occurrence, e.g.:

    image.png
  • Now, we need to ensure we get the right number of records and consolidated values. Right-click on the Name column and choose Unpivot Other Columns, e.g.:

    image.png
    The reason we do other columns rather than selected columns is that later on you might have more than three animals per row, and doing it on the Name column just works on whatever's left after splitting by delimiter.
  • Now, you'll have a row per name and value, e.g.:

    image.png
  • Now, we just clean-up the artifacts. Delete the Attribute column and rename the Value column to Animal, e.g.:

    image.png
  • Now, you can Close & Apply your query and the data will be loaded.

At this point, you shoud be able to just add the Animal and a count to the report canvas, e.g.:

image.png

And you should be good to go. Good luck!

Daniel


If my post helps, then please consider accepting as a solution to help other forum members find the answer more quickly 🙂





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!


My course: Introduction to Developing Power BI Visuals


On how to ask a technical question, if you really want an answer (courtesy of SQLBI)




Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.