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

Calculate weekly hours from daily hours and week number - DAX

Hello!

 

I need some help to calculate a new column using DAX. I've never done a calculation like this, so I'm lost. 

 

I have a weekly calendar, with the following columns: Week and Daily hour. Now I need a new column to count all the hours per week (  from 1 to 168 hours, 7 days * 24 hours = 168 hours ).

 

I wish to obtain the Weekly hour column for all the weeks of the year:

 

Weekly hour.PNG

 

The 3 dots just represent the rest of the values between the shown values.  

 

I appreciate all your help!

 

1 ACCEPTED SOLUTION

Hi @e17aguilar,

 

I made some update on your formulas. Seemed your missed time 8, so I added that by myself.

 

Calendar = 
ADDCOLUMNS (
    CROSSJOIN (
        CALENDAR (DATE(2016,1,2), DATE(2016,1,12)),
        UNION (
            ROW ( "Time", TIME ( 1, 0, 0 ) ),
            ROW ( "Time", TIME ( 2, 0, 0 ) ),
            ROW ( "Time", TIME ( 3, 0, 0 ) ),
            ROW ( "Time", TIME ( 4, 0, 0 ) ),
            ROW ( "Time", TIME ( 5, 0, 0 ) ),
            ROW ( "Time", TIME ( 6, 0, 0 ) ),
            ROW ( "Time", TIME ( 7, 0, 0 ) ),
            ROW ( "Time", TIME ( 8, 0, 0 ) ),
            ROW ( "Time", TIME ( 9, 0, 0 ) ),
            ROW ( "Time", TIME ( 10, 0, 0 ) ),
            ROW ( "Time", TIME ( 11, 0, 0 ) ),
            ROW ( "Time", TIME ( 12, 0, 0 ) ),
            ROW ( "Time", TIME ( 13, 0, 0 ) ),
            ROW ( "Time", TIME ( 14, 0, 0 ) ),
            ROW ( "Time", TIME ( 15, 0, 0 ) ),
            ROW ( "Time", TIME ( 16, 0, 0 ) ),
            ROW ( "Time", TIME ( 17, 0, 0 ) ),
            ROW ( "Time", TIME ( 18, 0, 0 ) ),
            ROW ( "Time", TIME ( 19, 0, 0 ) ),
            ROW ( "Time", TIME ( 20, 0, 0 ) ),
            ROW ( "Time", TIME ( 21, 0, 0 ) ),
            ROW ( "Time", TIME ( 22, 0, 0 ) ),
            ROW ( "Time", TIME ( 23, 0, 0 ) ),
            ROW ( "Time", TIME ( 24, 0, 0 ) )
        )
    ),
    "DateTime", [Date] + [Time]
)

Then we can create a calculated columns to work on that.

 

Weekly hour1 = CALCULATE(SUM(Calendar[Column]),FILTER(ALLEXCEPT('Calendar','Calendar'[Week]),'Calendar'[DateTime]<=EARLIER('Calendar'[DateTime])))

For more details, please check the pbix as attached.

 

Regards,

Frank

Community Support Team _ Frank
If this post helps, then please consider Accept it as the solution to help the others find it more quickly.

View solution in original post

6 REPLIES 6
v-frfei-msft
Community Support
Community Support

Hi @e17aguilar,

 

Here I made one sample for your reference.

 

1. Enter the sample data and create two calculated columns.

 

week = WEEKNUM(Table1[Date])
Column = IF(ISBLANK(Table1[time]),BLANK(),1)

 

 

2.Create a measure to achieve your goal.

 

 

Measure = CALCULATE(SUM(Table1[Column]),FILTER(ALLEXCEPT(Table1,Table1[week]),Table1[time]<=MAX(Table1[time])))

2.PNG

 

 

If you want to get the calculated column not the measure. Then please use the formula.

 

result = CALCULATE(SUM(Table1[Column]),Table1[time]<=EARLIER(Table1[time]))

 

 

For more details, please check the pbix as attached.

 

Regards,

Frank

Community Support Team _ Frank
If this post helps, then please consider Accept it as the solution to help the others find it more quickly.

Hi Frank! @v-frfei-msft

 

Thank you for your help. 

 

I tried your "new column DAX formula" to calculate weekly hours, but it doesn't work for me, I don't know why, maybe because of the way I calcultate the calendar. 

 

Here is the pbix file, check it if you have a chance. 

 

Thanks in advance for your help. 

 

Kind regards, 

Ernesto

 

 

Hi @e17aguilar,

 

I made some update on your formulas. Seemed your missed time 8, so I added that by myself.

 

Calendar = 
ADDCOLUMNS (
    CROSSJOIN (
        CALENDAR (DATE(2016,1,2), DATE(2016,1,12)),
        UNION (
            ROW ( "Time", TIME ( 1, 0, 0 ) ),
            ROW ( "Time", TIME ( 2, 0, 0 ) ),
            ROW ( "Time", TIME ( 3, 0, 0 ) ),
            ROW ( "Time", TIME ( 4, 0, 0 ) ),
            ROW ( "Time", TIME ( 5, 0, 0 ) ),
            ROW ( "Time", TIME ( 6, 0, 0 ) ),
            ROW ( "Time", TIME ( 7, 0, 0 ) ),
            ROW ( "Time", TIME ( 8, 0, 0 ) ),
            ROW ( "Time", TIME ( 9, 0, 0 ) ),
            ROW ( "Time", TIME ( 10, 0, 0 ) ),
            ROW ( "Time", TIME ( 11, 0, 0 ) ),
            ROW ( "Time", TIME ( 12, 0, 0 ) ),
            ROW ( "Time", TIME ( 13, 0, 0 ) ),
            ROW ( "Time", TIME ( 14, 0, 0 ) ),
            ROW ( "Time", TIME ( 15, 0, 0 ) ),
            ROW ( "Time", TIME ( 16, 0, 0 ) ),
            ROW ( "Time", TIME ( 17, 0, 0 ) ),
            ROW ( "Time", TIME ( 18, 0, 0 ) ),
            ROW ( "Time", TIME ( 19, 0, 0 ) ),
            ROW ( "Time", TIME ( 20, 0, 0 ) ),
            ROW ( "Time", TIME ( 21, 0, 0 ) ),
            ROW ( "Time", TIME ( 22, 0, 0 ) ),
            ROW ( "Time", TIME ( 23, 0, 0 ) ),
            ROW ( "Time", TIME ( 24, 0, 0 ) )
        )
    ),
    "DateTime", [Date] + [Time]
)

Then we can create a calculated columns to work on that.

 

Weekly hour1 = CALCULATE(SUM(Calendar[Column]),FILTER(ALLEXCEPT('Calendar','Calendar'[Week]),'Calendar'[DateTime]<=EARLIER('Calendar'[DateTime])))

For more details, please check the pbix as attached.

 

Regards,

Frank

Community Support Team _ Frank
If this post helps, then please consider Accept it as the solution to help the others find it more quickly.

Hello Frank!

 

Many thanks for your help. 

 

Unffurtanetly, the this code is not working for me, I receive this message after wait 5 minutes to calculate.

PBI message.PNG

 

I think your code works, but it doesn't work for my model, maybe my model is too large. 

 

Finnaly, I decided to create a calendar using parameters and merging two queries. 

 

Many thanks for your time and your attention.

 

Regards,

 

Ernesto

Greg_Deckler
Super User
Super User

Are those all of the source columns that you have or do you have a Date column in there as well?


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Hi Greg,

 

I also have a Date and DateTime column:

 

Other columns.PNG

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.