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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
Anonymous
Not applicable

Count occurrences of value in specific columns

Lets say I have a table that shows which businesses are currently open in which cities:

 

CityZooSpaArt MuseumScience MuseumHistory MuseumPark
El PasoOPENCLOSECLOSECLOSENACLOSE
AnchorageCLOSECLOSEOPENOPENNACLOSE
San FranciscoOPENCLOSECLOSECLOSENANA
ClevelandOPENCLOSEOPENCLOSEOPENNA
BostonCLOSECLOSECLOSECLOSECLOSECLOSE
AtlantaOPENOPENCLOSENANANA
HonoluluOPENOPENNACLOSENACLOSE
DallasCLOSEOPENCLOSEOPENNACLOSE

 

Lets imagine that I want to add two DAX Calculated columns with the following information:

- Desired Column 1: Count of Open businesses (a count of the value "OPEN" in the columns Zoo, Spa, Art Museum, Science Museum, History Museum, Park for each particular city)

- Desired Column 2: List of Open Businesses (lists the column names on this row that have the value "OPEN")

 

CityZooSpaArt MuseumScience MuseumHistory MuseumParkDesired Column: Count of Open BusinessesDesired Column 2: List of Open Businesses
El PasoOPENCLOSECLOSECLOSENACLOSE1Zoo
AnchorageCLOSECLOSEOPENOPENNACLOSE2Art Museum, Science Museum
San FranciscoOPENCLOSECLOSECLOSENANA1Zoo
ClevelandOPENCLOSEOPENCLOSEOPENNA3Zoo, Art Museum, History Museum
BostonCLOSECLOSECLOSECLOSECLOSECLOSE0None
AtlantaOPENOPENCLOSENANANA2Zoo, Spa
HonoluluOPENOPENNACLOSENACLOSE2Zoo, Spa
DallasCLOSEOPENCLOSEOPENNACLOSE2Spa, Science Museum

 

I am able to do this in Microsoft Excel but I'm not sure if this is possible with DAX. 

1 ACCEPTED SOLUTION
tamerj1
Super User
Super User

Hi @Anonymous 

please try the following 

Count of Open businesses =
VAR Businesses = {
( "Zoo", [Zoo] ),
( "Art", [Art] ),
( "Museum", [ Museum] ),
( "Science", [Science] ),
( "Spa", [Spa] ),
( "History", [History] ),
( "Park", [Park] )
}
RETURN
COUNTROWS ( FILTER ( Businesses, [Value2] = "Open" ) )

List of Open businesses =
VAR Businesses = {
( "Zoo", [Zoo] ),
( "Art", [Art] ),
( "Museum", [ Museum] ),
( "Science", [Science] ),
( "Spa", [Spa] ),
( "History", [History] ),
( "Park", [Park] )
}
RETURN
CONCATENATEX (
FILTER ( Businesses, [Value2] = "Open" ),
[Value1],
UNICHAR ( 10 )
)

 

View solution in original post

5 REPLIES 5
tamerj1
Super User
Super User

Hi @Anonymous 

please try the following 

Count of Open businesses =
VAR Businesses = {
( "Zoo", [Zoo] ),
( "Art", [Art] ),
( "Museum", [ Museum] ),
( "Science", [Science] ),
( "Spa", [Spa] ),
( "History", [History] ),
( "Park", [Park] )
}
RETURN
COUNTROWS ( FILTER ( Businesses, [Value2] = "Open" ) )

List of Open businesses =
VAR Businesses = {
( "Zoo", [Zoo] ),
( "Art", [Art] ),
( "Museum", [ Museum] ),
( "Science", [Science] ),
( "Spa", [Spa] ),
( "History", [History] ),
( "Park", [Park] )
}
RETURN
CONCATENATEX (
FILTER ( Businesses, [Value2] = "Open" ),
[Value1],
UNICHAR ( 10 )
)

 

Anonymous
Not applicable

Thank you! This works if I add these codes as calculated columns. This was what I ended up using:

 

Open businesses count =
VAR Businesses = {
( "Zoo", [Zoo] ),
( "Art", [Art Museum] ),
( "Science", [Science Museum] ),
( "Spa", [Spa] ),
( "History", [History Museum] ),
( "Park", [Park] )
}
RETURN
COUNTROWS ( FILTER ( Businesses, [Value2] = "Open" ) )

Open businesses list =
VAR Businesses = {
( "Zoo", [Zoo] ),
( "Art", [Art Museum] ),
( "Science", [Science Museum] ),
( "Spa", [Spa] ),
( "History", [History Museum] ),
( "Park", [Park] )
}
RETURN
CONCATENATEX (
FILTER ( Businesses, [Value2] = "Open" ),
[Value1],
", "
)
 
I am really excited that you can use variables in this way.
 
To make sure I understand what you did for each function:
 
1) You created a variable and you created a virtual DAX table to store within that variable. When creating the new table, you turned each column into a row using DAX table creation syntax. 
 
2) For the Return, you then used a function to count the number of "Open" values or to grab the name of the columns that had open values.
 
This is my screenshot of the resulting columns for anyone who is curious:
powerquest1234_1-1663158515743.png

 

I'm amazed that Power BI/DAX is able to count the correct amounts for each city rows, because the virtual tables don't seem to store the city information and the return doesn't seem to reference the original table that has the city information (unless they do on the back-end?).

 

 
 

@Anonymous 
What you have stated above is 100% correct.

Regarding your last paragraph, When creating a calculated column there is an active row context in the background. So for each row or the table we are creating a two column virtual table that contains the [Business Type] and [Status]. In other words, the DAX code actually unpivots the table for each row.

 

Just noticed that the formula requires fine tuning. The following formula orders the businesses in ascending order

Open businesses list =
VAR Businesses = {
    ( "Zoo", [Zoo] ),
    ( "Art", [Art Museum] ),
    ( "Science", [Science Museum] ),
    ( "Spa", [Spa] ),
    ( "History", [History Museum] ),
    ( "Park", [Park] )
}
RETURN
    CONCATENATEX (
        FILTER ( Businesses, [Value2] = "Open" ),
        [Value1],
        ", ",
        [Value1], ASC
    )
Jihwan_Kim
Super User
Super User

Hi,

If you can use Power Query Editor, I suggest unpivotting the table like below, load it, and then create a new table by using DAX formula.

Jihwan_Kim_1-1663126121258.png

 

 

Jihwan_Kim_0-1663126104761.png

 

New Table = 
ADDCOLUMNS (
    VALUES ( Data[City] ),
    "@Count of Open Business",
        CALCULATE ( COUNTROWS ( FILTER ( Data, Data[Status] = "OPEN" ) ) ) + 0,
    "@List of Open Business",
        VAR _result =
            CALCULATE (
                CONCATENATEX ( FILTER ( Data, Data[Status] = "OPEN" ), Data[Category], ", " )
            )
        RETURN
            IF ( _result = BLANK (), "None", _result )
)

 


If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.
Anonymous
Not applicable

Thank you so much for your response. I do think this would work as a solution but unfortunately I don't think I would be allowed to alter the source table in power query since multiple reports use this dataset. I should have mentioned this in the initial post and I apologize.

Helpful resources

Announcements
July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 community update carousel

Fabric Community Update - June 2025

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