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

script for proportion for a certain column

I'd like to make a proportion column for sales cloumn.

How can I make a script.

please help me.

   

NameSalesProportion(%)
A3010%
B4014%
C6021%
A207%
B103%
C7024%
A5017%
B4014%
7 Replies
petter
Partner - Champion III
Partner - Champion III

Which proportion are you aiming to calculate? It seems like you have 110% if you consider all of your data. Shouldn't it be 100%?

Energetic
Contributor III
Contributor III
Author

It's something wrong sorry for that.

I changed sample data and I'd like to get the sales proportion for each name.

   

NameSalesProportion(%)
A3011.50%
B7026.90%
C207.70%
D6023.10%
E4015.40%
F3011.50%
G103.80%
PrashantSangle

can you give more clarity on proportion logic??

Regards,

Great dreamer's dreams never fulfilled, they are always transcended.
Please appreciate our Qlik community members by giving Kudos for sharing their time for your query. If your query is answered, please mark the topic as resolved 🙂
PrashantSangle

Try expression

sum(Sales) / Sum(Total Sales)

then select show in %

Regards,

Great dreamer's dreams never fulfilled, they are always transcended.
Please appreciate our Qlik community members by giving Kudos for sharing their time for your query. If your query is answered, please mark the topic as resolved 🙂
petter
Partner - Champion III
Partner - Champion III

I would recommend to do it like Prashant proposed directly in the UI in a chart, straight table or pivot table and not in the script.

Then you can use name as a dimension and Sum(Sales) / Sum(TOTAL Sales) as expression in a measure.

However if you really need to do it in the script this is a way of doing it:

RAW_DATA:

LOAD

  *,

  If(RecNo() = 1 , Sales , Peek('AccSum')+Sales) AS AccSum

INLINE [

Name,Sales

A,30

B,40

C,60

A,20

B,10

C,70

A,50

B,40

];

TotSum = Peek('AccSum',-1,'RAW_DATA');

DATA:

LOAD

  Name,

  Sales,

  Num(Sales/$(TotSum),'0%','.',',') AS Prop

RESIDENT

  RAW_DATA;

 

DROP TABLE RAW_DATA;

TotSum=;

All this is not neccessary if you do the calculation on the fly as suggested earlier. An additional disadvantage of doing it in the script is much less flexibility. If the user for instance selectes specific names the total sum will not be adjusted to only the selected names.

RAW_DATA: LOAD   *,  If(RecNo() = 1 , Sales , Peek('AccSum')+Sales) AS AccSum INLINE [Name,SalesA,30B,40C,60A,20B,10C,70A,50B,40];
TotSum = Peek('AccSum',-1,'RAW_DATA');
DATA:LOAD  Name,  Sales,  Num(Sales/$(TotSum),'0%','.',',') AS PropRESIDENT  RAW_DATA;  DROP TABLE RAW_DATA;TotSum=;

PrashantSangle

Nice solution sir.

we can also try like if we need to find proportion w.r.t. all

then

try

Sales_table:

Load * from table;

Join

Load Sum(Sales) as Total_Sales from Table

group by Name;

sales_with_proportion:

Load *, Sum(Sales)/ Sum(Total_Sales) as Proportion

Resident Sales_table

group by Name;

Drop table Sales_table;

Regards,

Great dreamer's dreams never fulfilled, they are always transcended.
Please appreciate our Qlik community members by giving Kudos for sharing their time for your query. If your query is answered, please mark the topic as resolved 🙂
Anonymous
Not applicable

Hi Sanghyuck

kindly try this

Table:

LOAD Name,

     Sales,

     '' as temp

FROM

[1.xlsx]

(ooxml, embedded labels, table is Sheet6);

left join(Table)

load

temp,

sum(Sales)as Total

Resident Table Group by temp;

Final:

load

Name,

Sales,

Sales/Total as Proportion

Resident Table;

DROP table Table;