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
Anonymous
Not applicable

Net Work Days Grouped By Month

Hi

 

I am trying to create a matrix vizual that allows me to view a number of values by month, one of them being the # of net work days between a projects start and end date, but with grouped by months (please see example)

 

 MarchAprilMay

Project1

# of Network days for project 1 in march# of Network days for project 1 in April# of Network days for project 1 in May
Project2# of Network days for project 2 in march# of Network days for project 2 in April# of Network days for project 2 in May

 

 

 

 

 

I currently have the following measure which uses the projects start and end date - how can i revise the code to bucket the number of work days into it's associated month?

 

NetWorkDays =
VAR Calendar1 = CALENDAR(MAX('Project'[StartDate]),MAX('Project'[EndDate]))
VAR Calendar2 = ADDCOLUMNS(Calendar1,"WeekDay",WEEKDAY([Date],2))
RETURN COUNTX(FILTER(Calendar2,[WeekDay]<6),[Date])

 

 

 

 

2 ACCEPTED SOLUTIONS
Anonymous
Not applicable

File attached.

 

Best

D

View solution in original post

Anonymous
Not applicable

OK. Here's one possible solution. Please read the description in the file and play around with the data to understand what's going on.

 

Best

D

View solution in original post

34 REPLIES 34
Anonymous
Not applicable

// First, you should have a Calendar
// in your model that covers all the
// years found in any of the date fields
// in any of your tables. This Calendar
// should be disconnected from the Project
// dimension.
// Second, this Calendar should have all
// the date/time entities defined like
// months, years, day names, and each 
// date should have a column indicator
// to say if it's a working day or not.
// Project dimension stores Projects,
// that is, their ID's, names and start
// and end dates.

// Then you can write this measure:

[Net Work Days] =
var __oneProjectVisible = HASONEVALUE( 'Project'[ProjectID] )
var __startDate = SELECTEDVALUE( Project[StartDate] )
var __endDate = SELECTEDVALUE( Project[EndDate] )
var __result =
	CALCULATE(
		COUNTROWS( 'Calendar' ),
		KEEPFILTERS( __startDate <= 'Calendar'[Date] ),
		KEEPFILTERS( 'Calendar'[Date] <= __endDate ),
		KEEPFILTERS( 'Calendar'[Day Type] = "Working Day" )
	)
return
	if( __oneProjectVisible, __result )

 

This measure returns the number of working days in the currently selected period of time for any one project that's been selected. If more than one project is visible, it'll return BLANK since you've not defined what it means "the number of working days if more than 1 project is selected." It might be that you want the number of working days that belong to any of the projects (which would be reasonable) but if that's the case, you'll need to make some changes to the code.

 

Best

D

Anonymous
Not applicable

Also, the matrix will have multiple projects (loaded onto the 'Rows' pill) - how can I accomodate?

Anonymous
Not applicable

If you've got only one project visible in your matrix/table in each row... you don't have to do anything. If there are multiple projects visible in the current context, then BLANK will be returned.

Best
D
Anonymous
Not applicable

2020-04-12 15_58_39-Couldn't load the data for this visual.png

Anonymous
Not applicable

Dates must be dates, numbers must be numbers. Make sure you've got correct data types set up.

Best
D
Anonymous
Not applicable

My 'Day Type' field to denote working day is currently text (probably causing the error) - Should it be an integer flag?

Anonymous
Not applicable

[Net Work Days] =
// ProjectID can be anything but must be the identifier
var __oneProjectVisible = HASONEVALUE( 'Project'[ProjectID] )
// StartDate must be date
var __startDate = SELECTEDVALUE( Project[StartDate] )
// EndDate must be date
var __endDate = SELECTEDVALUE( Project[EndDate] )
var __result =
	CALCULATE(
		COUNTROWS( 'Calendar' ),
		// Calendar[Date] must be date
		KEEPFILTERS( __startDate <= 'Calendar'[Date] ),
		KEEPFILTERS( 'Calendar'[Date] <= __endDate ),
		// Day Type must be text since I equate it
		// to a text vaule
		KEEPFILTERS( 'Calendar'[Day Type] = "Working Day" )
	)
return
	if( __oneProjectVisible, __result )

 

Best

D

Anonymous
Not applicable

Thank you - got it so it does not throw an error but it's returning BLANK cells. Do I need to create a relationship from the DATE table to my project table?

Anonymous
Not applicable

This is what I put in the measure originally at the head:

// First, you should have a Calendar
// in your model that covers all the
// years found in any of the date fields
// in any of your tables. This Calendar
// should be DISCONNECTED from the Project
// dimension.
// Second, this Calendar should have all
// the date/time entities defined like
// months, years, day names, and each
// date should have a column indicator
// to say if it's a working day or not.
// Project dimension stores Projects,
// that is, their ID's, names and start
// and end dates.

Best
D
Anonymous
Not applicable

Yes - I saw it (thought it may be a typo) - guess I'm not understanding how the measure operates - should it be placed in the matrix as a value? or elsewhere?

 

Anonymous
Not applicable

Drag your project name on the matrix. Drag the months on the matrix. And drag the measure on the matrix. Measure will always be in the Values field. You can't put it anywhere else.

Best
D
Anonymous
Not applicable

Here's the solution. Notice that the date slicer is dynamic and shows only those units of time that do have at least one project running inside them.

 

Best

D

Anonymous
Not applicable

Thank you so much - it's working perfectly. I do have another layer of complexity though. I need to multiply the returned date by a percentage. I thought I could just reference the measure in the calc (i.e., networkdays * [column with %]) which works, but doesn't incorporate the month aspect. Is there a way to update the measure to reference the column I need to multiply by?

Anonymous
Not applicable

I don't understand how you can multiply a date by a number... It's not possible.

Best
D
Anonymous
Not applicable

Sorry, I was not clear - the below highlights what I need to do ( right now, the output is not taking into consideration the month)

 

 

[Percentage] * [Net Work Day Measure] 

 

ProjectPercentageNet work Day (March)Output (March)Net work Day (April)Output (april)
Project 150%94.5157.5
Project 220%224.4205
      
Anonymous
Not applicable

Sure it's possible but I'd create a second measure for this and name it appropriately. If the measure must work on the month level only, you have to ensure this by checking whether the current context contains full months only and then multiply the measure by the percentage iterating through Projects.

Best
D
Anonymous
Not applicable

Thank you - would you happen to know the best way to do it? Sorry for the follow-up, if there's documentation that you'd recommend that would be appreciated.

 

Create a new measure ->[Network Day Measure] * [Percentage column?]

Anonymous
Not applicable

File attached.

 

Best

D

Anonymous
Not applicable

You're a saint - thank you so much for the help 😀

Anonymous
Not applicable

And one last thing.... Kudos would be much appreciated. That the button with the thumbs-up...

Best
D

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.