Forum Discussion
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 | Product | Date | FirstProduct | SecondProduct |
| A | Key1 | 01/01/2020 14:00 | Key1 | Key2 |
| A | Key1 | 02/01/2020 14:00 | Key1 | Key2 |
| B | Key2 | 03/01/2020 14:00 | Key2 | |
| A | Key2 | 04/01/2020 14:00 | Key1 | Key2 |
| C | Key1 | 05/01/2020 14:00 | Key1 | Key3 |
| C | Key3 | 06/01/2020 14:00 | Key1 | Key3 |
| C | Key4 | 07/01/2020 14:00 | Key1 | Key3 |
| A | Key4 | 08/01/2020 14:00 | Key1 | Key2 |
| D | Key7 | 10/01/2020 14:00 | Key7 | Key1 |
| D | Key1 | 15/01/2020 14:00 | Key7 | Key1 |
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])
4 Replies
- Ashish_MathurSuper 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])
- VijayPCommunity Champion
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
- parry2kSuper 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.⚡
- MikelyticsResident 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.