Forum Discussion

Coryanthony's avatar
Coryanthony
Helper III
3 years ago
Solved

Calculating hours worked

Hi, I am attemping to calculate the sum of allowed $ based on hours worked on a given day. I currently enter time on several rows since different chargecodes are involved. 7/1/2022 | 5 hours 7/1/2...
  • pi_eye's avatar
    3 years ago

    Hi Coryanthony  

     

    What's happening here is that the calculated column makes a value for $allowed on every single row of data. As there are 3 rows for 7/1/22, naturally there are 3 repeated values of $allowed.

    When $Allowed is dropped into a table, it is a number and will be automatically aggregated which is causing those values to Sum. 

    There are a few things you can do to change this:

     

    1) Change the model so that there is a separate table dimension for employee & date

    This will then return just the one value for every combination of the above
     
    2) Create a measure that aggregates your existing $allowed column as appropriate
    In the UI, create a measure. Here I've just taken a flat average
    Allowed_Aggr = AVERAGE(Merge1[$Allowed])
    You will need to tweak this to sum properly depending on the context that you are going to use. The average above will work for combinations of Employee & Date, but will need changing depending on how you handle totals

     

    3) Change $Allocation to a measure rather than a calculated column

    This could have performance implications, but the equivalent of your calculation above, as a measure would be:

    Allowed_As_Measure = 
    SumX(SUMMARIZE(
            Merge1,
            Merge1[Employee ID],Merge1[Date],Merge1[Holiday],Merge1[Day of Week],
            "DailyHrs",sum(Merge1[Hours])),
        SWITCH(TRUE,
            [DailyHrs]>=4 && [DailyHrs]<8 && Merge1[Holiday] = "Yes" ,12,
            [DailyHrs]>=8 && Merge1[Holiday] = "Yes" ,32,
            [DailyHrs]>=10 &&'Merge1'[Day of Week]<=4,20,
            [DailyHrs]>=4 && [DailyHrs]<8 && 'Merge1'[Day of Week]>=5,12,
            [DailyHrs]>=8 && 'Merge1'[Day of Week]>=5,32,
            0
        ))

     

    In this, I am assuming that the totals need to be summed. I have also replaced the if statements with a switch() - this is a much cleaner way to introduce much logic.
     
     
    In addition to the above, notice how changing to a measure introduces "0" instead of blank.
    HTH
     
    Pi