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

Count( if column value = string)

Hi,
Having trouble with DAX syntax in if statements for checking if a column's string value equals a certain value.

 

I want to do some calculations using different counts of strings, such as:

COUNT(IF(Table[Location] = "In Storage 1", 1, 0)) - COUNT(IF(Table[Location] = "Out Storage 1", 1, 0))

 

DateLocation
14/04/2019In Storage 1
14/04/2019In Storage 2
14/04/2019Out Storage 1
14/04/2019Out Storage 2
14/04/2019In Storage 1
14/04/2019In Storage 2
14/04/2019Out Storage 1
14/04/2019Out Storage 2
1 ACCEPTED SOLUTION
parry2k
Super User
Super User

@Anonymous add new measure with following expression

 

MyBalanceCount = 
CALCULATE( COUNTROWS( MyTable ), MyTable[Location] = "In Storage" ) -
CALCULATE( COUNTROWS( MyTable ), MyTable[Location] = "Out Storage" )


Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

View solution in original post

5 REPLIES 5
parry2k
Super User
Super User

@Hamza_Meknassi you can do this too:

 

del =
CALCULATE (
         COUNTROWS(dashboard ), 
         dashboard[code] IN { "RES", "DES" } 
)
 

 

Subscribe to my youtube channel https://youtube.com/@PowerBIHowTo



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Adamboer
Responsive Resident
Responsive Resident

Hello,

The syntax for checking if a column's string value equals a certain value in DAX is to use the "=" operator inside the IF statement. Here's the correct syntax for your calculation:

COUNT(IF(Table[Location] = "In Storage 1", 1, BLANK())) - COUNT(IF(Table[Location] = "Out Storage 1", 1, BLANK()))

Note that the BLANK() function should be used instead of 0 in the IF statement to avoid incorrect results. Also, if you want to include all rows that don't match the conditions, you can use the COUNTROWS function outside the IF statements, like this:

COUNTROWS(Table) - COUNT(IF(Table[Location] = "In Storage 1", 1, BLANK())) - COUNT(IF(Table[Location] = "Out Storage 1", 1, BLANK()))

This will give you the count of rows that don't match the conditions.

I hope this helps! Let me know if you have any further questions.

suhasakole
Frequent Visitor

Hi Ben,

Along with the above approach, you can try the following creating 2 calculated measure and then use those in creating a final measure. Hope this might help you.

CntRowsInStorage = CALCULATETABLE(ROW("rows",COUNT(Table[Location])),FILTER(ALL(Table[Location]),SEARCH("In Storage",Table1[Text],1,0)))

CntRowsOutStorage = CALCULATETABLE(ROW("rows",COUNT(Table[Location])),FILTER(ALL(Table[Location]),SEARCH("Out Storage",Table1[Text],1,0)))

FinalCount = CntRowsInStorage - CntRowsOutStorage

parry2k
Super User
Super User

@Anonymous add new measure with following expression

 

MyBalanceCount = 
CALCULATE( COUNTROWS( MyTable ), MyTable[Location] = "In Storage" ) -
CALCULATE( COUNTROWS( MyTable ), MyTable[Location] = "Out Storage" )


Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

can we have two calculate in the same DAX query ? in my case, i have a syntaxe error !

del =
CALCULATE (
COUNTROWS(dashboard ), dashboard[code] = "RES"
) + CALCULATE (
COUNTROWS(dashboard ), dashboard[code] = "DES"
)

 

 

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.