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

Unit Conversation in Script

I have the following the following two tables . one is Unit Conversion and second one is product.

Unit_Conv:

Unit_Conv:
Load * INLINE[
PriceUnt, QuantityUnt, ConversationRate
$/liter, gal, 3.78
$/gal, liter, 0.264
$/gal, ml, 0.0002
];

 

Product:

Load * INLINE [
Product, ProductID, Price, PriceUnit, Quantity, QuantityUnit
Diesel, 1, 5, $/liter, 20, gal
Petrol, 2, 8, $/gal, 30, liter
CNG, 3, 6, $/gal, 40, ml
];

To get the total cost of a product the unit needs to  be converted according to the unit of Quantity then it should be multiplied by QuantityUnit.

For example, to get the total cost of Diesel the PriceUnit needs to be converted from $/liter to $/gal as QuantityUnit is gal. So the total cost of Diesel will be =  5 * 3.78(Unit Conversion rate) * 20 = 378 $.

 

How to achieve this in Qlik Script? Suggestion please. Test file is attached.

Labels (1)
1 Solution

Accepted Solutions
marcus_sommer

Maybe with an approach like this one:

Unit_Conv:
mapping load PriceUnt & '|' & QuantityUnt, ConversationRate;
Load * INLINE[
PriceUnt, QuantityUnt, ConversationRate
$/liter, gal, 3.78
$/gal, liter, 0.264
$/gal, ml, 0.0002
];

Product:
load *, applymap('Unit_Conv', PriceUnit & '|' & QuantityUnit, 0) * Price as TotalCosts;
Load * INLINE [
Product, ProductID, Price, PriceUnit, Quantity, QuantityUnit
Diesel, 1, 5, $/liter, 20, gal
Petrol, 2, 8, $/gal, 30, liter
CNG, 3, 6, $/gal, 40, ml
];

- Marcus

View solution in original post

2 Replies
safik003
Contributor III
Contributor III
Author

@sunny_talwar Your valuable input please.

marcus_sommer

Maybe with an approach like this one:

Unit_Conv:
mapping load PriceUnt & '|' & QuantityUnt, ConversationRate;
Load * INLINE[
PriceUnt, QuantityUnt, ConversationRate
$/liter, gal, 3.78
$/gal, liter, 0.264
$/gal, ml, 0.0002
];

Product:
load *, applymap('Unit_Conv', PriceUnit & '|' & QuantityUnit, 0) * Price as TotalCosts;
Load * INLINE [
Product, ProductID, Price, PriceUnit, Quantity, QuantityUnit
Diesel, 1, 5, $/liter, 20, gal
Petrol, 2, 8, $/gal, 30, liter
CNG, 3, 6, $/gal, 40, ml
];

- Marcus