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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
russm
Helper I
Helper I

Counting unique text occurrences across multiple columns?

I am new to DAX and to Power BI. I’m working with an existing qualitative data set and I need to count the occurrences of unique text values across multiple columns. The existing data set used a controlled vocabulary based on a simple dictionary table.

 

In my data, I have several thousand records sort of like this:

 

Comment

Comment_Code_01

Comment_Code_02

Comment_Code_03

Comment_Code_04

I like things that fly but not things that crawl. If it has scales, I don't mind if it swims but I don’t want anything to do with it if had legs.

Likes birds

Hates bugs

Likes fish

Hates reptiles

I don’t like things that fly but I do like things that crawl.     If it has scales, I don’t mind if it has legs but I can't stand it if it swims.

Hates birds

Likes bugs

Hates fish

OK with reptiles

I don’t like anything living but I do like wood carvings of things that crawl.

Hates all creatures

Likes facsimiles of creatures

  

I only like birds. Well, I kind of like beetles too.

Likes birds

OK with some bugs

  

I hate everything living except for fish.

Only likes fish

   

 

The dictionary is about 900 records and looks something like this:

 

Comment_Code

Sentiment

Category

Likes Birds

Positive

Birds

Hates birds

Negative

Birds

Hates all creatures

Negative

Global

Only likes fish

Positive

Fish

Hates bugs

Negative

Insects

Likes bugs

Positive

Insects

Likes facsimiles of creatures

Positive

Nonliving

OK with some bugs

Neutral

Insects

Likes fish

Positive

Fish

Hates fish

Negative

Fish

Hates reptiles

Neutral

Reptiles

OK with reptiles

Positive

Reptiles

 

I need to produce a report that looks something like:

 

Theme

Count

Likes Birds

2

Hates birds

1

Hates all creatures

1

Only likes fish

1

Hates bugs

1

Likes bugs

1

Likes facsimiles of creatures

1

OK with some bugs

1

Likes fish

1

Hates fish

1

Hates reptiles

1

OK with reptiles

1

 

This was trivial in Excel using COUNTIF but I haven’t been able to work out a way to do this with DAX in Power BI. Is there a way to look at the array of Comment_Code_01: Comment_Code_04 and generate a table of all unique text values along with their frequencies of occurrence?

1 ACCEPTED SOLUTION
ImkeF
Super User
Super User

You do this in this query editor:

 

1) Load your data-table

2) Check column "Comment"

3) Rightclick your mouse: "Unpivot Other Columns"

4) Check column "Value"

5) Rightclick mouse: "Group By" and just click "OK" (don't change the defaults here)

 

This will deliver your table below. No need for the lookuptable so far. (But you can join it if you want to show the other values as well. The operation is called "Merge" in the PBI menu)

 

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

View solution in original post

3 REPLIES 3
ImkeF
Super User
Super User

You do this in this query editor:

 

1) Load your data-table

2) Check column "Comment"

3) Rightclick your mouse: "Unpivot Other Columns"

4) Check column "Value"

5) Rightclick mouse: "Group By" and just click "OK" (don't change the defaults here)

 

This will deliver your table below. No need for the lookuptable so far. (But you can join it if you want to show the other values as well. The operation is called "Merge" in the PBI menu)

 

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Do you know how to get a count based upon a concatenation of several columns without using the Group By function in Query Editor? I'm trying to do a count based upon 3 columns in a table but need to have the flexibility to show other columns associated with the data.  See my examples below for clarification.  I am very new to Power Bi and spinning my wheels on this one.

 

Table      
NameRaceGenderCollege GraduateHigh School GraduateGED 
FrankWMNYN 
FrankWMYYN 
AliceWFYYN 
AliceWFNNY 
JohnWMNYN 
JohnBMYYN 
       
View if all columns are displayed   
NameRaceGenderCollege GraduateHigh School GraduateGEDCount of Name, Race, Gender
FrankWMNYN1
FrankWMYYN1
AliceWFYYN1
AliceWFNNY1
JohnWMNYN1
JohnBMYYN1
       
View if first three columns are displayed   
NameRaceGenderCount of Name, Race, Gender   
FrankWM2   
AliceWF2   
JohnWM1   
JohnBM1   

Thanks, Imke!

 

That was just the nudge in the direction that I needed.  

 

My actual dataset has loads of columns on either side of the array that's similar to what I shared above.  I ended up multiselecting all of the Comment_Code_n columns and used Unpivot to flatten them. Then I related the resulting Values column with Comment_Code in the Dictionary table which gave me that counts that I needed.  Though it wasn't part of the stated problem, I was able to use a discrete row identifier to get distinct counts of unique respondents too.

 

Thanks again for your help!

 

- Russ

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.