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
spandy34
Responsive Resident
Responsive Resident

DAX Commands Count

Hi  
I am just learning about DAX and was wondering if someone could help.
 
I am trying to create a measure within a table called Main Claim Data where I count the number of records (field name ClaimRef) where the field named ClassOfBusiness is OT and the field PolicyCode contains the letters REC
 
Can anyone help me please.
 
2 ACCEPTED SOLUTIONS
MarkLaf
Solution Sage
Solution Sage

The below measure works for the following sample data:

NetClassOfBusinessPolicyCode
$51.02OT123RECabc
$99.99OTZ
$121.74AZ
$21.65BY
$82.93BX
$11.13OTrecabcdef

 

 

Measure = 
CALCULATE( 
    SUM( 'Main Claim Data'[Net] ),
    'Main Claim Data'[ClassOfBusiness] = "OT",
    FILTER( 
        'Main Claim Data',
        ISNUMBER( SEARCH( "REC", 'Main Claim Data'[PolicyCode], , BLANK() ) )
    )
)

 

MarkLaf_1-1645553873165.png

Note that if the check against "REC" needs to be case-sensitive, then you'll want to use FIND instead of SEARCH.

View solution in original post

Sure can. See the below. I incorporated CONTAINSSTRING used by @goncalogeraldes as that has the same effect as ISNUMBER/SEARCH but is more readable (+1 didn't know about those functions). Also, I refreshed myself on best practices for multiple filter arguments at the following, which is why the syntax is a little different from before for the filters: https://www.sqlbi.com/articles/specifying-multiple-filter-conditions-in-calculate/

Measure2 = 
CALCULATE( 
    SUM( 'Main Claim Data'[Net] ),
    KEEPFILTERS( 'Main Claim Data'[ClassOfBusiness] = "OT" ),
    KEEPFILTERS( CONTAINSSTRING( 'Main Claim Data'[PolicyCode], "REC" ) ), 
    KEEPFILTERS( CONTAINSSTRING( 'Main Claim Data'[Reference], "ADMIN" ) )
)

Output (Measure is from previous answer, Measure2 is the new measure above):

MarkLaf_0-1645626312767.png

Sample data used:

NetClassOfBusinessPolicyCodeReference
$51.02OT123RECabcabcd
$99.99OTZefgh
$121.74AZijkl
$21.65BYadmin123
$82.93BXmnop
$11.13OTrecabcdef123admin
$55.1OTrec123SYSADMIN
$48.88OT: ) REC : (topadministrator
$73.31BX000admin0

 

As @goncalogeraldes mentioned, use CONTAINSSTRINGEXACT instead of CONTAINSSTRING if you need case-sensitivity.

 

Also, I'm assuming the new filter is another AND criteria. As long as you need AND logic on criteria, you can add each as a separate filter argument in CALCULATE. If you need OR logic, you'll have to combine (just what needs to be OR'd) in one filter argument.

View solution in original post

10 REPLIES 10
MarkLaf
Solution Sage
Solution Sage

The below measure works for the following sample data:

NetClassOfBusinessPolicyCode
$51.02OT123RECabc
$99.99OTZ
$121.74AZ
$21.65BY
$82.93BX
$11.13OTrecabcdef

 

 

Measure = 
CALCULATE( 
    SUM( 'Main Claim Data'[Net] ),
    'Main Claim Data'[ClassOfBusiness] = "OT",
    FILTER( 
        'Main Claim Data',
        ISNUMBER( SEARCH( "REC", 'Main Claim Data'[PolicyCode], , BLANK() ) )
    )
)

 

MarkLaf_1-1645553873165.png

Note that if the check against "REC" needs to be case-sensitive, then you'll want to use FIND instead of SEARCH.

spandy34
Responsive Resident
Responsive Resident

Thats brilliant - can we also include the function is the measure below where it also includes in the SUM records where the field named (Reference) contains text ADMIN in the field 

 

Test = CALCULATE( SUM( 'Main Claim Data'[Net] ), 'Main Claim Data'[ClassOfBusinessCode] = "OT", FILTER( 'Main Claim Data', ISNUMBER( SEARCH( "REC", 'Main Claim Data'[PolicyCode], , BLANK() ) ) ) )

Sure can. See the below. I incorporated CONTAINSSTRING used by @goncalogeraldes as that has the same effect as ISNUMBER/SEARCH but is more readable (+1 didn't know about those functions). Also, I refreshed myself on best practices for multiple filter arguments at the following, which is why the syntax is a little different from before for the filters: https://www.sqlbi.com/articles/specifying-multiple-filter-conditions-in-calculate/

Measure2 = 
CALCULATE( 
    SUM( 'Main Claim Data'[Net] ),
    KEEPFILTERS( 'Main Claim Data'[ClassOfBusiness] = "OT" ),
    KEEPFILTERS( CONTAINSSTRING( 'Main Claim Data'[PolicyCode], "REC" ) ), 
    KEEPFILTERS( CONTAINSSTRING( 'Main Claim Data'[Reference], "ADMIN" ) )
)

Output (Measure is from previous answer, Measure2 is the new measure above):

MarkLaf_0-1645626312767.png

Sample data used:

NetClassOfBusinessPolicyCodeReference
$51.02OT123RECabcabcd
$99.99OTZefgh
$121.74AZijkl
$21.65BYadmin123
$82.93BXmnop
$11.13OTrecabcdef123admin
$55.1OTrec123SYSADMIN
$48.88OT: ) REC : (topadministrator
$73.31BX000admin0

 

As @goncalogeraldes mentioned, use CONTAINSSTRINGEXACT instead of CONTAINSSTRING if you need case-sensitivity.

 

Also, I'm assuming the new filter is another AND criteria. As long as you need AND logic on criteria, you can add each as a separate filter argument in CALCULATE. If you need OR logic, you'll have to combine (just what needs to be OR'd) in one filter argument.

spandy34
Responsive Resident
Responsive Resident

 

Thats brilliant - thank you - 

I have the following measure but instead of calculating the amount of records Year to Date with the criteria, I want count the total ever, how can I amend to count all

Total Recoveries No Successful Recoveries Uninsured Loss =

CALCULATE (

COUNTROWS ( 'Main Claim Data' ),

FILTER (

'Main Claim Data',

'Main Claim Data'[ClassOfBusinessCode] = "OT"

&& CONTAINSSTRINGEXACT ( [PolicyCode], "REC" )),

DATESYTD('Fin_Calendar'[Date]))

@spandy34  Simply remove the FILTER() from the function. I have assumed that when you said you wanted to count records, records = rows, hence my suggestion of using COUNTROWS(). If you need to count events or IDs, use COUNT() or COUNTA() (does not count blank values) or if you need distinct values use DISTINCTCOUNT(). Anyhow, your measure can look something like:

Total Recoveries No Successful Recoveries Uninsured Loss = COUNTROWS ( 'Main Claim Data' )

For additional help, please @ me in your reply!

You can also check out my LinkedIn!

Best regards,
Gonçalo Geraldes

Hiya - but it will need to be all records where ClassofBusinessCode = OT and PolicyCode contains REC - sorry when I said all records I meant all within those parameters not matter what the date range.

Depends on exactly what you want.

 

If you just want the total count to stick in a card where the measure isn't getting filtered (except by filters from other visuals/slicers), you can just remove the DATESYTD filter as @goncalogeraldes recommended.

 

If you rather would want the total to be repeated per row in a visual (usually you would want this to use as a denominator for calculating a percentage or ratio), then the measure would be similar, but you would want to remove filters. Examples measures below:

 

Total Recoveries No SRUL_regularcount = 
CALCULATE (
    COUNTROWS ( 'Main Claim Data' ),
    KEEPFILTERS( 'Main Claim Data'[ClassOfBusinessCode] = "OT" ) ,
    KEEPFILTERS( CONTAINSSTRINGEXACT ( 'Main Claim Data'[PolicyCode], "REC" ) )
)

Total Recoveries No SRUL_rowdenominator = 
CALCULATE (
    COUNTROWS ( 'Main Claim Data' ),
    'Main Claim Data'[ClassOfBusinessCode] = "OT",
    CONTAINSSTRINGEXACT ( 'Main Claim Data'[PolicyCode], "REC" ),
    REMOVEFILTERS( 'Main Claim Data' )
)

Total Recoveries No SRUL % = 
DIVIDE( 
    [Total Recoveries No SRUL_regularcount], 
    [Total Recoveries No SRUL_rowdenominator] 
)

 

MarkLaf_0-1645632993711.png

Sample model used:

Relationships:

Fin_Calendar --1:M-> 'Main Claim Data'

'Main Claim Data':

NetPolicyCodeReferenceClassOfBusinessCodeTransactionDate
$51.02123RECabcabcdOT5/20/2021
$99.99ZefghOT3/12/2022
$121.74ZijklA1/30/2021
$21.65123RECadmin123OT11/12/2021
$82.93XmnopB8/23/2021
$11.13RECabcdef123ADMINOT2/20/2020
$55.10rec123SYSADMINOT5/20/2022
$48.88: ) REC : (topadministratorOT12/3/2022
$73.31X000ADMIN0B5/20/2021

 

Fin_Calendar:

 

Fin_Calendar =
CALENDAR( DATE( 2020, 1, 1 ), DATE( 2022, 12, 31 ) )

 

Date
1/1/2020
1/2/2020
1/3/2020
1/4/2020
1/5/2020
12/27/2022
12/28/2022
12/29/2022
12/30/2022
12/31/2022
goncalogeraldes
Super User
Super User

Hello there @spandy34 ! Check if this works:

 

Count =
CALCULATE (
    COUNTROWS ( 'Main Claim Data' ),
    /* depending on the type of count you need, you might need to use COUNTA() */
    FILTER (
        'Main Claim Data',
        'Main Claim Data'[ClassOfBusiness] = "OT"
            && CONTAINSSTRINGEXACT ( [PolicyCode], "REC" ) /* you can use CONTAINSSTRING() if not case sensitive */
    )
)

 

Hope this answer solves your problem!
If you need any additional help please @ me in your reply.
If my reply provided you with a solution, please consider marking it as a solution ✔️ or giving it a kudoe 👍
Thanks!

You can also check out my LinkedIn!

Best regards,
Gonçalo Geraldes

spandy34
Responsive Resident
Responsive Resident

@goncalogeraldes Would it be posible for you to help me with this query as this measure is to be included into your previous answer provided - the issue I have is when you have multiple criteria and one of them is contains REC as opposed to = REC

 

Thanks again for all your help

spandy34
Responsive Resident
Responsive Resident

 

 

I am trying to create a measure within a table called Main Claim Data where it calculates the sum of a field currency field called (Net)  where the field named ClassOfBusiness is OT and the field PolicyCode contains the letters REC 

 

 

 

 

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.

Top Solution Authors