Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Qlik Open Lakehouse is Now Generally Available! Discover the key highlights and partner resources here.
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

Calculated fields

Hi All,

I loaded EMP data by using the specified script:

EMP:

LOAD EMPNO,

     ENAME,

     JOB,

     MGR,

     HIREDATE,

     SAL,

      DEPTNO,

     IF(COMM = 'NULL',0,COMM) as COMM,

     Alt(COMM, '',0) as COMM1

FROM

[//.......QV\EMP Details.xlsx]

(ooxml, embedded labels, table is EMP);

Now I want create some calculated fields.

    (SAL+COMM) as SALARY,

     SALARY*12 as Total_SALARY,

     (SALARY/Total_SALARY)*100 as Salary_Percentage

How to achieve this.

Thanks in advance.

28 Replies
saumyashah90
Specialist
Specialist

My Bad,

Can you check this.If ot i have one more last option

EMP:

LOAD EMPNO,

     ENAME,

     JOB,

     MGR,

     HIREDATE,

     SAL,

      DEPTNO,

     IF(COMM = 'NULL',0,COMM) as COMM,

     Alt(COMM, '',0) as COMM1

FROM

[//.......QV\EMP Details.xlsx]

(ooxml, embedded labels, table is EMP);

EMP_1:

Load

EMPNO,

     ENAME,

     JOB,

     MGR,

     HIREDATE,

     SAL,

      DEPTNO,

COMM,

COMM1,

(SAL+COMM) as SALARY    

resident EMP;

Drop Table EMP;

EMP_2:

Load

EMPNO,

     ENAME,

     JOB,

     MGR,

     HIREDATE,

     SAL,

      DEPTNO,

COMM,

COMM1,

SALARY ,   

SALARY*12 as Total_SALARY

Resident EMP_1;


Drop Table EMP_1;


EMP_Final:

Load

EMPNO,

     ENAME,

     JOB,

     MGR,

     HIREDATE,

     SAL,

      DEPTNO,

COMM,

COMM1,

SALARY ,   

Total_SALARY,

(SALARY/Total_SALARY)*100 as Salary_Percentage


Resident EMP_2;


Drop Table EMP_2;


jagan
Partner - Champion III
Partner - Champion III

Hi,

The script is correct, you are getting the same number because you are dividing all row by 12, can you attach sample data and your expected output.

Regards,

Jagan.

tyagishaila
Specialist
Specialist

you can easily calculate these field by using expression, when you create Table/Chart.

It will be more easy and no error will come, by using this you will protect you from reload data again and again.

Not applicable
Author

Hi Jagan,

This is the required output.

EMPNOENAMEJOBMGRHIREDATESALCOMMDEPTNOSALARYTotal_SALARYSalary_Percentage
7369SMITHCLERK790217-Dec-80800020800312252.56204964
7499ALLENSALESMAN769820-Feb-811600300301900312256.084867894
7521WARDSALESMAN769822-Feb-811250500301750312255.604483587
7566JONESMANAGER78392-Apr-8129750202975312259.527622098
7654MARTINSALESMAN769828-Sep-8112501400302650312258.486789432
7698BLAKEMANAGER78391-May-8128500302850312259.127301841
7782CLARKMANAGER78399-Jun-8124500102450312257.846277022
7788SCOTTANALYST75669-Dec-8230000203000312259.607686149
7839KINGPRESIDENTNULL17-Nov-81500001050003122516.01281025
7844TURNERSALESMAN76988-Sep-8115000301500312254.803843074
7876ADAMSCLERK778812-Jan-8311000201100312253.522818255
7900JAMESCLERK76983-Dec-81950030950312253.042433947
7902FORDANALYST75663-Dec-8130000203000312259.607686149
7934MILLERCLERK778223-Jan-8213000101300312254.163330665
31225

Not applicable
Author

Hi,

Try this.

Employee:

Load *,

(SAL+COMM) as SALARY;

LOAD EMPNO,

     ENAME,

     JOB,

     MGR,

     HIREDATE,

     SAL,

      DEPTNO,

     IF(COMM = 'NULL',0,COMM) as COMM,

     Alt(COMM, '',0) as COMM1

FROM

[//.......QV\EMP Details.xlsx]

(ooxml, embedded labels, table is EMP);

Join (Employee)

LOAD Sum(Salary) AS TotalSalary Resident Employee;

Join (Employee)

LOAD EMPNO, (Salary/TotalSalary)*100 AS Percetage Resident Employee;

Anonymous
Not applicable
Author

Hi,

you can't use the alias name in order to do a formula in the script.

SAL+ COMM  as SALARY is correct, but to have total_Salary you should write a formula with original field:

(SAL+COMM)*12 as Total_Salary and so on for the percentage.

Elena

its_anandrjs
Champion III
Champion III

Hi,

Then try this ways

masterData:

LOAD EMPNO,

     ENAME,

     JOB,

     MGR,

     HIREDATE,

     SAL,

     COMM,

     DEPTNO,

     SAL+COMM AS SALARY

FROM

[..\..\Data\EmpData.xlsx]

(ooxml, embedded labels, table is Sheet1);

left Join

LOAD

sum(SALARY) AS Total_Salary

Resident masterData;

Left Join

LOAD

EMPNO,

num((SALARY/Total_Salary),'#,##0.00%') as Percentage

Resident masterData;

And final output you get

emp.png

Regards

Anand

Anonymous
Not applicable
Author

Hi,

For huge data,complex calculations in UI will cause performance issue.

so its better to do calculations in script.

Regards

neetha

Anonymous
Not applicable
Author

Hi Try this below code

EMP:

LOAD EMPNO,

     ENAME,

     JOB,

     MGR,

     HIREDATE,

     SAL,

      DEPTNO,

     IF(COMM = 'NULL',0,COMM) as COMM,

     Alt(COMM, '',0) as COMM1

FROM

[//.......QV\EMP Details.xlsx]

(ooxml, embedded labels, table is EMP);

Left Join(EMP)

Load

EMPNO

(SAL+COMM) as SALARY,

     SALARY*12 as Total_SALARY,

     (SALARY/Total_SALARY)*100 as Salary_Percentage

Resident table EMP;