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

14 REPLIES 14
StephenIngutia
New Member

Hi, I need help, I am looking for a measure than can return the count of customers buying buying a particular product say product A for the first time. It can be a new customer or an exsiting customer. 

 

I have a data runing from Jan 2022 to Jan 2023. I need to get the number of customers buying product spirits for the first time ever in the month of Jan 2023. 

Any help will be appreciated.

Hi,

Ensure that you have a Calendar Table with calculated column formulas for Year, Month name and Month number.  Sort the Month name column by the Month number.  Create a relationship (Many to One and Single) from the Date column of the Data Table to the Date column of the Calendar Table.  Write these measures:

First date = calculate(min(Data[Date]),datesbetween(calendar[date],minx(all(calendar),calendar[date]),max(calendar[date])))

First customers = countrows(filter(values(Data[Customer]),[First date]>=min(calendar[date])))

To your visual, drag Year and Month from the Calendar Table.  In the slicer, select spirits.

Hope this helps.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
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
)

 

I know this is almost a year late, but if you happen to still want to know this you should know EARLIER is mostly used in the context of calculated columns. You can find more informations about it here 

 

https://blog.enterprisedna.co/how-to-use-the-earlier-function-in-power-bi-a-dax-tutorial/

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
Carousel_PBI_Wave1

2023 Release Wave 1 Plans

Power BI release plans for 2023 release wave 1 describes all new features releasing from April 2023 through September 2023.

Power BI Summit Carousel 2

Global Power BI Training

Make sure you register today for the Power BI Summit 2023. Don't miss all of the great sessions and speakers!

Thank you 2022 Review

2022 Monthly Feature Releases

We had a great 2022 with a ton of feature releases to help you drive a data culture.