Number of Zeros after a non Zero number

Viewed 62

Guys I have a business case where I need to count the number of Zeros after a non Zero number in a column with transaction values in qliksense. For example,

1,000 = 3 10,000 = 4 10,500 = 2 11,510 = 1 23,415 = 0

I have tried various codes but nothing has worked so far.

Can anyone help?

2 Answers
  • Convert Value to Text,
  • Find position of last occurrence of 1-9 using FindOneOf,
  • Take part of the string after position using Mid (we need to add 1 to get after),
  • Check length of our 0 only string using Len

Here is the code:

Len(Mid(Text(Value), FindOneOf(Text(Value), '123456789', -1)+1))

One (dummy) way is to first find the last possition of any non-zero number in the string. Then to find the max possition of these and use the result to substring the main value and count the rest.

For example: if we have 11510

we'll find the last possition of each non-zero numer

Num LastPostion
1   4
2   0
3   0
4   0
5   3
6   0
7   0
8   0
9   0

Then we have to find the max value. In our case this is 4. After that we'll use mid() and len() functions

mid(11510, 4 + 1) - this will return 0 and we'll get the length (len(mid(11510, 4 + 1))). This will result in 1

The result of the script below will be:

enter image description here

RawData:
Load * Inline [
Value
1000
10000
10500
11510
23415
]; 

join

Load
 Value,
 RangeMax(One, Two, Three, Four, Five, Six, Seven, Eight, Nine) as MaxNonZero
;
Load 
  Value,
  Index(Value, '1', -1) as One,
  Index(Value, '2', -1) as Two,
  Index(Value, '3', -1) as Three,
  Index(Value, '4', -1) as Four,
  Index(Value, '5', -1) as Five,
  Index(Value, '6', -1) as Six,
  Index(Value, '7', -1) as Seven,
  Index(Value, '8', -1) as Eight,
  Index(Value, '9', -1) as Nine
Resident
  RawData
;


NoConcatenate

Data:
Load 
  Value,
  len(mid(Value, MaxNonZero + 1)) as NumberOfZeros
Resident
  RawData
;

Drop Table RawData;
Related