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

Calculate the sum of a measure based on the value of another measure

Hi ,

I got a data set as shown below, (The data set loaded to qlik sense app)

    

Transaction DateDivision_IDService HoursActual Cost
2/04/2018A100
15/04/2018B120
20/04/2018C110
25/04/2018D 130
1/05/2018A11515000
10/05/2018B12017000
12/05/2018C11013500
1/05/2018D 10012000
5/06/2018A10511000
15/06/2018B13520000
5/06/2018C12518000
25/06/2018D 14022500
15/07/2018A150
17/07/2018B125
10/07/2018C130
1/07/2018D 40


Output table:
I want to compare the service hours against the actual Cost for each division. The output should looks as below,

   

Division_IDService HoursActual Cost
A22026000
B25537000
C23531500
D 24034500

Thus, the sum of each division's service hours and actual costs required to be calculated.

Note

The sum of Service hours needed to be calculated only if a respective actual cost is available.

I tried using Aggr function and other simple expressions such as,

If([Actual Cost]>0, Sum([Service Hours])) and Aggr(If(Sum([Actual Cost]))>0,[Service Hours]).

But none worked. Could anyone please help me with this regard?

Thanking you.

Kind regards,

Andy

1 Solution

Accepted Solutions
jmmolero
Partner - Creator
Partner - Creator

Hi Andy,

You can create in your data model a new field based on "Service Hours"  that take value only if "[Actual Cost] > 0":

LOAD Service Hours,

Actual Cost

if([Actual Cost] > 0 ,[Service Hours],0) as [Service Hours with Actual Cost]

...

then your expression is:

Sum([Service Hours with Actual Cost])

View solution in original post

4 Replies
tresesco
MVP
MVP

Try using Sum(If(.... instead of If(..Sum(..

Straight table:

Dim 1: Division_ID

Exp 1: Sum(If([Actual Cost]>0, [Service Hours]))

Exp 2: Sum([Actual Cost])



You might have to suppress the null values in dimension as well.

andymanu
Creator II
Creator II
Author

Hi Tresesco,

Thanks for the reply. Actually, my real data set is a different and a large one.

Apparently I have tried the method you said also where I have got the correct answer but have failed to select the entire data set when performing the manual testing.

Really, sorry about that.

Anyway, thank you again.

Kind regards,

Andy

jmmolero
Partner - Creator
Partner - Creator

Hi Andy,

You can create in your data model a new field based on "Service Hours"  that take value only if "[Actual Cost] > 0":

LOAD Service Hours,

Actual Cost

if([Actual Cost] > 0 ,[Service Hours],0) as [Service Hours with Actual Cost]

...

then your expression is:

Sum([Service Hours with Actual Cost])

andymanu
Creator II
Creator II
Author

Hi Jose,

I really like the method you have approached the issue.

Yes, it works for the given data set, however, it does not work accurately when there are "0" values for the "Service Hours".

Thanks.

Regards,

Andy