cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
rabiafarooq
Helper I
Helper I

first occurrence of a value.

I have a table called Pharmacy_Issuance, in which i have to calculate the number of prescriptions for new or refills for certain medicines. So what i want to do is if the medical record number[MRN] occurs for the first time in column its for new medicine else its a refill. i want to add two calculated column, if new then 1 else 0 and same for refills. please guide.

1 ACCEPTED SOLUTION
v-yulgu-msft
Microsoft
Microsoft

Hi @rabiafarooq,

 

Add an index column first. Then, create a new column like below:

first occurrence check =
IF (
    CALCULATE (
        COUNTROWS ( Test1 ),
        FILTER (
            ALLEXCEPT ( Test1, Test1[MRN] ),
            Test1[Index] <= EARLIER ( Test1[Index] )
        )
    )
        > 1,
    0,
    1
)

1.PNG

 

However, if there is a date column in your dataset, there is no need to add index column, you can directly refer to date field in above formula.

 

Best regards,

Yuliana Gu

Community Support Team _ Yuliana Gu
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

11 REPLIES 11
v-yulgu-msft
Microsoft
Microsoft

Hi @rabiafarooq,

 

Add an index column first. Then, create a new column like below:

first occurrence check =
IF (
    CALCULATE (
        COUNTROWS ( Test1 ),
        FILTER (
            ALLEXCEPT ( Test1, Test1[MRN] ),
            Test1[Index] <= EARLIER ( Test1[Index] )
        )
    )
        > 1,
    0,
    1
)

1.PNG

 

However, if there is a date column in your dataset, there is no need to add index column, you can directly refer to date field in above formula.

 

Best regards,

Yuliana Gu

Community Support Team _ Yuliana Gu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Hi Yuliana, 

Would you be able to help turn this formula so that it counts the number of occurance? For example, I'd want the second occurence of HIR to say 2 or the third occurance of DTA to say 3 for each employee ID. 

 

cgallegos_0-1643814530534.png

 

Occurence = IF(CALCULATE(COUNTROWS('Records'),FILTER(ALLEXCEPT('Records','Records'[Empl ID],'Records'[Action]),
VALUE('Records'[Effective Date - Job Dta])<=VALUE(EARLIER('Records'[Effective Date - Job Dta]))))>1,0,1)

 

Thank you in advance!!

Thank you for this. I have the same question. However "EARLIER" does not work when I try to use it.  Have no idea what I do wrong...I read on the internet that "EARLIER" is not recommended... Is there an alternative way to answer the same question (showing the first occurence of a number in a table). Thank you! 

first occurrence check =
IF (
    CALCULATE (
        COUNTROWS ( Test1 ),
        FILTER (
            ALLEXCEPT ( Test1, Test1[MRN] ),
            Test1[Index] <= EARLIER ( Test1[Index] )
        )
    )
        > 1,
    0,
    1
)

 

Anonymous
Not applicable

How can I dynamically check the first occurence when i change the date values in a slicer

Hi,

Share some data, describe the question and show the expected result.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
Anonymous
Not applicable

Hi,

I need to create a dynamic column where the first occurance/flag column changes based on the slicer date range. Here's the sample data

Blessy154_0-1624855509821.png

So, if I select the data range from March,2019 to August,2019 the resulted column should have only 4 rows and their first occurance flaged 1.

 

Hi,

Assuming the slicer is from the Calendar Table and the Dates in the Table visual are from the Data Table, try this measure

=if(startofmonth(Data[Date])=min(calendar[date]),1,0)


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
Anonymous
Not applicable

Thankyou, this measure works. But, is there a way that i can optimise by using the slicer from the Data Table?.

Because if I use the calender table, the resulted table is having six rows and I want the rows from the selected range only

Blessy154_0-1624937774505.png

 

 

Hi,

Create a relationship from the Date column of the Data Table to the Date column of the Calendar Table.  You must create a Calendar Table.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

My table has 282,000 records. I tried the formula for the first occurence.

But my machine can't finish the calculation

rabiafarooq
Helper I
Helper I

please, anyone?

Helpful resources

Announcements
Microsoft Build 768x460.png

Microsoft Build is May 24-26. Have you registered yet?

Come together to explore latest innovations in code and application development—and gain insights from experts from around the world.

charticulator_carousel_with_text (1).png

Charticulator Design Challenge

Put your data visualization and design skills to the test! This exciting challenge is happening now through May 31st!

May UG Leader Call Carousel 768x460.png

What difference can a User Group make for you?

At the monthly call, connect with other leaders and find out how community makes your experience even better.

Power BI Release May 2022 768x460.png

Check it out!

Click here to read more about the May 2022 updates!