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
ausphil
Helper I
Helper I

Sum values between datetimes in related table

Hello, I have ShiftTable like so:

 

ShiftTimeStartShiftTimeEnd
12-09-18 7:0012-09-18 19:00
12-09-18 19:0013-09-18 7:00
13-09-18 7:0013-09-18 19:00
13-09-18 19:0014-09-18 7:00
14-09-18 7:0014-09-18 19:00
14-09-18 19:0015-09-18 7:00

  

And my data is like so:

 

 

PersonSyncTimeTotal StepsHourly Steps
John12-09-18 7:0020000 
John12-09-18 8:0020754754
John12-09-18 9:0021076322
John12-09-18 10:0021532456
John12-09-18 11:002154412
John12-09-18 12:002155814
John12-09-18 13:002157719
John12-09-18 14:0021831254
John12-09-18 15:0022164333
John12-09-18 16:0022664500
John12-09-18 17:002269733
John12-09-18 18:002273740
John12-09-18 19:002278750
Martin12-09-18 7:0011568 
Martin12-09-18 8:0011808240
Martin12-09-18 9:0011952144
Martin12-09-18 10:0012237285
Martin12-09-18 11:001230366
Martin12-09-18 12:001237774
Martin12-09-18 13:001247699
Martin12-09-18 14:0012801325
Martin12-09-18 15:0012912111
Martin12-09-18 16:0013362450
Martin12-09-18 17:001343977
Martin12-09-18 18:0014239800
Martin12-09-18 19:0014679440

 

I would like to add a calculated column that sums all the hourly steps for each person between the ShiftTable times, with the value only being in the row corresponding to that Shift time like so:

  

PersonSyncTimeTotal StepsHourly StepsShift Steps
John12-09-18 7:0020000  
John12-09-18 8:0020754754 
John12-09-18 9:0021076322 
John12-09-18 10:0021532456 
John12-09-18 11:002154412 
John12-09-18 12:002155814 
John12-09-18 13:002157719 
John12-09-18 14:0021831254 
John12-09-18 15:0022164333 
John12-09-18 16:0022664500 
John12-09-18 17:002269733 
John12-09-18 18:002273740 
John12-09-18 19:0022787502787
Martin12-09-18 7:0011568  
Martin12-09-18 8:0011808240 
Martin12-09-18 9:0011952144 
Martin12-09-18 10:0012237285 
Martin12-09-18 11:001230366 
Martin12-09-18 12:001237774 
Martin12-09-18 13:001247699 
Martin12-09-18 14:0012801325 
Martin12-09-18 15:0012912111 
Martin12-09-18 16:0013362450 
Martin12-09-18 17:001343977 
Martin12-09-18 18:0014239800 
Martin12-09-18 19:00146794403111

 

SyncTime is related to ShiftTimeStart.

 

Appreciate your help!!

1 ACCEPTED SOLUTION

OK I think I got it, changed the relationship to ShiftTime[ShiftTimeEnd] and used:

 

Column = IF (
    RELATED ( ShiftTime[ShiftTimeEnd] ) = Data[SyncTime], 
    CALCULATE (
        SUM ( Data[Hourly Steps] ),
        FILTER (
            ALL ( Data ),
            Data[Person]  = EARLIER ( Data[Person]  )
            && Data[SyncTime] > EARLIER(RELATED(ShiftTime[ShiftTimeStart]))
            && Data[SyncTime] <= EARLIER(RELATED(ShiftTime[ShiftTimeEnd]))
        )
    ),
    BLANK()
)

View solution in original post

4 REPLIES 4
v-cherch-msft
Employee
Employee

Hi @ausphil

 

You may try to add an index column in Query Editor for your Data table. Then create the calculated column with EARLIER Function as below:

Column =
IF (
    RELATED ( ShiftTime[ShiftTimeEnd] ) = Data[SyncTime],
    CALCULATE (
        SUM ( Data[Hourly Steps] ),
        FILTER (
            ALL ( Data ),
            Data[Person] = EARLIER ( Data[Person] )
                && Data[Index] <= EARLIER ( Data[Index] )
        )
    ),
    BLANK ()
)

1.png

 

Regards,

Cherie

Community Support Team _ Cherie Chen
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

OK I think I got it, changed the relationship to ShiftTime[ShiftTimeEnd] and used:

 

Column = IF (
    RELATED ( ShiftTime[ShiftTimeEnd] ) = Data[SyncTime], 
    CALCULATE (
        SUM ( Data[Hourly Steps] ),
        FILTER (
            ALL ( Data ),
            Data[Person]  = EARLIER ( Data[Person]  )
            && Data[SyncTime] > EARLIER(RELATED(ShiftTime[ShiftTimeStart]))
            && Data[SyncTime] <= EARLIER(RELATED(ShiftTime[ShiftTimeEnd]))
        )
    ),
    BLANK()
)

Hi @ausphil

 

Glad to hear that you've solved it. please accept your reply as solution, that way, other community members will easily find the solution when they get same issue.

 

Regards,

Cherie

Community Support Team _ Cherie Chen
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Hi Cherie,

 

Thank you for your suggestion. That works for the first shift, but it will cumulatively total all others after that. I want the number to only total between the shifts. So for example, using your formula, when I have data for the 13th Sep, then the result at 13-09-18 19:00 includes the Shift Total for 12-09-18 19:00. 

 

Thanks

Phil

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.