Forum Discussion
Issue with SUMX and Row Duplicates
Hi amitchandak ,
I would like to avoid the addition of columns if not strictly required. In any case I tried your proposed solution but the behaviour remains the same.
As soon as the two rows are duplicates, the result is double the correct amount.
| DEPARTMENTID | EMPLOYEEID | WORKPROCESSID | DATEENTRY | DURATION |
| AD/OF | E1 | E01 | 30.09.2020 00:00 | 08:00 |
| AD | E1 | E01 | 30.09.2020 00:00 | 08:00 |
| DEPARTMENTID | HOURLYRATE |
| AD/OF | 30 |
| AD | 30 |
Result of the LabourCost calculation = 480, so correct
With this data containing duplicates in the Hours Table, the result is the double amount.
| DEPARTMENTID | EMPLOYEEID | WORKPROCESSID | DATEENTRY | DURATION |
| AD/OF | E1 | E01 | 30.09.2020 00:00 | 08:00 |
| AD/OF | E1 | E01 | 30.09.2020 00:00 | 08:00 |
| DEPARTMENTID | HOURLYRATE |
| AD/OF | 30 |
| AD | 30 |
Result of the LabourCost calculation = 960
Hi, any help on this please?
I slightly changed the data and calculations and also tried a variation of the code with lookupvalue.
Original measure:
LabourCost1.1 =
VAR _Result =
SUMX (
Hours1,
[Hours1]
*
VAR _HourlyRate =
MAXX (
FILTER (
HourlyRates,
HourlyRates[DEPARTMENTID] = Hours1[DEPARTMENTID]
&& HourlyRates[YEAR] = YEAR ( Hours1[DATEENTRY] )
),
HourlyRates[HOURLYRATE]
)
VAR _HourlyRateNoValuePast =
MAXX (
FILTER (
HourlyRates,
HourlyRates[DEPARTMENTID] = Hours1[DEPARTMENTID]
&& HourlyRates[YEAR] = MIN ( HourlyRates[YEAR] )
),
HourlyRates[HOURLYRATE]
)
VAR _HourlyRateNoValueFuture =
MAXX (
FILTER (
HourlyRates,
HourlyRates[DEPARTMENTID] = Hours1[DEPARTMENTID]
&& HourlyRates[YEAR] = MAX ( HourlyRates[YEAR] )
),
HourlyRates[HOURLYRATE]
)
RETURN
IF (
NOT ( ISBLANK ( _HourlyRate ) ),
_HourlyRate,
SWITCH (
TRUE,
YEAR ( Hours1[DATEENTRY] ) < MIN ( HourlyRates[YEAR] ), _HourlyRateNoValuePast,
YEAR ( Hours1[DATEENTRY] ) < MIN ( HourlyRates[YEAR] ), _HourlyRateNoValueFuture
)
)
)
RETURN
_ResultVariation with lookupvalue:
LabourCost1.2 =
VAR _Result =
SUMX (
Hours1,
[Hours1]
*
VAR _HourlyRate =
LOOKUPVALUE (
HourlyRates[HOURLYRATE],
HourlyRates[DEPARTMENTID],Hours1[DEPARTMENTID],
HourlyRates[YEAR], YEAR ( Hours1[DATEENTRY] )
)
VAR _HourlyRateNoValuePast =
LOOKUPVALUE (
HourlyRates[HOURLYRATE],
HourlyRates[DEPARTMENTID],Hours1[DEPARTMENTID],
HourlyRates[YEAR], MIN ( HourlyRates[YEAR] )
)
VAR _HourlyRateNoValueFuture =
LOOKUPVALUE (
HourlyRates[HOURLYRATE],
HourlyRates[DEPARTMENTID],Hours1[DEPARTMENTID],
HourlyRates[YEAR], MAX ( HourlyRates[YEAR] )
)
RETURN
IF (
NOT ( ISBLANK ( _HourlyRate ) ),
_HourlyRate,
SWITCH (
TRUE,
YEAR ( Hours1[DATEENTRY] ) < MIN ( HourlyRates[YEAR] ), _HourlyRateNoValuePast,
YEAR ( Hours1[DATEENTRY] ) < MIN ( HourlyRates[YEAR] ), _HourlyRateNoValueFuture
)
)
)
RETURN
_Result
I tested these 2 measures on 3 different Hours Tables with 2 rows only. Only the Hours3 Table contains duplicate rows.
HOURLYRATES Table
HOURS1 Table
HOURS2 Table
HOURS3 Table
For each of these 3 Hours Tables I tested both measure variations, returning the following result.
The total duration in all Hours Tables i 16 hours and the Hourly rate to be considered is 25€/h. Consequently the correct result for labour cost is 400€, which is correct for the tables (Hours 1 and Hours 2 ) with no duplicate rows. The measures for Table Hours 3, containing duplicate rows, return 800€ as a result, which is incorrect.
The attached the PBix sample file under the link below:
Best regards