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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
tc1311
Regular Visitor

Measure for counting strings that belong to different groups

Need help to create a measure that can count the number of entities getting impacted under 2 different regions. My data table looks like below:

EntitiesRegion
A / BEMEA
CEMEA
C / DEMEA
C / DEMEA
A / B / C / DEMEA

There is further segregation of EMEA into UK and EU wherein:

UKGermany
AC
BD

My chart should give a count of 2 entities for UK and 4 entities for Germany. Please guide.

1 ACCEPTED SOLUTION

Hi @tc1311 ,

If you want Year appears in the columns and Regions in rows, then you can't use calculated column, you can only use measure, and it becomes cumbersome to achieve your desired results.
First you need to create a table like this:

vjunyantmsft_0-1715917677475.png

Build the relationships:

vjunyantmsft_1-1715917696380.png

Use these DAXs to create measures:

EntitiesContain_UKCount = 
SUMX(
    Table1,
    IF(
        NOT ISBLANK(Table1[Entities]),
        IF(
            CALCULATE(
                COUNTROWS(Table2),
                FILTER(
                    ALL(Table2),
                    CONTAINSSTRING(Table1[Entities], Table2[UK])
                )
            ) > 0,
            1,
            0
        ),
        0
    )
)
EntitiesContain_GermanyCount = 
SUMX(
    Table1,
    IF(
        NOT ISBLANK(Table1[Entities]),
        IF(
            CALCULATE(
                COUNTROWS(Table2),
                FILTER(
                    ALL(Table2),
                    CONTAINSSTRING(Table1[Entities], Table2[Germany])
                )
            ) > 0,
            1,
            0
        ),
        0
    )
)
Measure = 
SWITCH(
    TRUE(),
    SELECTEDVALUE('Table'[Country]) = "UK", 'Table1'[EntitiesContain_UKCount],
    SELECTEDVALUE('Table'[Country]) = "Germany", 'Table1'[EntitiesContain_GermanyCount]
)

Then build the matrix like this:

vjunyantmsft_2-1715917784441.png

 

In addition, after testing, the DAX provided before has no problems in my test environment. It is indeed possible that some relationships in your data model cause your DAX to return incorrect results. Please try to delete the relationship or make the relationship Inactive.

Best Regards,
Dino Tao
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

5 REPLIES 5
tc1311
Regular Visitor

Hi Dino,

Thank you! This is exactly what I was looking for. However, it would have been nicer if the table could be transposed that is Year appears in the columns and Regions in rows.

Another bizarre thing is .. while this code works absolutey fine for calculating UK but the count of German is returning 1 for all the entities and 0 only where there are no entities at all. Could it be something related to relationships?

Please help.

Hi @tc1311 ,

If you want Year appears in the columns and Regions in rows, then you can't use calculated column, you can only use measure, and it becomes cumbersome to achieve your desired results.
First you need to create a table like this:

vjunyantmsft_0-1715917677475.png

Build the relationships:

vjunyantmsft_1-1715917696380.png

Use these DAXs to create measures:

EntitiesContain_UKCount = 
SUMX(
    Table1,
    IF(
        NOT ISBLANK(Table1[Entities]),
        IF(
            CALCULATE(
                COUNTROWS(Table2),
                FILTER(
                    ALL(Table2),
                    CONTAINSSTRING(Table1[Entities], Table2[UK])
                )
            ) > 0,
            1,
            0
        ),
        0
    )
)
EntitiesContain_GermanyCount = 
SUMX(
    Table1,
    IF(
        NOT ISBLANK(Table1[Entities]),
        IF(
            CALCULATE(
                COUNTROWS(Table2),
                FILTER(
                    ALL(Table2),
                    CONTAINSSTRING(Table1[Entities], Table2[Germany])
                )
            ) > 0,
            1,
            0
        ),
        0
    )
)
Measure = 
SWITCH(
    TRUE(),
    SELECTEDVALUE('Table'[Country]) = "UK", 'Table1'[EntitiesContain_UKCount],
    SELECTEDVALUE('Table'[Country]) = "Germany", 'Table1'[EntitiesContain_GermanyCount]
)

Then build the matrix like this:

vjunyantmsft_2-1715917784441.png

 

In addition, after testing, the DAX provided before has no problems in my test environment. It is indeed possible that some relationships in your data model cause your DAX to return incorrect results. Please try to delete the relationship or make the relationship Inactive.

Best Regards,
Dino Tao
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

v-junyant-msft
Community Support
Community Support

Hi @tc1311 ,

Please try these DAXs:

EntitiesContain_UKCount = 
SUMX(
    Table1,
    IF(
        NOT ISBLANK(Table1[Entities]),
        IF(
            CALCULATE(
                COUNTROWS(Table2),
                FILTER(
                    ALL(Table2),
                    CONTAINSSTRING(Table1[Entities], Table2[UK])
                )
            ) > 0,
            1,
            0
        ),
        0
    )
)
EntitiesContain_GermanyCount = 
SUMX(
    Table1,
    IF(
        NOT ISBLANK(Table1[Entities]),
        IF(
            CALCULATE(
                COUNTROWS(Table2),
                FILTER(
                    ALL(Table2),
                    CONTAINSSTRING(Table1[Entities], Table2[Germany])
                )
            ) > 0,
            1,
            0
        ),
        0
    )
)

And the final output is as below:

vjunyantmsft_0-1715649630920.png


Best Regards,
Dino Tao
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Hi Dino,

Thanks for the solution. It gives the correct count using card but I want to use Matrix to show year-wise split. For that I tried to add conditional column to my data set to include UK and Germany whereever the text contains entities as per my second table. However, it didn't work. Can you please help with this problem too?

Hi @tc1311 ,

I'm sorry I'm not sure I'm understanding you correctly?
You mentioned year-wise split, so I updated the test dataset a bit:

vjunyantmsft_0-1715751825511.png

And you mentioned add conditional column, so I use these DAXs to create two calculated columns:

UK = 
IF(
    CALCULATE(
        COUNTROWS(Table2),
        FILTER(
            ALL(Table2),
            CONTAINSSTRING(Table1[Entities], Table2[UK])
        )
    ) > 0,
    1,
    0
)
Germany = 
IF(
    CALCULATE(
        COUNTROWS(Table2),
        FILTER(
            ALL(Table2),
            CONTAINSSTRING(Table1[Entities], Table2[Germany])
        )
    ) > 0,
    1,
    0
)

vjunyantmsft_1-1715751913157.png

And since you want to use Matrix, so I build the Matrix like below:

vjunyantmsft_2-1715751965558.png

Is this what you want? I do not quite understand what you are looking for, if this is not what you are looking for, could you explain your desired outcome in more detail or show your desired outcome in the form of sample data?

Best Regards,
Dino Tao
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel

Power BI Monthly Update - May 2024

Check out the May 2024 Power BI update to learn about new features.

LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.