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
SErgey-kuznetso
Frequent Visitor

Calculate the number of days between the last and the previous sale DAX

What formula / measure can be used to calculate the number of days between the last and the last but one sale? There is the following table and it is necessary to calculate, for example, the number of days between sales of company1 on 10/21/2020 (last) and 12/31/19 (penultimate).

SErgey-kuznetso_0-1603269388987.png

Or you can just suggest how to find the penultimate sale of the company. Calculate the number of days between I can myself.

1 ACCEPTED SOLUTION
amitchandak
Super User
Super User

@SErgey-kuznetso , as new columns

max Date = maxx(filter(Table, [company] =earlier([company])),[Date])
second last = maxx(filter(Table, [company] =earlier([company]) && [Date] < earlier([max Date]) ),[Date])
diff = datediff([second last],[max Date],day)

 

or as measures

 

max Date = maxx(filter(allselected(Table), [company] =max([company])),[Date])
second last = maxx(filter(allselected(Table), [company] =max([company]) && [Date] < max([max Date]) ),[Date])
diff = datediff([second last],[max Date],day)

View solution in original post

3 REPLIES 3
amitchandak
Super User
Super User

@SErgey-kuznetso , as new columns

max Date = maxx(filter(Table, [company] =earlier([company])),[Date])
second last = maxx(filter(Table, [company] =earlier([company]) && [Date] < earlier([max Date]) ),[Date])
diff = datediff([second last],[max Date],day)

 

or as measures

 

max Date = maxx(filter(allselected(Table), [company] =max([company])),[Date])
second last = maxx(filter(allselected(Table), [company] =max([company]) && [Date] < max([max Date]) ),[Date])
diff = datediff([second last],[max Date],day)

Tahreem24
Super User
Super User

@SErgey-kuznetso ,

Try out this Measure. I am also attaching screen shot with output:

Day =
VAR Min_ = CALCULATE(MIN(Tbl2[Date]),ALLEXCEPT(Tbl2,Tbl2[Category]))
VAR Max_ = CALCULATE(MAX(Tbl2[Date]),ALLEXCEPT(Tbl2,Tbl2[Category]))
RETURN DATEDIFF(Min_,Max_,DAY)
 
Capture.PNG
Don't forget to give thumbs up and accept this as a solution if it helped you!!!

Please take a quick glance at newly created dashboards : Restaurant Management Dashboard , HR Analytics Report , Hotel Management Report, Sales Analysis Report , Fortune 500 Companies Analysis , Revenue Tracking Dashboard
manikumar34
Solution Sage
Solution Sage

@SErgey-kuznetso , 

 

Not clear, use something like this.

 

DATEDIFF(DATE(2020,10,21),DATE(2019,12,31),DAY)

 

Regards, 

Manikumar





If this helps, Appreciate your KUDOS!
Did I answer your question? Mark my post as a solution!


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.