cancel
Showing results for
Did you mean:

The ultimate Microsoft Fabric, Power BI, Azure AI & SQL learning event! Join us in Las Vegas from March 26-28, 2024. Use code MSCUST for a \$100 discount. Register Now

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
Employee

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
)```

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.
15 REPLIES 15
Frequent Visitor

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.

Super User

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
Employee

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
)```

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.
Helper I

Hello, thank you so much for the answer.

What if there are ties in the date and we would like to randomly choose one of the ties to flag it as first occurrence?

Regular Visitor

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.

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)

Helper I

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
)```

New Member

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

Super User

Hi,

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

Regards,
Ashish Mathur
http://www.ashishmathur.com
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

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.

Super User

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
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

Super User

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
Helper II

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

But my machine can't finish the calculation

Helper I

Announcements

#### Microsoft Fabric Community Conference

Join us at our first-ever Microsoft Fabric Community Conference, March 26-28, 2024 in Las Vegas with 100+ sessions by community experts and Microsoft engineering.

#### Microsoft Fabric Career Hub

Explore career paths and learn resources in Fabric.

#### Microsoft Fabric Partner Community

Engage with the Fabric engineering team, hear of product updates, business opportunities, and resources in the Fabric Partner Community.