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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Learn more

Reply
android1
Post Patron
Post Patron

Count Number of Weeks with a condition

Hi,

 

I'm using the below to give me the number of weeks someone has been employed.

 

Wks of Employment = DATEDIFF(vw_PivotVisitsInvoiced[Carer_StartDate],TODAY(),WEEK)

 

Works great however in another table CarersList there is an end date column [DateLeft].

Those still employed will have no end date. 

 

How do I modify the above to account for those who have an end date?

2 ACCEPTED SOLUTIONS

@android1

 

It appears to be some data quality. Here is another approach:

 

On your vw_PivotVisitsInvoiced table create the following calculated column:  CarerLastDate = RELATED(CarersList[DateLeft])

This will create another column populated with DateLeft values.

 

From here you can write another calc column:

 

DATEDIFF(vw_PivotVisitsInvoiced[Carer_StartDate],PivotVisitsInvoiced[DateLeft] ,WEEK)

 

or for a measure you got to wrap your fields in FIRSTDATE and LASTDATE

 

See if works.

 

N -

View solution in original post

@android1

 

I see.

Something like this ?

 

IF(

     ISBLANK(vw_PivotVisitsInvoiced[DateLeft],

     Wks of Employment,

     Wks of Employment 2

)

View solution in original post

15 REPLIES 15
nickchobotar
Skilled Sharer
Skilled Sharer

Hi @android1

 

Have you tried replacing TODAY() with [DateLeft] ?

 

DATEDIFF(vw_PivotVisitsInvoiced[Carer_StartDate], [DateLeft] ,WEEK)

 

Nick

I have but columns in the Carerslist table are not available to use. This is my relationship between the two tables.

 

2018-03-14 18_57_32-Carer Cost - Power BI Desktop.png2018-03-14 18_59_55-Carer Cost - Power BI Desktop.png

@android1


Give this one a try. You do not need RELATED() function here.

DATEDIFF(

      FIRSTDATE(vw_PivotVisitsInvoiced[Carer_StartDate]),

      LASTDATE(CareList([DateLeft]) ,

      WEEK

)

It's not letting me choose columns from Carerlist table. 

 

Also, I'd need to accomodate for people who have no DateLeft.

 

2018-03-14 19_20_58-Carer Cost - Power BI Desktop.png

Replace CareList([DateLeft]) to CareList[DateLeft]

Still the same unfortunately.

 

2018-03-14 19_27_50-Carer Cost - Power BI Desktop.png

@android1

 

It appears to be some data quality. Here is another approach:

 

On your vw_PivotVisitsInvoiced table create the following calculated column:  CarerLastDate = RELATED(CarersList[DateLeft])

This will create another column populated with DateLeft values.

 

From here you can write another calc column:

 

DATEDIFF(vw_PivotVisitsInvoiced[Carer_StartDate],PivotVisitsInvoiced[DateLeft] ,WEEK)

 

or for a measure you got to wrap your fields in FIRSTDATE and LASTDATE

 

See if works.

 

N -

Oh, works perfectly that. Thank you.

 

Now my problem is those with no DateLeft. Their Wks Of employment is blank. 

 

 

 

 

 

 

@android1

 

What's your ultimate goal? How do you want to see your data ?

 

Do you want to see both currently employed and also the folks that have already left in a single column ?

 

I'd like to see Wks of Employment in one column yes. If that's doable.

 

 

@android1

 

Try adding together two instances of DATEIFF() like so   DATEIFF(logic for employed) + DATEIFF(logic for those who left).  Otherwise, you have an options to create two separate columns using two measures.

 

N -

Added both as Wks of Employment 3.

 

I'd need it to use the original Wks of Employment = DATEDIFF(vw_PivotVisitsInvoiced[Carer_StartDate],TODAY(),WEEK)

but if they have and EndDate then use 

Wks of Employment 2 DATEDIFF(vw_PivotVisitsInvoiced[Carer_StartDate],PivotVisitsInvoiced[DateLeft] ,WEEK)

 

Wks of Employment 3 = vw_PivotVisitsInvoiced[Wks of Employment] + vw_PivotVisitsInvoiced[Wks of Employment 2]

 

2018-03-14 20_26_51-Carer Cost - Power BI Desktop.png

@android1

 

If all worked out for you, please accept this as a solution.

 

Thanks, N -

@nickchobotar

 

I'd like to be able to show all in one column. So if there is no DateLeft then use the first calc column but if there is then use the second one. Like combining into an If statement.

@android1

 

I see.

Something like this ?

 

IF(

     ISBLANK(vw_PivotVisitsInvoiced[DateLeft],

     Wks of Employment,

     Wks of Employment 2

)

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors