Skip to main content
Announcements
Qlik Connect 2024! Seize endless possibilities! LEARN MORE
cancel
Showing results for 
Search instead for 
Did you mean: 
markgraham123
Specialist
Specialist

Swapping highest value with lowest value

Hi all,

I'm working on a requirement where i have to :

  • Select only Hour - '9' and swap SKUs of highest 2 Qty items of Area 'A' with lowest 4 items of Area 'B' 
  • example is exampled below:

In below example at Hour - '9',     Area 'A' SKUs - 1 and 3 has highest qty.  So, i want to swap them with SKUs 13 and 14 from Area B. Below is sample data.

Attached is the QV file with data. Any help is highly appreciated. Please advise.

OrderHourSKUQtyLocationArea
A9122A-1A
B9121A-1A
C9140A-1A
L9110A-1A
D8212A-2A
E8213A-2A
F8210A-2A
G10341A-3A
H11345A-3A
I9312A-3A
J7411A-4A
K8443A-4A
M9510B-1B
N963B-2B
O1071B-3B
P1188B-4B
Q1292B-5B
R13102B-6B
S14113B-7B
T9121B-8B
U9131B-9B
V9147B-10B
W9153B-11B

 

Labels (4)
1 Solution

Accepted Solutions
sunny_talwar

May be this

 

T1:
LOAD Hour,
	 SKU,
	 Sum(Qty) as Qty,
	 If(Area = 'A', Sum(Qty), -Sum(Qty)) as Qty_Temp,
	 Location,
	 Area
Group By Hour, SKU, Location, Area;
LOAD * INLINE [
    Order, Hour, SKU, Qty, Location, Area
    A, 9, 1, 22, A-1, A
    B, 9, 1, 21, A-1, A
    C, 9, 1, 40, A-1, A
    L, 9, 1, 10, A-1, A
    D, 8, 2, 12, A-2, A
    E, 8, 2, 13, A-2, A
    F, 8, 2, 10, A-2, A
    G, 10, 3, 41, A-3, A
    H, 11, 3, 45, A-3, A
    I, 9, 3, 12, A-3, A
    J, 7, 4, 11, A-4, A
    K, 8, 4, 43, A-4, A
    M, 9, 5, 10, B-1, B
    N, 9, 6, 3, B-2, B
    O, 10, 7, 1, B-3, B
    P, 11, 8, 8, B-4, B
    Q, 12, 9, 2, B-5, B
    R, 13, 10, 2, B-6, B
    S, 14, 11, 3, B-7, B
    T, 9, 12, 1, B-8, B
    U, 9, 13, 1, B-9, B
    V, 9, 14, 7, B-10, B
    W, 9, 15, 3, B-11, B
];

Temp:
LOAD *,
	 If(Area = Previous(Area) and Hour = Previous(Hour), RangeSum(Peek('Rank'), 1), 1) as Rank
Resident T1
Order By Area, Hour, Qty_Temp desc, Location;

DROP Table T1;

Left Join (Temp)
LOAD Rank,
	 Hour,
	 SKU as SKU_A
Resident Temp
Where Area = 'A' and Hour = 9;

Left Join (Temp)
LOAD Rank,
	 Hour,
	 SKU as SKU_B
Resident Temp
Where Area = 'B' and Hour = 9;

Final:
LOAD Area,
	 Hour,
	 Location,
	 If(Len(Trim(SKU_A)) > 0, If(Area = 'A', SKU_B, SKU_A), SKU) as SKU
Resident Temp;

DROP Table Temp;

 

View solution in original post

16 Replies
jonathandienst
Partner - Champion III
Partner - Champion III

Its not clear what you mean by "swap". Do you want to manipulate the data during load? If you want to do this in the front end, provide some detail on the object yous are using (pivot table, bar chart etc) and exactly what output you want for the given input.

Also I don't see four values for A or B at some time values. How do you want to handle this case?

Logic will get you from a to b. Imagination will take you everywhere. - A Einstein
markgraham123
Specialist
Specialist
Author

Hi, Sorry for confusion caused.

I want to do this in script.  I want to apply the logic for hour - 9.

If 4 SKUs are not available, i want the logic to take whatever is available. In this case,  2 SKUs.

The logic should take SKU - 1 and 3 from Area 'A' (as they are high qty) and SKU - 12 and 13 (as they are less qty) from Area 'B' and then swap their locations.

So, final O/P would be like below highlighted. Please help.

AreaHourLocationSKU
A7A-44
A8A-22
A8A-44
A9A-112
A9A-313
A10A-33
A11A-33
B9B-15
B9B-26
B9B-81
B9B-93
B9B-1014
B9B-1115
B10B-37
B11B-48
B12B-59
B13B-610
B14B-711

 

 

markgraham123
Specialist
Specialist
Author

@sunny_talwar  Any help you do is highly appreciated 🙂

sunny_talwar

I am not sure I understand the logic here.... A-1 has multiple rows in the input, but within output we don't have Qty and the multiple rows become a single row? Does that mean that we don't need Qty and Order field to be part of the output?

Also, you initially mentioned highest 2 Qty items of Area 'A' with lowest 4 items of Area 'B'? What 4 items are we talking about? It seems you did a 2 - 2 swap? And what was the logic to assign A-1 with SKU 12 and A-3 with SKU 13? What if B had another SKU with Qty 3... let's say 14... now which ones will be swaped? 12, 13 or 12, 14 or 13, 14?

sunny_talwar

Actually, there are 3 Area B with 1 Qty... 7, 12, 13... why did you not include 7 in your swap?

image.png

markgraham123
Specialist
Specialist
Author

Yes. we don't need Order and qty in output. They are used to calculate sum(qty) by location in the area in the logic.

To swap, we are comparing A Vs B.

In this example, i only have 2 items at Hour 9 in 'A'.

So, i take those 2 highest items swap it with lowest 2 items in 'B' with lowest qty at '9'

 

 

 

markgraham123
Specialist
Specialist
Author

Because SKU '7' transaction has occurred at Hour - '10'.

I only want to look at transactions happened at Hour - '9'

sunny_talwar

Okay, now why are we doing this for only Hour 9? What about if there was an hour 10? Would you want to do it from Hour 9 and Hour 10 or just Hour 9 or just Hour 10? Also, what if there was another Qty 1 in Area B for Hour 9? What would you have done then?

sunny_talwar

Sorry, I didn't realize that you already have hour 10 and 11... so this seems like a very specific logic for hour 9. That works, but what if there were three Qty 1s in Area B? against 2 values in Area A.