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

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

Reply
PBI_newuser
Post Prodigy
Post Prodigy

How to create duration for months based on multiple criteria

Hi,

I have 2 tables, one is Contract table and the other one is Service table.

I would like to create the duration in months from 1 to 12 based on the contract start date.

For example: 

Product A has a 3-year contract where contract start date is 1/1/2018 and contract end date is 31/12/2020.

The duration should stop at 12 only. After 12 months, the duration start from 1 again from 13th to 24th month of the contract.

DurationContract Year 1Contract Year 2Contract Year 3
1Jan-18Jan-19Jan-20
2Feb-18Feb-19Feb-20
3Mar-18Mar-19Mar-20
4Apr-18Apr-19Apr-20
5May-18May-19May-20
6Jun-18Jun-19Jun-20
7Jul-18Jul-19Jul-20
8Aug-18Aug-19Aug-20
9Sep-18Sep-19Sep-20
10Oct-18Oct-19Oct-20
11Nov-18Nov-19Nov-20
12Dec-18Dec-19Dec-20

 

In the Service table, we look at the Job Created Date and see which duration it falls within the contract. For example, if the job is created in Oct-18, then it should fall on duration 10, if the job is created in May-19, then it should fall on duration 5. Then we calculate the hours difference between Repair Date and Closed Date of each job. We wanna see if the hours difference is above or below the target line as shown in the example below.

 
 
 

Contract table:

ProductContract Start DateContract End DateContract Duration (years)
AAA01-02-1931-01-201
BBB04-07-1803-07-202
CCC20-01-1719-01-214
DDD15-06-2014-12-200.5

 

Service table:

JobProductJob Created DateRepair DateClosed DateHours DiffExpected Duration
Job-123AAA11-01-19 14:0322-01-19 14:0304-02-19 14:03312 
Job-124AAA20-05-19 23:0401-06-19 23:0413-06-19 23:042884
Job-125AAA14-03-20 9:1327-03-20 9:1307-04-20 9:13264 
Job-126AAA06-07-19 4:0820-07-19 4:0830-07-19 4:082405
Job-127BBB02-06-18 22:2017-06-18 22:2026-06-18 22:20216 
Job-128BBB06-10-18 10:1022-10-18 10:1030-10-18 10:101924
Job-129BBB20-06-20 8:4507-07-20 8:4514-07-20 8:4516812
Job-130BBB03-04-20 16:2321-04-20 16:2327-04-20 16:2314410
Job-131BBB16-03-19 17:3203-04-19 17:3209-04-19 17:321449
Job-132CCC12-01-17 10:4331-01-17 10:4305-02-17 10:43120 
Job-133CCC12-01-21 14:5522-01-21 14:5505-02-21 14:5533612
Job-134CCC04-08-19 22:2706-08-19 22:2728-08-19 22:275283
Job-135CCC15-09-18 21:4319-09-18 21:4309-10-18 21:434808
Job-136CCC26-02-17 13:3204-03-17 13:3222-03-17 13:324322
Job-137CCC13-02-21 9:3321-02-21 9:3309-03-21 9:33384 
Job-138CCC14-07-19 19:3031-07-19 19:3007-08-19 19:301687
Job-139DDD22-07-20 8:5627-07-20 8:5615-08-20 8:564562
Job-140DDD10-12-20 12:4011-12-20 12:4003-01-21 12:405526

 

Can anyone help on this? Sample data here.

Thanks.

8 REPLIES 8
Anonymous
Not applicable

Hi @PBI_newuser ,

 

Please replace

SWITCH ( TRUE (), _hour > 12, MOD ( _hour, 12 ), _hour )

with

IF(MOD(_hour,12)=0,12,MOD(_hour,12))

 

But sorry for that I'm a little confused about your last question.

I did not learn a lot about FLOOR(). Sorry again...

 

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

Hi @Anonymous  , sorry for the confusion. I wish to create Cumulative Hours Diff based on the "Product_Year" and "Time(months)". Here is the pbix file.

For example,

PBI_newuser_0-1614169733542.png

sample.png

In addition, how to create a column with generating a series as below?

