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

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
Anonymous
Not applicable

Calculate period between two dates, with exception of two months.

Hello eveyone! 

 

I've looked online, but can't find the answer anywhere. I might get there with a lot of if's, but I'm hoping there's a more elegant solution. 

 

I need to calculate te period (in days or months) between two dates, but the summermonths (July and August) need to be disregarded. 

The problem is that the start and end date can fall in these months, or across years. 

 

Can anyone help? 🙂 

Data example: 

StartDate

 

EndDate

3/03/2019

 

5/06/2019

6/05/2019

 

12/08/2019

29/08/2019

 

8/12/2019

30/08/2019

 

2/02/2020

12/11/2019

 

5/03/2020

6/06/2020

 

9/09/2020

1 ACCEPTED SOLUTION
CNENFRNL
Community Champion
Community Champion

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Tc3LDcAwCAPQXTgjmY8ShVlQ9l+jKGma3owfkjPJ4TDRIKaGvuPkpI52ejWMCxaQcWig8COXH1Vf0WRJfaneHb/S12gdTIF4+/kA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [StartDate = _t, EndDate = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"StartDate", type date}, {"EndDate", type date}}, "de"),
    Days = Table.AddColumn(#"Changed Type", "Days Excluding Summer", each let gap=Duration.Days([EndDate]-[StartDate]) in List.Accumulate({1..gap}, 0, (s,c) => if List.Contains({7,8},Date.Month([StartDate]+#duration(c,0,0,0))) then s else s+1))
in
    Days

Screenshot 2021-08-03 133046.png


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

View solution in original post

2 REPLIES 2
CNENFRNL
Community Champion
Community Champion

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Tc3LDcAwCAPQXTgjmY8ShVlQ9l+jKGma3owfkjPJ4TDRIKaGvuPkpI52ejWMCxaQcWig8COXH1Vf0WRJfaneHb/S12gdTIF4+/kA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [StartDate = _t, EndDate = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"StartDate", type date}, {"EndDate", type date}}, "de"),
    Days = Table.AddColumn(#"Changed Type", "Days Excluding Summer", each let gap=Duration.Days([EndDate]-[StartDate]) in List.Accumulate({1..gap}, 0, (s,c) => if List.Contains({7,8},Date.Month([StartDate]+#duration(c,0,0,0))) then s else s+1))
in
    Days

Screenshot 2021-08-03 133046.png


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

Anonymous
Not applicable

Thank you so much! 

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors
Top Kudoed Authors