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

Struggling with a SUMX that involves a measure

Hello, everybody,

While doing a mashup of Jira logged time data and ERP data I ran against the wall with a seemingly simple calculation of planned costs.

I take the average cost for each employee over the last 3 months and try to multiply it by the hours that each of them were allocated for each ERP project. Just multiplying the sums is not an option because each employee has a different cost. Normally a SUMX would work, but in this case I get blank results. Is there any way to get around this? Maybe CALCULATETABLE, SUMMARIZE or something similar?

Any help would be appreciated


The measures involved and the data model:

Total Planned Cost = SUMX('Fact', 'Fact'[PlannedHours] * [Average Unit Cost])

Average Unit Cost = CALCULATE(SUM('Fact'[Cost (Balance)])/[Available hours], FILTER( ALL(DimDate), DimDate[Last3]=1))

Available hours is quite complex, but I'll put it after the screenshot.

Capture.PNG

 

 Available hours = SUMX(DimEmployeeHistory,
IF(DimEmployeeHistory[Statusas]="nedirba",0,

(IF(DimEmployeeHistory[StartDate]<=MIN(DimDate[Date]) && DimEmployeeHistory[EndDate]>=MIN(DimDate[Date]) && DimEmployeeHistory[EndDate]<=MAX(DimDate[Date]), (CALCULATE(SUM(DimDate[IsWorkday]), DATESBETWEEN(DimDate[Date], MIN(DimDate[Date]), DimEmployeeHistory[EndDate]))-1)*8*DimEmployeeHistory[Etatas],

(IF(DimEmployeeHistory[StartDate]>=MIN(DimDate[Date]) && DimEmployeeHistory[EndDate]>=MIN(DimDate[Date]) && DimEmployeeHistory[EndDate]<=MAX(DimDate[Date]), (CALCULATE(SUM(DimDate[IsWorkday]), DATESBETWEEN(DimDate[Date], DimEmployeeHistory[StartDate], DimEmployeeHistory[EndDate]))-1)*8*DimEmployeeHistory[Etatas],

(IF(DimEmployeeHistory[StartDate]>=MIN(DimDate[Date]) && DimEmployeeHistory[StartDate]<=MAX(DimDate[Date]) && (DimEmployeeHistory[EndDate]>=MAX(DimDate[Date]) || ISBLANK(DimEmployeeHistory[EndDate])), (CALCULATE(SUM(DimDate[IsWorkday]), DATESBETWEEN(DimDate[Date], DimEmployeeHistory[StartDate], MAX(DimDate[Date]))))*8*DimEmployeeHistory[Etatas],

(IF(DimEmployeeHistory[StartDate]<=MIN(DimDate[Date]) && (DimEmployeeHistory[EndDate]>=MAX(DimDate[Date]) || ISBLANK(DimEmployeeHistory[EndDate])), (CALCULATE(SUM(DimDate[IsWorkday]), DATESBETWEEN(DimDate[Date], MIN(DimDate[Date]), MAX(DimDate[Date]))))*8*DimEmployeeHistory[Etatas],
0))))))))))

1 REPLY 1
Eric_Zhang
Employee
Employee

@Anonymous

 

Though you have posted the table relationships, however it is still difficult to find why the measures don't return the expected output. Could you please post a sample pbix?

 

Or you may try to do the self troubleshooting. Try to make the formula more readable and check the result one by one. eg, if the Available hours doesn't work, try to eliminate the nested IFs and add and check them one by one.

 

Available hours =
SUMX (
    DimEmployeeHistory,
    IF (
        DimEmployeeHistory[Statusas] = "nedirba",
        0,
        (
            IF (
                DimEmployeeHistory[StartDate] <= MIN ( DimDate[Date] )
                    && DimEmployeeHistory[EndDate] >= MIN ( DimDate[Date] )
                    && DimEmployeeHistory[EndDate] <= MAX ( DimDate[Date] ),
                (
                    CALCULATE (
                        SUM ( DimDate[IsWorkday] ),
                        DATESBETWEEN (
                            DimDate[Date],
                            MIN ( DimDate[Date] ),
                            DimEmployeeHistory[EndDate]
                        )
                    )
                        - 1
                )
                    * 8
                    * DimEmployeeHistory[Etatas],
                (
                    IF (
                        DimEmployeeHistory[StartDate] >= MIN ( DimDate[Date] )
                            && DimEmployeeHistory[EndDate] >= MIN ( DimDate[Date] )
                            && DimEmployeeHistory[EndDate] <= MAX ( DimDate[Date] ),
                        (
                            CALCULATE (
                                SUM ( DimDate[IsWorkday] ),
                                DATESBETWEEN (
                                    DimDate[Date],
                                    DimEmployeeHistory[StartDate],
                                    DimEmployeeHistory[EndDate]
                                )
                            )
                                - 1
                        )
                            * 8
                            * DimEmployeeHistory[Etatas],
                        (
                            IF (
                                DimEmployeeHistory[StartDate] >= MIN ( DimDate[Date] )
                                    && DimEmployeeHistory[StartDate] <= MAX ( DimDate[Date] )
                                    && (
                                        DimEmployeeHistory[EndDate] >= MAX ( DimDate[Date] )
                                            || ISBLANK ( DimEmployeeHistory[EndDate] )
                                    ),
                                (
                                    CALCULATE (
                                        SUM ( DimDate[IsWorkday] ),
                                        DATESBETWEEN (
                                            DimDate[Date],
                                            DimEmployeeHistory[StartDate],
                                            MAX ( DimDate[Date] )
                                        )
                                    )
                                )
                                    * 8
                                    * DimEmployeeHistory[Etatas],
                                (
                                    IF (
                                        DimEmployeeHistory[StartDate] <= MIN ( DimDate[Date] )
                                            && (
                                                DimEmployeeHistory[EndDate] >= MAX ( DimDate[Date] )
                                                    || ISBLANK ( DimEmployeeHistory[EndDate] )
                                            ),
                                        (
                                            CALCULATE (
                                                SUM ( DimDate[IsWorkday] ),
                                                DATESBETWEEN ( DimDate[Date], MIN ( DimDate[Date] ), MAX ( DimDate[Date] ) )
                                            )
                                        )
                                            * 8
                                            * DimEmployeeHistory[Etatas],
                                        0
                                    )
                                )
                            )
                        )
                    )
                )
            )
        )
    )
)

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.