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
NilR
Post Patron
Post Patron

Count Uniq ID, IF value exist anywhere in column2

I need a measure to Distinctcount([IDs]),

I need two set of filters first to check IF anywhere in [count] column the data is equal 1, contains( [Count],1)  in my date range,Then select those uniq IDs and filter them agian where [Active months] >=11 and [Flag] = y, it seems I need nested filter or something similar.

 

This is what I wrote:

 

 

 

var _Max_Date = MAX('Calendar Service'[Date])
VAR _ENR_DATE = EOMONTH(_Max_Date,-2)+1
var _Max_12_MOS = date(year(_Max_Date),Month(_Max_Date)-11,1)

VAR _gpnb = MAX(LIST[GP])
VAR _ID =
GROUPBY (
    FILTER (
        'Table',
        [Start_Date] >= _ENR_DATE
            && [Start_Date] <= _Max_Date
            && [GP] = _gpnb
    ),
    [ID]
)
VAR _count =
CALCULATETABLE (
    SUMMARIZE (
        'Table',
        [GP],
        [ID],
        [FLAG],
        [Active_months],
        "HAD_WELL", SUM ( 'Table'[Count] )
    ),
    FILTER (
        'Table',
        [GP] = _gpnb
            && [ID]
                IN _ID
                    && ( [Start_Date] >= _Max_12_MOS
                    && [Start_Date] <= _Max_Date )
                    && 'Table'[count]
                        = MAX ( [count] )
    )
)

return 
CALCULATE(SUMX(_count,IF([count]>=1,1,0)), FILTER(_count, [FLAG] = "Y" && [Active_months] >= 11 ))

 

 

 

GPIDCOUNTStart_DateActive__monthsFLAG
94212003/01/202212Y
94212002/01/202212Y
94212001/01/202212Y
94212012/01/202111Y
94212111/01/202110N
94212010/01/20219N
943000103/01/202212Y
943000102/01/202212Y
943000001/01/202212Y
943000012/01/202111Y

Result:

NilR_0-1658238665330.png

 

2 ACCEPTED SOLUTIONS
selimovd
Super User
Super User

Hey @NilR ,

 

that should be possible in one calculation.

What do you want the output to be? A calculated column that shows 1 or 0?

 

I don't have your data model, but based on your result table, the following calculated column should return the result you want:

FilterCurrentRow =
IF (
    -- Checks if for the current ID there is a Count = 1
    CALCULATE (
        COUNTROWS ( myTable ),
        myTable[COUNT] = 1,
        ALLEXCEPT ( myTable, myTable[ID] )
    ) > 0
    -- Checks if active_months >= 11
    && myTable[Active__months] >= 11
    -- checks if Flag = "Y"
    && myTable[FLAG] = "Y",
    1,
    0
)

 

The result would then look like this:

selimovd_0-1658239352996.png

 

If you need any help please let me know.
If I answered your question I would be happy if you could mark my post as a solution ✔️ and give it a thumbs up 👍

Best regards
Denis

Blog: WhatTheFact.bi
Follow me: twitter.com/DenSelimovic

View solution in original post

@NilR 
In this case, it needs to be a measure from scratch. https://we.tl/t-FqbJPF6cvc

Count Measure = 
VAR T1 = 
    ADDCOLUMNS (
        'Table',
        "@Flag",
            VAR CurrentMonth = 'Table'[Active__months]
            VAR CurrentIDTable = 
                CALCULATETABLE ( 'Table', ALLEXCEPT ( 'Table','Table'[ID] ) )
            VAR FilteredTable =
                FILTER ( CurrentIDTable, 'Table'[COUNT] = 1 )
            RETURN
                IF ( 
                    CurrentMonth >= 11 && COUNTROWS ( FilteredTable ) > 0,
                    "Y",
                    "N"
                )
    )
VAR T2 =
    SUMMARIZE (
        T1,
        'Table'[ID],
        [@Flag]
    )
VAR T3 = 
    FILTER ( T2, [@Flag] <> "N" )
RETURN
    COUNTROWS ( T3 )

 

View solution in original post

9 REPLIES 9
tamerj1
Super User
Super User

Hi @NilR 
Please try https://www.dropbox.com/t/UwGLd2yKQjN3TunS

