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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
Anonymous
Not applicable

Average Days between two dates for each calendar month

Hi All,
I have a requirement to create a column AVG Days Sale to Event which will evaluate for all events/performance sold in a calendar month, the average number of days between the date of sale and the date of the event/performance.

 

My data model looks like the below screenshot where I have Transaction and Performance table joined on Performance Key and I can fetch Transaction Date Key and Performance Date time as the date of sale and date of the event.

SaloniGupta_1-1638248089557.png

The challenge I face is how to restrict Performance Date Time to be the same calendar month. Like in the below-shown screenshot for Calendar Month Jun-20, Performance Date Time shows all the dates and I want it to calculate for the same month only.

 

Days Diff Sale to Event =
VAR TransactionDate = MAX('Transaction'[Transaction Date Key])
VAR EventDate = MAX('Performance'[Performance Date Time])
VAR DaysDiff = CALCULATE(DATEDIFF(TransactionDate,EventDate,DAY))
VAR CountMonthRows = COUNTROWS(CALCULATETABLE(VALUES('Performance'[Performance Name]),
DATESBETWEEN('Transaction Date'[Date Key],TransactionDate,EventDate)))
RETURN DaysDiff

SaloniGupta_2-1638248712563.png

Can you please help me with the DAX to calculate the Average Days Sale to the event for each calendar month (Transaction Date table - Month Year)?
Here is the sample data 
https://docs.google.com/spreadsheets/d/1xmg36d-68GnFWTj8cHlNIpbwi1Ms7PqI/edit?usp=sharing&ouid=11317... 
Thanks in Advance!
@v-zhangti 

1 ACCEPTED SOLUTION
v-yangliu-msft
Community Support
Community Support

Hi  @Anonymous ,

Your sample data only has the permission to view, and you cannot copy the data. I created some data based on the sample:

Performance Table:2019 – 6 - 1 and 2019 – 7 - 17.

vyangliumsft_0-1638518844382.png

Transaction Table:2019 – 6 - 1 and 2019 – 6 – 30.

vyangliumsft_1-1638518844385.png

Here are the steps you can follow:

1. Create measure.

Flag =
var _1=MONTH(MAX('Performance Table'[Performance Date Time]))
var _2=MONTH(MAX('Transaction Table'[Transaction Date Key]))
return
if(_1 = _2&&AND(_1 <> BLANK() , _2 <>BLANK()) ,1,0)

2. Place [Flag] in Filter, set is=1, and apply filter.

vyangliumsft_2-1638518844387.png

3. Result:

There is no data for July in the visual object:

vyangliumsft_3-1638518844389.png

 

Best Regards,

Liu Yang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

View solution in original post

2 REPLIES 2
v-yangliu-msft
Community Support
Community Support

Hi  @Anonymous ,

Your sample data only has the permission to view, and you cannot copy the data. I created some data based on the sample:

Performance Table:2019 – 6 - 1 and 2019 – 7 - 17.

vyangliumsft_0-1638518844382.png

Transaction Table:2019 – 6 - 1 and 2019 – 6 – 30.

vyangliumsft_1-1638518844385.png

Here are the steps you can follow:

1. Create measure.

Flag =
var _1=MONTH(MAX('Performance Table'[Performance Date Time]))
var _2=MONTH(MAX('Transaction Table'[Transaction Date Key]))
return
if(_1 = _2&&AND(_1 <> BLANK() , _2 <>BLANK()) ,1,0)

2. Place [Flag] in Filter, set is=1, and apply filter.

vyangliumsft_2-1638518844387.png

3. Result:

There is no data for July in the visual object:

vyangliumsft_3-1638518844389.png

 

Best Regards,

Liu Yang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

Anonymous
Not applicable

Hi @v-yangliu-msft,
The flag is working perfectly fine. Thanks a lot.
Can you please also help me get the CountofRows so that I can calculate the average of the difference between the two dates?

Average Days Sale to Event =
VAR TransactionDate = MONTH(MAX('Transaction'[Transaction Date Key]))
VAR EventDate = MONTH(MAX('Performance'[Performance Date Time]))
VAR FLag = if(TransactionDate = EventDate &&AND(TransactionDate <> BLANK() , EventDate <>BLANK()) ,1,0)
VAR DaysDiff = IF(Flag = 1, CALCULATE(DATEDIFF(MAX('Transaction'[Transaction Date Key]),MAX('Performance'[Performance Date Time]),DAY)))
RETURN
CALCULATE( AVERAGE(DaysDiff), FILTER ( ALLSELECTED ('Transaction Date' ), 'Transaction Date'[Month Year] = MAX ( 'Transaction Date'[Month Year] ) )
)
Here, as you know Average accepts only a column name as an argument, how can I calculate the Average of the Days difference. Final Output looks like below:
SaloniGupta_0-1638545193129.png

 

Apologies for the permission to the sample file, I have now changed it to Editor.
https://docs.google.com/spreadsheets/d/1xmg36d-68GnFWTj8cHlNIpbwi1Ms7PqI/edit?usp=sharing&ouid=11317...


Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.