Starting December 3, join live sessions with database experts and the Microsoft product team to learn just how easy it is to get started
Learn moreGet certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now
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.
Solved! Go to Solution.
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
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.
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
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?
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.
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/
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.
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.
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)
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
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.
My table has 282,000 records. I tried the formula for the first occurence.
But my machine can't finish the calculation
please, anyone?
Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.
March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.
User | Count |
---|---|
90 | |
87 | |
84 | |
66 | |
49 |
User | Count |
---|---|
131 | |
110 | |
96 | |
70 | |
67 |