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

Announcements
Want to see what's currently in public preview? We've pulled it all together in one place. Check it out here
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

Count for Each Record

Hi All ,

TRN ID

Date

Time

123A

6/1/2015

10:00 AM

123A

6/1/2015

10:30 AM

145A

7/1/2015

11:00 AM

156A

7/1/2015

11:00 AM

156A

7/2/2015

10:00 AM

For the Above table ,

i need to add a counter for each TRN ID sorted based on Date and Time.

And the Counter will be used as a KEY for further calculations.

Final Expected Output

TRN ID

Date

Time

Counter

123A

6/1/2015

10:00 AM

1

123A

6/1/2015

10:30 AM

2

145A

7/1/2015

11:00 AM

1

156A

7/1/2015

11:00 AM

1

156A

7/2/2015

10:00 AM

2

Regards,

Senthil

Labels (1)
1 Solution

Accepted Solutions
rwunderlich
MVP
MVP

Another approach is to use AutoNumber().

LOAD

  *,

  autonumber(recno(), TRN_ID) as Counter

Inline [

TRN_ID, Date, Time

123A, 6/1/2015, 10:00 AM

123A, 6/1/2015, 10:30 AM

145A, 7/1/2015, 11:00 AM

156A, 7/1/2015, 11:00 AM

156A, 7/2/2015, 10:00 AM

];

This approach is not dependent on the input being in TRN_ID order.

-Rob

http://masterssummit.com

http://robwunderlich.com

View solution in original post

6 Replies
senpradip007
Specialist III
Specialist III

Try this. Hope it will help.

LOAD *, if(TRN_ID <> Previous(TRN_ID), 1, Peek(Counter)+1) as Counter Inline [

TRN_ID, Date, Time

123A, 6/1/2015, 10:00 AM

123A, 6/1/2015, 10:30 AM

145A, 7/1/2015, 11:00 AM

156A, 7/1/2015, 11:00 AM

156A, 7/2/2015, 10:00 AM

];

rwunderlich
MVP
MVP

Another approach is to use AutoNumber().

LOAD

  *,

  autonumber(recno(), TRN_ID) as Counter

Inline [

TRN_ID, Date, Time

123A, 6/1/2015, 10:00 AM

123A, 6/1/2015, 10:30 AM

145A, 7/1/2015, 11:00 AM

156A, 7/1/2015, 11:00 AM

156A, 7/2/2015, 10:00 AM

];

This approach is not dependent on the input being in TRN_ID order.

-Rob

http://masterssummit.com

http://robwunderlich.com

Not applicable
Author

Hi Rob,

Thanks, Counter is working fine.

If am loading data from 2 different tables, my counter is shared between both the tables.

Hence am not able to create a key using the counters in both the files.

Tabl1:

    

TRNCounterRec in OFAC dateReceived Time
20150309-00029814115031002:28:16
20150309-00029814215040116:19:02
20150312-00004425115031203:47:29
20150312-00004425215040114:20:03
20150317-00036485115031802:37:23
20150317-000364852150401

04:47:10

Table 2:

    

TRNCounter1Real timeRelease date
20150309-00029814315:07:25150401
20150309-00029814415:28:30150401
20150312-00004425310:52:52150401
20150312-00004425413:50:40150401
20150317-00036485316:15:04150331
20150317-0003648545:51:01

150401

Counter in table 2 also should start with 1

rwunderlich
MVP
MVP

‌You can concatenate the filename with the tranid in the autonumber to get a unique count for each file.

autonumber(recno(), TRN_ID & filename())


-Rob

Not applicable
Author

Hi Bob ,

Counter Perfomance is very slow. Is there any other alternative?

rwunderlich
MVP
MVP

‌autonumber can be slow if you have many values for the second parameter. The previous/ peek method suggested by Pradip is much faster. But you do have to read the rows in order.

Rob