Table = ADDCOLUMNS( GenerateSeries(1,12,1),"Cumulated Target",[Value]*200,
Anonymous
Not applicable

Hi @PBI_newuser ,

 

Sorry for my careless idea.

 

According to my understanding ,when [Job Created Date] is between [Contract Start Date] and [Contract End Date], return a remainder after a HourDiff is divided by a 12, otherwise, return blank, right?

 

Please try this:

 

Time(months) =
VAR _start =
    LOOKUPVALUE (
        'Contract_New'[Contract Start Date],
        'Contract_New'[Product], [Product]
    )
VAR _end =
    LOOKUPVALUE (
        'Contract_New'[Contract End Date],
        'Contract_New'[Product], [Product]
    )
VAR _hour =
    DATEDIFF ( _start, [Job Created Date], MONTH ) + 1
RETURN
    IF (
        [Job Created Date] >= [Contract Start Date]
            && [Job Created Date] <= [Contract End Date],
        SWITCH ( TRUE (), _hour > 12, MOD ( _hour, 12 ), _hour ),
        BLANK ()
    )

 

 

2.24.fo.PNG

Here is the pbix file.

 

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

Hi  , thank you so much for your great solution! I learned a lot from your solution.

Yes, it should be the remainder but if the remainder is 0, I would like to have it as 12.

Sorry to bother you, one last question, I learned how you created the new Table by using the earlier sample data. But with the current data, is it possible to a measure to plot the similar chart? E.g. "Product BBB Year 2" field will auto populate by using the FLOOR (_hour,12)?

PBI_newuser_0-1614157385907.png

 

Anonymous
Not applicable

Hi @PBI_newuser ,

 

1. Add Time(months) column:

 

 

Time(months) =
VAR _start =
    LOOKUPVALUE ( 'Contract'[Contract Start Date], 'Contract'[Product], [Product] )
VAR _end =
    LOOKUPVALUE ( 'Contract'[Contract End Date], 'Contract'[Product], [Product] )
VAR _hour =
    DATEDIFF ( _start, [Job Created Date], MONTH ) + 1
RETURN
    SWITCH ( TRUE (), _hour < 0, BLANK (), _hour > 12, _hour - 12, _hour )

 

 

2. Create the new table:

 

 

Table =
ADDCOLUMNS (
    GENERATESERIES ( 1, 12, 1 ),
    "Cumulated Target", [Value] * 200,
    "Product AAA Year 1",
        CALCULATE (
            SUM ( 'Service'[Hours Diff] ),
            FILTER (
                'Service',
                'Service'[Product] = "AAA"
                    && 'Service'[Time(months)] <> BLANK ()
                    && 'Service'[Time(months)] <= [Value]
            )
        ),
    "Product BBB Year 1",
        CALCULATE (
            SUM ( 'Service'[Hours Diff] ),
            FILTER (
                'Service',
                'Service'[Product] = "BBB"
                    && YEAR ( [Job Created Date] ) = 2019
                    && 'Service'[Time(months)] <> BLANK ()
                    && 'Service'[Time(months)] <= [Value]
            )
        ),
    "Product BBB Year 2",
        CALCULATE (
            SUM ( 'Service'[Hours Diff] ),
            FILTER (
                'Service',
                'Service'[Product] = "BBB"
                    && YEAR ( [Job Created Date] ) = 2020
                    && 'Service'[Time(months)] <> BLANK ()
                    && 'Service'[Time(months)] <= [Value]
            )
        )
)

 

 

The final output is show below:

2.23.6.1.PNG

But according to the screenshot, the  logic of Product BBB Year 1 column seems to be different from the other two columns(Product AAA Year 1 and Product BBB Year 2), Is it wrong or there is another logic?

 

Here is the pbix file.

 

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

Hi @Anonymous , you are correct! The screenshot for Product BBB Year 1 is incorrect.

I noticed you created a new table for each Product-Year field. How can I create the same if I have different Contract Start date? When the Job Created Date is beyond the range of contract duration, how to ignore them in the calculation? Please see "Contract_New" and "Service"New" tables in the sample here.

PBI_newuser_0-1614145863774.png

I have created the same on "Service_New" table.

PBI_newuser_1-1614148335986.png

 

Thank you!! 

Anonymous
Not applicable

Hi @PBI_newuser ,

 

Not very clear. I'm quite confused about the logic of the Expected Duration column.

 

Could you please provide me with more details about your table and your problem or share me with your pbix file from your Onedrive for Business after removing sensitive data?

 

Best Regards,
Eyelyn Qin

 

Hi @Anonymous , sorry for the confusion. I have recreated the sample data here.

Please find the explanation in the below screenshot. The chart is expected output.

There are 2 sheets/tables to load into Power BI only.

PBI_newuser_1-1613754157567.png

 

PBI_newuser_0-1613754039948.png

 

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

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.