Skip to main content
Announcements
July 15, NEW Customer Portal: Initial launch will improve how you submit Support Cases. IMPORTANT DETAILS
cancel
Showing results for 
Search instead for 
Did you mean: 
lalacloudvisuals
Contributor
Contributor

How to fill a column with values based on other columns?

Hi, 

I have a table where I'd like to create a set of derived columns based on columns but I'm having a lot of difficulty with coding the logic. Any help would be highly appreciated!

Suppose my current table is such that: 

Current table:

Product TypeFruitColorColor Known? Individual ColorCount
IndividualApple N 4
IndividualGrapes YRed6
BasketAppleRed  1
BasketAppleYellow  3
BasketAppleGreen  2
BasketGrapesGreen  2
BasketGrapesRed  4
BasketOrangeOrange  3

 

Essentially, suppose fruit is being sold two ways: individually vs. combined in a basket. We want to be able to calculate the number of fruit associated with each color, with drill-down into type. If fruit is being sold individually, we either know or do not know the color. If known, we can designate the color in the "Individual Color" column. If unknown (i.e. Color Known? = N), then we can assume the color distribution based on the distribution of same fruit type in a basket and then allocate to each color.

For example, we do not know the color of individual apples but know that a basket of apples being sold contains 1 Red, 3 Yellow, and 2 Green (i.e. total 6 apples). In other words, in the basket, 17% is Red (1/6), 50% is Yellow (3/6), and 33% (2/6) is Green. Applied to the individual apples (total 4), we can assume there are 0.7 Red apples (4 * 0.17), 2 Yellow apples (4 * 0.5), and 1.3 Green apples (4 * 0.33). Assume decimals are allowed. 

As a result our final table should look like the below: 


Final data table

Product TypeFruitColorFinal ColorCountFinal Count
IndividualApple Red 0.7
IndividualApple Yellow 2
IndividualApple Green 1.3
IndividualGrapes Green 6
BasketAppleRedRed11
BasketAppleYellowYellow33
BasketAppleGreenGreen22
BasketGrapesGreenGreen22
BasketGrapesRedRed44
BasketOrangeOrangeOrange33

 

In doing so, I can then build a chart with dimensions Final Color and Fruit to get the total # of fruit for each color. 

I'm trying to implement these during the Load script. I am trying to get the Final Color logic such that:

  • if Product Type = Basket then Final Color = Color
  • if Product Type = Individual then Final Color = same as Color when Product Type = Basket 

Likewise, Final Count column logic should be:

  • if Product Type = Basket then Final Count = Count
  • if Product Type = Individual then Final Count = Individual Color Count * Distribution of Color under Basket

For Final Count, I tried creating an intermediary column in the load such that: 

 

Load * ,

sum(Total <[Product Type], [Fruit], [Color]> [Count]) as [Calculation1]

 

 

However I ran into an error saying that Sum only allows 1 parameter even though the sum formula works fine as an expression when creating a table. 

Thanks in advance!

 

Labels (3)
2 Replies
rubenmarin

Hi, are you sure the error is because of that row of code? it only shows one parameter for sum.

You can try with rangesum if you need to add more than one field.

Or upload a sample so we can take a look.

Brett_Bleess
Former Employee
Former Employee

Here is the Help link for Sum as used in the Script, which is potentially different than what is allowed in an Expression, so be sure to compare things:

https://help.qlik.com/en-US/qlikview/April2019/Subsystems/Client/Content/QV_QlikView/Scripting/Aggre...

I agree with Ruben otherwise, you likely will have to attach a sample app etc. in order for folks to be able to help further, as this is one of those where you likely need to see a lot more in order to try to figure out what is going wrong.

Regards,
Brett

To help users find verified answers, please do not forget to use the "Accept as Solution" button on any post(s) that helped you resolve your problem or question.
I now work a compressed schedule, Tuesday, Wednesday and Thursday, so those will be the days I will reply to any follow-up posts.