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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
CLQ
Helper I
Helper I

Using rankx to get the first desired event

Hello! I am learning by reading a Dax book (The Definitive Guide to DAX by Marcos Russo, I recommend it) and an exercise occurred to me.... which I could not carry out.

Let's pretend we have this table:

 

NameStoreDatetime
JohnTV7/05/2022
KelyTV6/04/2022
FredPhone3/03/2022
JohnPhone1/01/2022
FredTV2/02/2022
MarcusTV5/07/2022
etcetcetc

 

Name-->it would be the ID, each Name is unique (it was easier for me with names than with numbers)


So, I want to create a column, called "First Purchase", which returns "Yes" if it was their first purchase and "No" otherwise. But, taking into account the Store column, since if you bought in a Store that sold "Phone" it is not valid, that is, if you buy first in a "Phone" and then in a "TV", First Purchase=Yes should appear in the row where the value "TV" is found, And if the user only purchased from Store=Phone, the result would still be "No"..

So, the table above would be:

 

NameStoreDatetimeFirst Purchase
JohnTV7/05/2022Yes
KelyPhone6/04/2022No
FredPhone3/03/2022No
JohnPhone1/01/2022No
FredTV2/02/2022Yes
MarcusTV5/07/2022Yes

 

I was able to do it by going into Power Querry and removing the "Phone" values ​​from the table, but without removing anything, I can't get it to bring me the desired result.

I don't know if the Rankx function is the best, I suppose it is and that's why I put it in the title, but if there is another more comfortable or better way, I'm all ears 😁

 

Thanks!

1 ACCEPTED SOLUTION
Ashish_Mathur
Super User
Super User

Hi,

Try this calculated column formula

First purchase = if(Data[Store]="Phone","No",if(CALCULATE(MIN(Data[Datetime]),FILTER(Data,Data[Name]=EARLIER(Data[Name])&&Data[Store]<>"Phone"))=Data[Datetime],"Yes","No"))

Hope this helps.

Untitled.png


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

View solution in original post

3 REPLIES 3
Ashish_Mathur
Super User
Super User

Hi,

Try this calculated column formula

First purchase = if(Data[Store]="Phone","No",if(CALCULATE(MIN(Data[Datetime]),FILTER(Data,Data[Name]=EARLIER(Data[Name])&&Data[Store]<>"Phone"))=Data[Datetime],"Yes","No"))

Hope this helps.

Untitled.png


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

It worked! Thanks. I was stuck in the logic of, using the rank, that the oldest date returns a 1 and then in another column I would do the "if". Yours is much more effective

 
Thanks

You are welcome.


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

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

November Power BI Update Carousel

Power BI Monthly Update - November 2025

Check out the November 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.