on 02-26-2016 5:02 PM
Hi Guys,
I have a scenario to output the result of the sample formula below.
If TYPE = A, then Total1 = (RATE/UNIT) * NUM
Else If TYPE = X, then Total2 = ((RATE/UNIT) * (DEN/NUM)) * NUM
There are instances that in the PRICE segment, the segment with type X comes first.
When I perform the test, incorrect value is produce.
Below are my Scenarios. Scenario 1 outputs the correct result and Scenario 2 outputs the incorrect result.
Scenario 1:
<CONVERT>
<COUNT>
<NUM>2</NUM>
<DEN>1</DEN>
</COUNT>
<COUNT>
<NUM>1000</NUM>
<DEN>1250</DEN>
</COUNT>
<CONVERT>
<SALES>
<PRICE SEGMENT="1">
<RATE>37764.00</RATE>
<UNIT>1000</UNIT>
<TYPE>A</TYPE>
</PRICE>
<PRICE SEGMENT="1">
<RATE>0.22</RATE>
<UNIT>1</UNIT>
<TYPE>X</TYPE>
</PRICE>
</SALES>
Correct Output:
Total1: 75.528 = (37764/1000) * 2
Total2: .55 = ((0.22/1) * (1250/1000)) * 2
Scenario 2:
<CONVERT>
<COUNT>
<NUM>2</NUM>
<DEN>1</DEN>
</COUNT>
<COUNT>
<NUM>1000</NUM>
<DEN>1250</DEN>
</COUNT>
</CONVERT>
<SALES>
<PRICE SEGMENT="1">
<RATE>0.22</RATE>
<UNIT>1</UNIT>
<TYPE>X</TYPE>
</PRICE>
<PRICE SEGMENT="1">
<RATE>37764.00</RATE>
<UNIT>1.000</UNIT>
<TYPE>A</TYPE>
</PRICE>
</SALES>
Incorrect Output:
Total1: 75.528 = (37764/1000) * 2
Total2: ((0.22*1) * (1/2)) * 1
Here's the issue on the second scenario, in the DEN/NUM, the first occurrence of segment COUNT is used for the TYPE = X.
It should always used the second occurrence of COUNT for the computation for TYPE = X.
Could you please help me on this.
Hi Neil
This is not a context issue as the fields in <COUNT> and <PRICE> are under different parents and therefore considered "unrelated". The issue is more related to the sorting of the PRICE segment, as it looks like your preferred sorting is for TYPE = A to come first.
One alternative is to have an intermediate mapping that sorts the price segment before your current mapping. This intermediate mapping will have a 1-1 mapping for all fields, apart from those in PRICE which will be sorted.
Refer below for an example:-
Sort Rate based on Type (ascending) using SortByKey function. Change context of Type and Rate to one level higher. Repeat the similar logic for Unit's mapping.
For mapping of Type, just perform sort. Change content of Type to one level higher.
With this, the results will have the Price segment sorted where Type A will be first.
Once you have this, the next mapping step can process your current logic accordingly.
Regards
Eng Swee
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Eng,
Great! This one works. Tried different scenarios and the output are produced as expected.
Although, I got some problem on the other fields that sometimes are empty (really empty fields, never used). For these fields, I just mapped them directly since they are not used in the computation.
I used this logic on the fields that I am sure contains values.
Thanks,
Neil
Hi Neil,
You can use copyValue[1] for type X computations.
Regards,
Mark
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Neil,
at the first glace I thought that you can optimize your calculation for total2, because r/u * d/n* n = r/u *d. But then I realized that you want use the NUM value of the first context always for the secound NUM in your calculation.
Maybe you can use a combination of udfs to achieve this:
One snipped can be for always returing the last value of an context (execution type: value of contexts)
if (contextValues != null && contextValues.length > 0) {
String value = ResultList.SUPPRESS;
for (int i = 0; i < contextValues.length; i++) {
String str = contextValues[i];
if (str != null && !ResultList.SUPPRESS.equalsIgnoreCase(str)) {
value = str;}}
result.addValue(value);
}
So it would look like this:
If TYPE = X, then Total2 = ((RATE/UNIT) * ( getLastContextValue{setContext higher(DEN/NUM)} )) * NUM
Kind regards
Konrad
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
76 | |
9 | |
8 | |
7 | |
6 | |
5 | |
5 | |
5 | |
5 | |
5 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.