# QlikView Scripting

Discussion Board for collaboration on QlikView Scripting.

New Contributor III

## Help needed while calculating a percentage in a text object field

Hello,

I'm trying to calculate a percentage in a text object field.

So far, by using the bellow expression I was able to get the result I want, but it gives me the full % value. But what I really need it a round number with no decimals, or max two decimals numbers.

My values are:

CC: 69

Other: 18

Total (CC + Other) = 87

My expression is:

=count(if([Tipo de contacto]='CC',[Tipo de contacto]))/count([Tipo de contacto])*100 & ' %'

Value gotten so far: 79,310344827586 %

I'm looking for an expression that gives me something like: 79% or 79,31% (with only two decimals)

Many thanks for your help.

Tags (1)
1 Solution

Accepted Solutions
Contributor III

## Re: Help needed while calculating a percentage in a text object field

You can use the Num function to format the number, below should give you 2 decimal places

Num(count(if([Tipo de contacto]='CC',[Tipo de contacto]))/count([Tipo de contacto])*100,'##0.##')

or

no decimal places

Num(count(if([Tipo de contacto]='CC',[Tipo de contacto]))/count([Tipo de contacto])*100,'##0')

https://help.qlik.com/en-US/qlikview/November2017/Subsystems/Client/Content/Scripting/FormattingFunc...

3 Replies
Contributor III

## Re: Help needed while calculating a percentage in a text object field

You can use the Num function to format the number, below should give you 2 decimal places

Num(count(if([Tipo de contacto]='CC',[Tipo de contacto]))/count([Tipo de contacto])*100,'##0.##')

or

no decimal places

Num(count(if([Tipo de contacto]='CC',[Tipo de contacto]))/count([Tipo de contacto])*100,'##0')

https://help.qlik.com/en-US/qlikview/November2017/Subsystems/Client/Content/Scripting/FormattingFunc...

New Contributor III

## Re: Help needed while calculating a percentage in a text object field

It is correct, it works:

=Num(count(if([Tipo de contacto]='CC',[Tipo de contacto]))/count([Tipo de contacto])*100,'##0,##')& ' %'

This gives the result I want. Many thanks!

MVP

## Re: Help needed while calculating a percentage in a text object field

Also, suggest you to look into set analysis rather than using if statement

=Num(Count({<[Tipo de contacto] = {'CC'}>} [Tipo de contacto])/Count([Tipo de contacto]), '#0.0%')

or this if you want it to be 0 when CC is not selected in Tipo de contacto

=Num(Count({<[Tipo de contacto] *= {'CC'}>} [Tipo de contacto])/Count([Tipo de contacto]), '#0.0%')