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

Score big with last-minute savings on the final tickets to FabCon Vienna. Secure your discount

Reply
Anonymous
Not applicable

Measure to find every occurrence of a string, except the first

Hi folks, I'm hoping someone can assist.

I have a table of data (example below), that contains some Reference Numbers, as well as audit log entries relating to each RefNum, and datestamps for when those audit log entries occurred. Not that the data may appear in any order:

RefNumActivityTypeDateCreatedAnalyst
1Opened01/09/2019 10:00amJim
1Comment01/09/2019 10:01amJim
1Search01/09/2019 10:02amJim
1Search01/09/2019 10:03amBarry
2Opened01/09/2019 10:04amBarry
2Comment01/09/2019 10:05amBarry
2Comment01/09/2019 10:06amBarry
2Search01/09/2019 10:07amJim
3Opened01/09/2019 10:08amJane
3Comment01/09/2019 10:09amSally
3Comment01/09/2019 10:10amJane
3Comment01/09/2019 10:11amJane
4Opened01/09/2019 10:12amJane
4Search01/09/2019 10:13amSally
4Comment01/09/2019 10:14amJane
4Search01/09/2019 10:15amJane

 

I want to be able to flag ONLY the first occurrence in each RefNum of "Search", based on the earliest DateCreated. A kind user put together the measure below for me:

 

 

Flag = 
IF (
    (
        'Table'[DateCreated]
            = CALCULATE (
                MIN ( 'Table'[DateCreated] ),
                ALLEXCEPT ( 'Table', 'Table'[Analyst], 'Table'[RefNum] )
            )
    )
        && (
            CALCULATE (
                MIN ( 'Table'[DateCreated] ),
                ALLEXCEPT ( 'Table', 'Table'[Analyst], 'Table'[RefNum] )
            )
                = CALCULATE (
                    MIN ( 'Table'[DateCreated] ),
                    FILTER (
                        ALLEXCEPT ( 'Table', 'Table'[Analyst], 'Table'[RefNum] ),
                        'Table'[ActivityType] = "Search"
                    )
                )
        ),
    "Not Count"
)

 

 

Which gives me the results below. I'm actually fine with it giving me 'Not Count', as I can use Power Query to fill in the blanks with a 'Count' string and give me the result I need (this is also why I've set the title of this request as such). But it's not currently flagging the right lines with 'Not Count', which is what I need help fixing. The 'X' represent correct incorrect flags of 'Not Count', and the ticks represent fields where 'Not Count' should appear:

Capture(1).PNG

 

Any help would really be appreciated!

1 ACCEPTED SOLUTION
JarroVGIT
Resident Rockstar
Resident Rockstar

Well, based on your example table, I have created the following calculated column:

FirstSearch = 
    VAR currentActivityType = [ActivityType]
    VAR currentRefNum = [RefNum]
    VAR rank1 = RANKX(FILTER('Table', 'Table'[RefNum] = currentRefNum && 'Table'[ActivityType] = currentActivityType), 'Table'[DateCreated],,ASC,Dense)

    RETURN
        IF((rank1=1 && currentActivityType = "Search"), TRUE, FALSE)

What is happening here is the following. For each row this is evaluated, so variable currentActivityType and currentRefNum are just referencing other columns of the row we are adding a new column to. The rank1 (rank is a reserved word) variable calculates what n-th time this row is based on refnum, sort by date for that activity type. The IF-statement checks if the rank=1 (it is the first time this type of activity is occured in the current refNum) and if the activitytype is Search. 

This results in the desired output:

image.png

 

Kind regards

Djerro123

-------------------------------

If this answered your question, please mark it as the Solution. This also helps others to find what they are looking for.

Kudo's are welcome 🙂





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




View solution in original post

7 REPLIES 7
az38
Community Champion
Community Champion

@Anonymous 

how are you going to "use Power Query to fill in the blanks with a 'Count' string" if your column/measure is created in DAX-mode? its impossible. PowerQuery is being used only for transform data source, before implement business logic

do not hesitate to give a kudo to useful posts and mark solutions as solution
Linkedin


do not hesitate to give a kudo to useful posts and mark solutions as solution
LinkedIn
Anonymous
Not applicable

Good point @az38  - I would likely have needed to create some sort of calculated column instead (and I would probably have wasted 30 mins of my life trying to figure out why I couldn't do it in Power Query, if you hadn't replied) 🙂

JarroVGIT
Resident Rockstar
Resident Rockstar

Well, based on your example table, I have created the following calculated column:

FirstSearch = 
    VAR currentActivityType = [ActivityType]
    VAR currentRefNum = [RefNum]
    VAR rank1 = RANKX(FILTER('Table', 'Table'[RefNum] = currentRefNum && 'Table'[ActivityType] = currentActivityType), 'Table'[DateCreated],,ASC,Dense)

    RETURN
        IF((rank1=1 && currentActivityType = "Search"), TRUE, FALSE)

What is happening here is the following. For each row this is evaluated, so variable currentActivityType and currentRefNum are just referencing other columns of the row we are adding a new column to. The rank1 (rank is a reserved word) variable calculates what n-th time this row is based on refnum, sort by date for that activity type. The IF-statement checks if the rank=1 (it is the first time this type of activity is occured in the current refNum) and if the activitytype is Search. 

This results in the desired output:

image.png

 

Kind regards

Djerro123

-------------------------------

If this answered your question, please mark it as the Solution. This also helps others to find what they are looking for.

Kudo's are welcome 🙂





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Anonymous
Not applicable

Thanks for the solution and the explanation @JarroVGIT , I really appreciate it 🙂

No sweat, just don't forget to mark it as the solution so other with similar questions can find it easily 🙂

Kind regards

Djerro123

-------------------------------

If this answered your question, please mark it as the Solution. This also helps others to find what they are looking for.

Kudo's are welcome 🙂





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




TomMartens
Super User
Super User

Hey @Anonymous 

 

maybe you want to try this DAX creating a Calculated Column:_

is 1st search = 
var firstSearch = CALCULATE(MIN('Table2'[DateCreated]) , ALLEXCEPT('Table2' , Table2[RefNum]) , 'Table2'[ActivityType] = "Search")
var _refnum = 'Table2'[RefNum]
return
if(AND('Table2'[DateCreated] = firstSearch , 'Table2'[RefNum] = _refnum) , "yes" , "no") 

 

Hopefully this provides what you are looking for.

 

Regards,

Tom 



Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany
Anonymous
Not applicable

Thanks very much @TomMartens , I really appreciate you taking the time to provide a respose here 🙂

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.