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

Table with Months as columns - but have Start Date and Finish Date (date range)

Hello,

 

I have a table of requests...

 

Requestor Department Start Date Finish Date

Joe BloggsHR03/10/201606/10/2016
John SmithMarketing13/11/201613/11/2016
Sam BoonFinance27/10/201607/11/2016
Joe BloggsHR19/10/201627/10/2016

 

That I want to summarise in a table in Power BI as something like below (numbers are days). Basically I want it to calculate the duration between each pair of dates, within each month -- excluding weekends.

 

Requestor Department September October November

Joe BloggsHR 11 
John SmithMarketing  35
Sam BoonFinance7  
     

 

Some of the date ranges span multiple months e.g. with Sam, it would need to work out that of the date range (27/10/2016 - 07/11/2016), 3 days are in October and 5 days are in November. 

 

Is something like the above table possible with Power BI??

 

Many thanks 🙂

3 REPLIES 3
ImkeF
Super User
Super User

You can do this in the query editor like this:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSlVwyslPTy9W0lHyCAISBsZ6hgZ6RgaGZiCOGZwTqxOt5JWfkacQnJtZkgGU800syk4tycxLB7INgZoMYZqQOCBNwYm5Ck75+XlAGbfMvMS85FQgy8gc2RZzFA1e+ehuMrREUo2kNTYWAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Name = _t, Department = _t, Start = _t, End = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Name", type text}, {"Department", type text}, {"Start", type date}, {"End", type date}}),
    AddListOfAllDays = Table.AddColumn(#"Changed Type", "AllDays", each {Number.From([Start])..Number.From([End])}),
    #"Expanded AllDays" = Table.ExpandListColumn(AddListOfAllDays, "AllDays"),
    ChangeToDateFormat = Table.TransformColumnTypes(#"Expanded AllDays",{{"AllDays", type date}}),
    AddDayOfWeek = Table.AddColumn(ChangeToDateFormat, "DayOfWeek", each Date.DayOfWeek([AllDays])),
    FilterOutWeekends = Table.SelectRows(AddDayOfWeek, each ([DayOfWeek] <> 5 and [DayOfWeek] <> 6)),
    CalculateMonth = Table.AddColumn(FilterOutWeekends, "Month", each Date.Month([AllDays])),
    #"Removed Columns" = Table.RemoveColumns(CalculateMonth,{"AllDays"}),
    #"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(#"Removed Columns", {{"Month", type text}}, "de-DE"), List.Distinct(Table.TransformColumnTypes(#"Removed Columns", {{"Month", type text}}, "de-DE")[Month]), "Month", "DayOfWeek", List.Count)
in
    #"Pivoted Column"

 

Please let me know if you need help implementing this.

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Hi ImkeF,


Thanks for your reply! I slightly simplifed the table in my example (it's from a SQL table); I tried to do just the first step (add the custom column AllDays, expand it, and then Close & Apply) and it gave me the following error:

 

DBerror.png

 

Many thanks

Could you please post your query-code from the advanced editor?

Thanks!

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.