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
TeunVerhagen
Frequent Visitor

Create column that calculates the days based on period in between dates

Hi all,

 

For a project I have to create a table that shows the number of days a contract is active per month. I manually created a table as an example (see below). The first two rows are manually filled in to make things clear. The bold columns are the ones that I want to add based on a formula.

 

Each contract has a start date and an end date. The formula must return the number of days that the contract is active based on the period between start date and end date. So for example a contract with start date 1-1-2020 and end date 5-2-2020 is 31 days in Januari and 5 days in Februari.

 

Can someone help with the formula that I have to use to create the month columns?

 

ContractStart dateEnd dateDays in jan-20Days in feb-20Days in mar-20
11-1-202012-2-202031120
210-1-202031-3-2020102931
35-2-202025-3-2020???

 

Thanks in advance!

 

Teun

1 ACCEPTED SOLUTION
dax
Community Support
Community Support

Hi @TeunVerhagen , 

I think the result should be similar to below

Contract Start date End date Days in jan-20 Days in feb-20 Days in mar-20
1 1-1-2020 12-2-2020 31 12 0
2 10-1-2020 31-3-2020 22(here should be 22) 29 31
3 5-2-2020 25-3-2020 0 25 25

If so , you could try below M code to see whether it work or not

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("RYrBCQAwCMR2ubeCnjiNuP8abSmtv5CkCg6BqyuNdpDKyy0FHmNTwzWmxjb5dgHzx14=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Contract = _t, #"Start date" = _t, #"End date" = _t]),
    #"Changed Type with Locale" = Table.TransformColumnTypes(Source, {{"Start date", type date},{"End date", type date}}, "aa-DJ"),
    #"Added Custom" = Table.AddColumn(#"Changed Type with Locale", "Custom", each {Number.From([Start date])..Number.From([End date])}),
    #"Expanded Custom" = Table.ExpandListColumn(#"Added Custom", "Custom"),
    #"Changed Type" = Table.TransformColumnTypes(#"Expanded Custom",{{"Custom", type date}}),
    #"Added Custom1" = Table.AddColumn(#"Changed Type", "Custom.1", each Text.From (Date.Year([Custom]))&" "& Date.MonthName([Custom])),
    #"Grouped Rows" = Table.Group(#"Added Custom1", {"Contract", "Start date", "End date", "Custom.1"}, {{"Count", each Table.RowCount(_), type number}}),
    #"Pivoted Column" = Table.Pivot(#"Grouped Rows", List.Distinct(#"Grouped Rows"[Custom.1]), "Custom.1", "Count", List.Sum)
in
    #"Pivoted Column"

 

Best Regards,
Zoe Zhi

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
dax
Community Support
Community Support

Hi @TeunVerhagen , 

I think the result should be similar to below

Contract Start date End date Days in jan-20 Days in feb-20 Days in mar-20
1 1-1-2020 12-2-2020 31 12 0
2 10-1-2020 31-3-2020 22(here should be 22) 29 31
3 5-2-2020 25-3-2020 0 25 25

If so , you could try below M code to see whether it work or not

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("RYrBCQAwCMR2ubeCnjiNuP8abSmtv5CkCg6BqyuNdpDKyy0FHmNTwzWmxjb5dgHzx14=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Contract = _t, #"Start date" = _t, #"End date" = _t]),
    #"Changed Type with Locale" = Table.TransformColumnTypes(Source, {{"Start date", type date},{"End date", type date}}, "aa-DJ"),
    #"Added Custom" = Table.AddColumn(#"Changed Type with Locale", "Custom", each {Number.From([Start date])..Number.From([End date])}),
    #"Expanded Custom" = Table.ExpandListColumn(#"Added Custom", "Custom"),
    #"Changed Type" = Table.TransformColumnTypes(#"Expanded Custom",{{"Custom", type date}}),
    #"Added Custom1" = Table.AddColumn(#"Changed Type", "Custom.1", each Text.From (Date.Year([Custom]))&" "& Date.MonthName([Custom])),
    #"Grouped Rows" = Table.Group(#"Added Custom1", {"Contract", "Start date", "End date", "Custom.1"}, {{"Count", each Table.RowCount(_), type number}}),
    #"Pivoted Column" = Table.Pivot(#"Grouped Rows", List.Distinct(#"Grouped Rows"[Custom.1]), "Custom.1", "Count", List.Sum)
in
    #"Pivoted Column"

 

Best Regards,
Zoe Zhi

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

camargos88
Community Champion
Community Champion

Hi @TeunVerhagen ,

 

Take a look on this file: Download PBIX 

 

It creates a table with the date range for each project and count the dates for each month.

 

If you consider it as a solution, please mark as a solution and kudos.

Ricardo



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.

Top Solution Authors
Top Kudoed Authors