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

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
Anonymous
Not applicable

Fill missing weeks and calculate hours per week

Hi!

 

I have a dataset with accumulated values per random weeks.

 

I want to calculate hours per week. In the cases where a consecutive week is missing I would like to add the week and a zero value for each id.

Idyywwacc hours
a2208110
a2207106
a220385
a220156
a215136
a215015
a214810
a21465
b2208164
b2207152
b2206148
b2205108
b220457
b220356
b220222
c220569
c220115
c211014
c21098
c21056
c21044
c21012

Any ideas?

 

/Anna.

1 ACCEPTED SOLUTION
Jihwan_Kim
Super User
Super User

Hi,

I suggest creating a calendar table like the attached pbix file, and create a measure like below.

 

Picture1.png

 

Calendar Table: 

Calendar =
ADDCOLUMNS (
    CALENDAR ( DATE ( 2021, 1, 1 ), DATE ( 2022, 12, 31 ) ),
    "YYYYWW",
        YEAR ( [Date] ) * 100
            + WEEKNUM ( [Date], 21 )
)

 

 

Measure:

Hours: =
VAR currentweeknumber =
    MAX ( 'Calendar'[YYYYWW] )
VAR acc_hours =
    CALCULATE (
        SUM ( Data[acc hours] ),
        FILTER ( Data, Data[yyww] + 200000 = currentweeknumber )
    )
VAR previousweeknumber =
    MAXX ( FILTER ( Data, Data[yyww] + 200000 < currentweeknumber ), Data[yyww] ) + 200000
VAR previousacchours =
    CALCULATE (
        SUM ( Data[acc hours] ),
        FILTER ( Data, Data[yyww] + 200000 = previousweeknumber )
    )
RETURN
    IF ( acc_hours <> 0, acc_hours - previousacchours, 0 )

 

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Go to My LinkedIn Page


View solution in original post

3 REPLIES 3
Jihwan_Kim
Super User
Super User

Hi,

I suggest creating a calendar table like the attached pbix file, and create a measure like below.

 

Picture1.png

 

Calendar Table: 

Calendar =
ADDCOLUMNS (
    CALENDAR ( DATE ( 2021, 1, 1 ), DATE ( 2022, 12, 31 ) ),
    "YYYYWW",
        YEAR ( [Date] ) * 100
            + WEEKNUM ( [Date], 21 )
)

 

 

Measure:

Hours: =
VAR currentweeknumber =
    MAX ( 'Calendar'[YYYYWW] )
VAR acc_hours =
    CALCULATE (
        SUM ( Data[acc hours] ),
        FILTER ( Data, Data[yyww] + 200000 = currentweeknumber )
    )
VAR previousweeknumber =
    MAXX ( FILTER ( Data, Data[yyww] + 200000 < currentweeknumber ), Data[yyww] ) + 200000
VAR previousacchours =
    CALCULATE (
        SUM ( Data[acc hours] ),
        FILTER ( Data, Data[yyww] + 200000 = previousweeknumber )
    )
RETURN
    IF ( acc_hours <> 0, acc_hours - previousacchours, 0 )

 

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Go to My LinkedIn Page


Anonymous
Not applicable

Hi! This certainly seems to work for you but I can't get the total per week right. I think I have managed using "Earlier" and a group index I found here https://radacad.com/create-row-number-for-each-group-in-power-bi-using-power-query

Samarth_18
Community Champion
Community Champion

Hi @Anonymous ,

 

Please use date table for week calculation and return zero for blank values.

 

Thanks,

Samarth

Best Regards,
Samarth

If this post helps, please consider accepting it as the solution to help the other members find it more quickly.
Appreciate your Kudos!!
Connect on Linkedin

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

Find out what's new and trending in the Fabric Community.