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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

First and Second Product

Hi, everyone, im trying to calculate a new column for the first and second product, I have 3 columns as User, Product and Date, what I want is the "FirstProduct" and "SecondProduct" as the example below. 

 

Thank you

 

 

User ProductDateFirstProductSecondProduct
AKey101/01/2020 14:00Key1Key2
AKey102/01/2020 14:00Key1Key2
BKey203/01/2020 14:00Key2 
AKey204/01/2020 14:00Key1Key2
CKey105/01/2020 14:00Key1Key3
CKey306/01/2020 14:00Key1Key3
CKey407/01/2020 14:00Key1Key3
AKey408/01/2020 14:00Key1Key2
DKey710/01/2020 14:00Key7Key1
DKey115/01/2020 14:00Key7Key1
1 ACCEPTED SOLUTION
Ashish_Mathur
Super User
Super User

Hi,

Try these calculated column formulas

First date of user = 

=CALCULATE(MIN(Data[Date]),FILTER(Data,Data[User]=EARLIER(Data[User])))

First product = 

LOOKUPVALUE(Data[Product],Data[User],Data[User],Data[Date],Data[First date of user])

Second date of user = 

CALCULATE(MIN(Data[Date]),FILTER(Data,Data[User]=EARLIER(Data[User])&&Data[Date]>Data[First date of user]&&Data[Product]<>EARLIER(Data[First product])))

Second product = 

=LOOKUPVALUE(Data[Product],Data[User],Data[User],Data[Date],Data[Second date of user])

Untitled.png


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

View solution in original post

4 REPLIES 4
Ashish_Mathur
Super User
Super User

Hi,

Try these calculated column formulas

First date of user = 

=CALCULATE(MIN(Data[Date]),FILTER(Data,Data[User]=EARLIER(Data[User])))

First product = 

LOOKUPVALUE(Data[Product],Data[User],Data[User],Data[Date],Data[First date of user])

Second date of user = 

CALCULATE(MIN(Data[Date]),FILTER(Data,Data[User]=EARLIER(Data[User])&&Data[Date]>Data[First date of user]&&Data[Product]<>EARLIER(Data[First product])))

Second product = 

=LOOKUPVALUE(Data[Product],Data[User],Data[User],Data[Date],Data[Second date of user])

Untitled.png


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

Hi @Anonymous 

 

Please try the following:

 

first product = 
SELECTCOLUMNS(
    TOPN(
        1,
        FILTER(ALL(fact_table_d),
            fact_table_d[Customer]=EARLIER(fact_table_d[Customer])
        )
        ,
        fact_table_d[Date],
    ASC 
    )
,"any name, it does not matter"
,fact_table_d[Product]
)

This calulcated column identifies all rows of the customer and sorts it by date. Then it takes value of the product column from the first row which is the first product bought by the customer.

 

for the seconf product use the following

second product = 
SELECTCOLUMNS(
    TOPN(
        1,
        FILTER(ALL(fact_table_d),
            fact_table_d[Customer]=EARLIER(fact_table_d[Customer])
            && fact_table_d[Product] <> EARLIER(fact_table_d[first product])
        )
        ,
        fact_table_d[Date],
    ASC 
    )
,"any name"
,fact_table_d[Product]
)

It is nearly the same query with the only exception that it also filters out the first product bought. So taht if somebody bought the same product twice in a row it will be ignored.

 

Best regards

Mikelytics

 

Did I solve your request? Please mark my post as solution.

 

Appreciate your Kudos.

 

 

------------------------------------------------------------------
Visit my blog datenhungrig which I recently started with content about business intelligence and Power BI in German and English or follow me on LinkedIn!
parry2k
Super User
Super User

@Anonymous seems like your business rule for 1st and 2nd product is to get based on the date within each user group.

 

See attached solution, you can tweak it as you see fit.

 

Check my latest blog post Compare Budgeted Scenarios vs. Actuals I would  Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make efforts to give Kudos to whoever helped to solve your problem. It is a token of appreciation!

Visit us at https://perytus.com, your one-stop-shop for Power BI-related projects/training/consultancy.



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

VijayP
Super User
Super User

@Anonymous 

I think if you can explain the logic of First and 2nd Columns then solution will be much easier, What is the basis of the new columns




Did I answer your question? Mark my post as a solution! Appreciate your Kudos!!
MY Blog || My YouTube Channel || Connect with me on Linkedin || My Latest Data Story - Ageing Analysis

Proud to be a Super User!


Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.