1.png

Count Measure = 
VAR T1 =
    SUMMARIZE (
        'Table',
        'Table'[ID],
        'Table'[FLAG]
    )
VAR T2 = 
    FILTER ( T1, 'Table'[FLAG] <> "N" )
RETURN
    COUNTROWS ( T2 )

Thanks @tamerj1 !
I also need to test [count] column first to make sure it is included value 1 before I do the count and test for flag. My problem is that the value does not align to flag column all the time so I have to check if this value ever existed anywhere in this column in this selected date range, now check those IDs for Flag = Yes and then finally count them.

@NilR 
So basically you need to recreate the flag column but with correct results. I believe this is the simplest approach. What do you think?

@NilR 
Here is my proposed solution. https://www.dropbox.com/t/ORfzxAxCo32Cb3Sk

1.png2.png

FLAG NEW = 
VAR CurrentMonth = 'Table'[Active__months]
VAR CurrentIDTable = 
    CALCULATETABLE ( 'Table', ALLEXCEPT ( 'Table','Table'[ID] ) )
VAR FilteredTable =
    FILTER ( CurrentIDTable, 'Table'[COUNT] = 1 )
RETURN
    IF ( 
        CurrentMonth >= 11 && COUNTROWS ( FilteredTable ) > 0,
        "Y",
        "N"
    )
Count Measure = 
VAR T1 =
    SUMMARIZE (
        'Table',
        'Table'[ID],
        'Table'[FLAG NEW]
    )
VAR T2 = 
    FILTER ( T1, 'Table'[FLAG NEW] <> "N" )
RETURN
    COUNTROWS ( T2 )

Thanks @tamerj1 ! what I am stuck is if the selected range is different and count is not equal to 1 then the new flag should be N. lets say the count on Jan 11, 2021 is 0 and Jan 10 2021 on your data set is 1. if my selection is from Jan 11,2021 then I do not have anywhere on my date range count = 1 and my new flag should be N.

@NilR 
In this case, it needs to be a measure from scratch. https://we.tl/t-FqbJPF6cvc

Count Measure = 
VAR T1 = 
    ADDCOLUMNS (
        'Table',
        "@Flag",
            VAR CurrentMonth = 'Table'[Active__months]
            VAR CurrentIDTable = 
                CALCULATETABLE ( 'Table', ALLEXCEPT ( 'Table','Table'[ID] ) )
            VAR FilteredTable =
                FILTER ( CurrentIDTable, 'Table'[COUNT] = 1 )
            RETURN
                IF ( 
                    CurrentMonth >= 11 && COUNTROWS ( FilteredTable ) > 0,
                    "Y",
                    "N"
                )
    )
VAR T2 =
    SUMMARIZE (
        T1,
        'Table'[ID],
        [@Flag]
    )
VAR T3 = 
    FILTER ( T2, [@Flag] <> "N" )
RETURN
    COUNTROWS ( T3 )

 

Thank you @tamerj1 !

selimovd
Super User
Super User

Hey @NilR ,

 

that should be possible in one calculation.

What do you want the output to be? A calculated column that shows 1 or 0?

 

I don't have your data model, but based on your result table, the following calculated column should return the result you want:

FilterCurrentRow =
IF (
    -- Checks if for the current ID there is a Count = 1
    CALCULATE (
        COUNTROWS ( myTable ),
        myTable[COUNT] = 1,
        ALLEXCEPT ( myTable, myTable[ID] )
    ) > 0
    -- Checks if active_months >= 11
    && myTable[Active__months] >= 11
    -- checks if Flag = "Y"
    && myTable[FLAG] = "Y",
    1,
    0
)

 

The result would then look like this:

selimovd_0-1658239352996.png

 

If you need any help please let me know.
If I answered your question I would be happy if you could mark my post as a solution ✔️ and give it a thumbs up 👍

Best regards
Denis

Blog: WhatTheFact.bi
Follow me: twitter.com/DenSelimovic

@selimovd Thank You!

I am very new to PBI and I was wondering how can I convert this to measure to also add some more dynamic filters?

[ID] IN _ID && ([Start_Date] >= _Max_12_MOS
            && [Start_Date] <= _Max_Date)

 

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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