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
Anonymous
Not applicable

Sum of hours, based on values from second table

I am trying to calculate a total sum of hours, using two tables and a relationship. My current config is like this:

 

Table 'Data':

'data'[date]

'data'[unrelated_info_a]

'data'[unrelated_info_b]

'data'[weekday]  // 1 (Mon) - 7 (Sun), is calculated based on [date] and has a relationship with 'opening_hours'[weekday].

 

Table 'Opening Hours':

'opening_hours'[day] // Mon - Sun

'opening_hours'[weekday] // 1 (Mon) - 7 (Sun) and has a relationship with 'data'[weekday].

'opening_hours'[unrelated_info_a]

'opening_hours'[unrelated_info_b]

'opening_hours'[duration] // HH:mm:ss

 

In my Data table I have a list of values. Related to my issue, this is an example:

 

dateunrelated_info_aweek day
12-10-2020<random data>1
12-10-2020<random data>1

13-10-2020

<random data>2
15-10-2020<random data>4
18-10-2020<random data>7
19-10-2020<random data>1

 

I'm having a hard time to get the total amount of hours per unique date from the opening_hours table.

 

Let's say all the 'opening_hours'[duration] values are 08:00:00 for every week day. The outcome I am trying to get is '40', since there are 5 unique dates. If one of those days was set in opening_hours with a duration of 6 hours (Sun the 18th, for example), the outcome should be '38'.

 

The reason I can't seem to solve this is because I thought I needed the product (SUMX) with the unique dates somehow, but it seems not possible to use this with multiple tables. Thus: SUMX isn't the way to go, or so it seems. Can someone give me a hand on using the correct type of formula?

1 ACCEPTED SOLUTION
DataInsights
Super User
Super User

@Anonymous,

 

Try this measure:

 

Total Hours = 
VAR vDates =
    ALL ( Data[date], Data[weekday] )
VAR vResult =
    SUMX (
        vDates,
        VAR vDate = Data[date]
        VAR vWeekday = Data[weekday]
        VAR vOpeningHours =
            FILTER ( ALL ( OpeningHours ), OpeningHours[weekday] = vWeekday )
        VAR vDuration =
            MAXX ( vOpeningHours, OpeningHours[duration] )
        RETURN
            vDuration
    )
RETURN
    vResult




Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




View solution in original post

2 REPLIES 2
DataInsights
Super User
Super User

@Anonymous,

 

Try this measure:

 

Total Hours = 
VAR vDates =
    ALL ( Data[date], Data[weekday] )
VAR vResult =
    SUMX (
        vDates,
        VAR vDate = Data[date]
        VAR vWeekday = Data[weekday]
        VAR vOpeningHours =
            FILTER ( ALL ( OpeningHours ), OpeningHours[weekday] = vWeekday )
        VAR vDuration =
            MAXX ( vOpeningHours, OpeningHours[duration] )
        RETURN
            vDuration
    )
RETURN
    vResult




Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Anonymous
Not applicable

It needed some adjustments with apostrophes for the tables, but it worked right away. Very helpful to search for some documentation to understand better on what it is doing exactly, so thanks a lot!

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.