Microsoft Fabric Community Conference 2025, March 31 - April 2, Las Vegas, Nevada. Use code FABINSIDER for a $400 discount.
Register nowGet inspired! Check out the entries from the Power BI DataViz World Championships preliminary rounds and give kudos to your favorites. View the vizzies.
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.
March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!
Check out the February 2025 Power BI update to learn about new features.
User | Count |
---|---|
83 | |
77 | |
40 | |
40 | |
35 |