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
Hari01
Frequent Visitor

How to calculate total count from a measure

I created a calculated column to count number of sites in each product and its working

 

 

Sites Count = //Number of Sites in each product
VAR str = SELECTEDVALUE(product[Site Key])
VAR str_len = LEN(str)
RETURN 
    IF(str_len>1, ((str_len - LEN(SUBSTITUTE(str, "||", "")))/2 + 1),0)

 

 

Now I have to calculate total number of sites in all product.

 

 

All Product Site Count = //Total number of Sites in all products
VAR str = SELECTEDVALUE(product[Site Key])
VAR str_len = LEN(str)
RETURN 
    CALCULATE(((str_len - LEN(SUBSTITUTE(str, "||", "")))/2 + 1), REMOVEFILTERS())

 

 

But All product site count is not giving expected output
Sample data and expected site count columns are

Product keySite keySite Count
2831
367||832
844||56||91||45||67||99       6
7 0
491||56||833

 

All product site count should return 12 for above sample data. 

someone please help here ?

8 REPLIES 8
tamerj1
Super User
Super User

Hi @Hari01 
Please refer top attached sample file with two proposed solutions

1.png2.png

Site Count 1 = 
VAR String =
    CONCATENATEX ( 
        VALUES ( 'Table'[Site key] ),
        'Table'[Site key],
        "|"
    )
VAR Items = SUBSTITUTE ( String, "||", "|" )
VAR Length = PATHLENGTH ( Items )
VAR T1 = GENERATESERIES ( 1, Length, 1 )
VAR T2 = SELECTCOLUMNS ( T1, "@SiteKey", PATHITEM ( Items, [Value] ) )
RETURN
    IF ( 
        String <> BLANK ( ),
        COUNTROWS ( T2 )
    )
Site Count 2 = 
VAR String =
    CONCATENATEX ( 
        VALUES ( 'Table'[Site key] ),
        'Table'[Site key],
        "|"
    )
VAR Items = SUBSTITUTE ( String, "||", "|" )
VAR Length = PATHLENGTH ( Items )
VAR T1 = GENERATESERIES ( 1, Length, 1 )
VAR T2 = SELECTCOLUMNS ( T1, "@SiteKey", PATHITEM ( Items, [Value] ) )
RETURN
    IF ( 
        String <> BLANK ( ),
        COUNTROWS ( DISTINCT ( T2 ) )
    )
FreemanZ
Super User
Super User

hi @Hari01 

Are you writing a measure for All Product Site Count? 

Try like:

All Product Site Count = 
SUMX(
    ALL(product[Site Key]),
    VAR str = [Site Key]
    VAR str_len = LEN(str)
    RETURN 
    IF(str_len>1, ((str_len - LEN(SUBSTITUTE(str, "||", "")))/2 + 1),0)
)

yes, I'm trying to write a measure to sum all  values of Sites Count.
Sumx is not working. Got zero

 

hi @Hari01 

strangely enought, with your data

FreemanZ_0-1675137265777.png

 

i get all count 12.

FreemanZ_2-1675137353258.png

 

 

 

Greg_Deckler
Community Champion
Community Champion

@Hari01 First, please vote for this idea: https://ideas.powerbi.com/ideas/idea/?ideaid=082203f1-594f-4ba7-ac87-bb91096c742e

This looks like a measure totals problem. Very common. See my post about it here: https://community.powerbi.com/t5/DAX-Commands-and-Tips/Dealing-with-Measure-Totals/td-p/63376

Also, this Quick Measure, Measure Totals, The Final Word should get you what you need:
https://community.powerbi.com/t5/Quick-Measures-Gallery/Measure-Totals-The-Final-Word/m-p/547907

Also: https://youtu.be/uXRriTN0cfY
And: https://youtu.be/n4TYhF2ARe8



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...
tamerj1
Super User
Super User

Hi @Hari01 

Shouldn't this be a distinct count? Seems to me 8 is more realistic than 12

I don't want distinct count. 

 

@Hari01 
Ok have you checked my solution with the sample file? I gave both options; normal count and distinct count.

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

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