Count distinct rows

Post Reply
manu0521
Posts: 124
Joined: Wed Nov 26, 2014 8:32 pm
OLAP Product: IBM TM1, Planning Analytics
Version: PA 2.0.5
Excel Version: 2016

Count distinct rows

Post by manu0521 »

Hi ,

I have a transaction file that i load in ti , user is asking for a requirement to count the number of distinct invoices by customer and report as a measure.

I have done this by loading in sql and then doing a distint count by invoice for each customer and then loaded that to tm1.

But is there a way to perform distinct count directly in tm1 ti process.

Thanks,
ascheevel
Community Contributor
Posts: 312
Joined: Fri Feb 15, 2013 5:49 pm
OLAP Product: TM1
Version: PA 2.0.9.1
Excel Version: 365
Location: Minneapolis, USA

Re: Count distinct rows

Post by ascheevel »

Would I be correct in assuming an invoice could be represented on multiple rows of your transaction file so a simple row count won't work?

You could populate a temporary dim in metadata of your import process with the invoice numbers and then do a DIMSIZ in the epilog before deleting the dimension. You could also make it a permanent dim and group the invoices by time period and then ELCOMPN the specific time period to get invoice count.
Emixam
Regular Participant
Posts: 156
Joined: Tue May 21, 2019 3:33 pm
OLAP Product: TM1
Version: PA 2.0.x
Excel Version: 2016
Location: The Internet

Re: Count distinct rows

Post by Emixam »

I think Ascheevel's solution is the best but if you want to do everything within a TI, you should do something like this :

Code: Select all

#=====================================
# Prolog
#=====================================

sStringOfInvoices = ''
nDistinctInvoices = 0;

#=====================================
# Data
#=====================================

IF( SCAN( TRIM( '_' | UPPER( vInvoice ) | '_' ), UPPER( sStringOfInvoices ) ) = 0 );
	nDistinctInvoices = nDistinctInvoices + 1;
	sStringOfInvoices = sStringOfInvoices | '_' | vInvoice | '_' ;	
ENDIF;
nDistinctInvoices will hold the number of distinct invoices.

PS: A string variable can be no more than 8000 characters long. Depending on the number of invoices you have in your file, you will have to add a IF Statement using the LONG function.
Post Reply