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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
TaroGulati
Helper III
Helper III

Time difference total issue

Hi all, 

I am having issue related to total for the below case:

In the below table i need to calculate the time difference:

TaroGulati_0-1738073306728.png

For each category I have to identify last step based on time field. If last step is "X or Z" then i have to calculate difference of time between max time for step Z and min time for step X. If last step is "Y" then difference of time between max time for step Y and min time for step X. 

 

I created the below measure and it is working at the category level but not showing the correct total:

 

SWITCH (
    CALCULATE(VALUES('Table'[Step]),FILTER('Table','Table'[Time] = MAX('Table'[Time]))),
    "Y",
             DATEDIFF(CALCULATE (
                    MIN('Table'[Time]),
                   'Table'[Step] = "X"
                ),
                CALCULATE (
                    MAX( 'Table'[Time] ),
                    'Table'[Step] = "Y"
                ),SECOND),
    "X",
                DATEDIFF(CALCULATE (
                    MIN('Table'[Time]),
                   'Table'[Step] = "X"
                ),
                CALCULATE (
                    MAX( 'Table'[Time] ),
                    'Table'[Step] = "Z"
                ),SECOND),
               
    "Z",
                DATEDIFF(CALCULATE (
                    MIN('Table'[Time]),
                   'Table'[Step] = "X"
                ),
                CALCULATE (
                    MAX( 'Table'[Time] ),
                    'Table'[Step] = "Z"
                ),SECOND))
 
This is the result: 
TaroGulati_1-1738073575178.png

As you can see total is not correct. 

 

does anyone know how to fix this?

Thanks

1 ACCEPTED SOLUTION

alright i believe this should solve your issue.

TimeDifference =
SUMX (
    SUMMARIZE (
        'Table',
        'Table'[Category],
        "TimeDiff",
            SWITCH (
                CALCULATE (
                    VALUES('Table'[Step]),
                    FILTER('Table', 'Table'[Time] = MAX('Table'[Time]))
                ),
                "Y",
                    DATEDIFF (
                        CALCULATE (
                            MIN('Table'[Time]),
                            'Table'[Step] = "X"
                        ),
                        CALCULATE (
                            MAX('Table'[Time]),
                            'Table'[Step] = "Y"
                        ),
                        SECOND
                    ),
                "X",
                    DATEDIFF (
                        CALCULATE (
                            MIN('Table'[Time]),
                            'Table'[Step] = "X"
                        ),
                        CALCULATE (
                            MAX('Table'[Time]),
                            'Table'[Step] = "Z"
                        ),
                        SECOND
                    ),
                "Z",
                    DATEDIFF (
                        CALCULATE (
                            MIN('Table'[Time]),
                            'Table'[Step] = "X"
                        ),
                        CALCULATE (
                            MAX('Table'[Time]),
                            'Table'[Step] = "Z"
                        ),
                        SECOND
                    )
            )
    ),
    [TimeDiff]
)

Did it work? ✔ Give a Kudo • Mark as Solution – help others too!

View solution in original post

6 REPLIES 6
rohit1991
Super User
Super User

hi @TaroGulati ,

Your measure is calculating correctly at the category level, but the total is incorrect because Power BI does not sum up row-level calculations for totals in a measure using CALCULATE. Instead, it evaluates the formula in the context of all data, which often leads to incorrect totals.

Fixing the Total Calculation

To ensure the correct total, modify your measure to sum up the values at the row level rather than relying on implicit aggregation:

Corrected Measure

Total Time Difference =
SUMX(
    VALUES('Table'[Category]),
    SWITCH (
        CALCULATE(VALUES('Table'[Step]),FILTER('Table','Table'[Time] = MAX('Table'[Time]))),
        "Y",
                 DATEDIFF(
                    CALCULATE ( MIN('Table'[Time]), 'Table'[Step] = "X" ),
                    CALCULATE ( MAX('Table'[Time]), 'Table'[Step] = "Y" ),
                    SECOND
                ),
        "X",
                 DATEDIFF(
                    CALCULATE ( MIN('Table'[Time]), 'Table'[Step] = "X" ),
                    CALCULATE ( MAX('Table'[Time]), 'Table'[Step] = "Z" ),
                    SECOND
                ),
        "Z",
                 DATEDIFF(
                    CALCULATE ( MIN('Table'[Time]), 'Table'[Step] = "X" ),
                    CALCULATE ( MAX('Table'[Time]), 'Table'[Step] = "Z" ),
                    SECOND
                )
    )
)

 


Did it work? ✔ Give a Kudo • Mark as Solution – help others too!

@rohit1991 thanks for the feedback. 

 

unfortunately, it is still showing the wrong total: 

TaroGulati_0-1738074972101.png

 

Please cross check for any filters on the page or the visual.
If it still continues to persist. Please share the file in DM if possible so i can have  a  look at it,


Did it work? ✔ Give a Kudo • Mark as Solution – help others too!

Hi @rohit1991 you can find the file at this link: 

Demo.pbix

alright i believe this should solve your issue.

TimeDifference =
SUMX (
    SUMMARIZE (
        'Table',
        'Table'[Category],
        "TimeDiff",
            SWITCH (
                CALCULATE (
                    VALUES('Table'[Step]),
                    FILTER('Table', 'Table'[Time] = MAX('Table'[Time]))
                ),
                "Y",
                    DATEDIFF (
                        CALCULATE (
                            MIN('Table'[Time]),
                            'Table'[Step] = "X"
                        ),
                        CALCULATE (
                            MAX('Table'[Time]),
                            'Table'[Step] = "Y"
                        ),
                        SECOND
                    ),
                "X",
                    DATEDIFF (
                        CALCULATE (
                            MIN('Table'[Time]),
                            'Table'[Step] = "X"
                        ),
                        CALCULATE (
                            MAX('Table'[Time]),
                            'Table'[Step] = "Z"
                        ),
                        SECOND
                    ),
                "Z",
                    DATEDIFF (
                        CALCULATE (
                            MIN('Table'[Time]),
                            'Table'[Step] = "X"
                        ),
                        CALCULATE (
                            MAX('Table'[Time]),
                            'Table'[Step] = "Z"
                        ),
                        SECOND
                    )
            )
    ),
    [TimeDiff]
)

Did it work? ✔ Give a Kudo • Mark as Solution – help others too!

Yes, this works. I understood what i was missing. Thanks

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

Check out the November 2025 Power BI update to learn about new features.

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors