Don't miss your chance to take the Fabric Data Engineer (DP-600) exam for FREE! Find out how by attending the DP-600 session on April 23rd (pacific time), live or on-demand.
Learn moreNext up in the FabCon + SQLCon recap series: The roadmap for Microsoft SQL and Maximizing Developer experiences in Fabric. All sessions are available on-demand after the live show. Register now
Hi,
I have a fact table of sales and I need to get 2 new columns, wich will display Previous date of purchase and Previous sales status (both red in the table below).
I'm trying to get same results as with using lag/window function in SQL ( lag(date_of_purchase) over (partition by month_of_sale, customer, product order by date_of_purchase).
Also, the problem is, I no longer can edit Query (the table in database no longer exists)..
| month of sale | customer | product | sales status | date of purchase | previous date of purchase | previous sales status |
| Dec-16 | john | A | booked | 01-12-16 | null | null |
| Dec-16 | john | A | booked | 20-12-16 | 01-12-16 | booked |
| Dec-16 | alicia | A | paid | 01-12-16 | null | null |
| Jan-17 | john | Ck111 | booked | 01-01-17 | null | null |
| Jan-17 | john | Ck111 | paid | 13-01-17 | 01-01-17 | booked |
| Jan-17 | john | A | booked | 01-01-17 | null | null |
| Jan-17 | john | A | booked | 03-01-17 | 01-01-17 | booked |
| Jan-17 | alicia | Ck111 | booked | 01-01-17 | null | null |
| Jan-17 | alicia | Ck111 | paid | 13-01-17 | 01-01-17 | booked |
| Jan-17 | alicia | Ck111 | paid | 20-01-17 | 13-01-17 | paid |
I've been trying to use calculate function with filters, but so far no useful results. I'm new at DAX, so probably I'm not using them right way..
Thanks in advance for any help!
Zuzana
Solved! Go to Solution.
Hi @Anonymous,
You can create calculated columns follow below DAX:
previous date of purchase = CALCULATE(MAX('Table1'[date of purchase]),FILTER(Table1,'Table1'[month of sale]=EARLIER(Table1[month of sale]) && 'Table1'[customer]=EARLIER(Table1[customer])&&'Table1'[product]=EARLIER(Table1[product])&&'Table1'[date of purchase]<EARLIER('Table1'[date of purchase])))
previous sales status = LOOKUPVALUE( Table1[sales status], Table1[month of sale], Table1[month of sale], Table1[customer], Table1[customer], Table1[product], Table1[product], Table1[date of purchase], Table1[previous date of purchase] )
Best Regards,
Qiuyun Yu
Hi @Anonymous,
You can create calculated columns follow below DAX:
previous date of purchase = CALCULATE(MAX('Table1'[date of purchase]),FILTER(Table1,'Table1'[month of sale]=EARLIER(Table1[month of sale]) && 'Table1'[customer]=EARLIER(Table1[customer])&&'Table1'[product]=EARLIER(Table1[product])&&'Table1'[date of purchase]<EARLIER('Table1'[date of purchase])))
previous sales status = LOOKUPVALUE( Table1[sales status], Table1[month of sale], Table1[month of sale], Table1[customer], Table1[customer], Table1[product], Table1[product], Table1[date of purchase], Table1[previous date of purchase] )
Best Regards,
Qiuyun Yu
https://community.powerbi.com/t5/Desktop/reference-Previous-row-using-measures/m-p/725894#M350298
help me out on the same issue.
Is there a way to do this with a measure instead of a column? My table has too many rows and there is not enough memory to solve it with a calculated column.
If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.
A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.
Experience the highlights from FabCon & SQLCon, available live and on-demand starting April 14th.
| User | Count |
|---|---|
| 48 | |
| 40 | |
| 40 | |
| 20 | |
| 16 |
| User | Count |
|---|---|
| 70 | |
| 67 | |
| 32 | |
| 27 | |
| 26 |