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

Announcements
Join us in Toronto Sept 9th for Qlik's AI Reality Tour! Register Now
cancel
Showing results for 
Search instead for 
Did you mean: 
qliksense1thomascook
Contributor II
Contributor II

Need to find the repeat count of PAN from previous year in current year

Hi all,

I have two fields, PAN_NUMBER and TRADE_DATE. PAN_NUMBER will repeat as the customer comes again. I want to show the count of Distinct PAN_NUMBER for a year, Total count of PAN_NUMBER for a year, How many Customers repeated in current year, How many customers from previous year were repeated in current year.

Any help or idea is appreciated

Thanks in advance.

Labels (3)
3 Solutions

Accepted Solutions
BrunPierre
Partner - Master II
Partner - Master II

 

Dimension 

Aggr(If(Count({$<TRADE_DATE = {">=$(=YearStart(Today(), -1))<=$(=YearEnd(Today(), -1))"}>} PAN_NUMBER) > 0, 'Repeat Customer', 'New Customer'), PAN_NUMBER)

Measures

CYV = Count({$<TRADE_DATE = {">=$(=YearStart(Today()))<=$(=Today())"}>} PAN_NUMBER)

PYV= Count({$<TRADE_DATE = {">=$(=YearStart(Today(), -1))<=$(=YearEnd(Today(), -1))"}>} PAN_NUMBER)

View solution in original post

howdash
Creator II
Creator II

Oh good catch! I've created a video showing how to find repeat customers for any year that you would like. Video is below.

The short answer is that you will need more than just an expression to make this work.

You would need to create:

  • An alternate state
  • A Trade Year dimension in the data model
  • Couple of filters for the Trade Year dimension in the default and alternative state
  • And expressions that will identify repeat customers for trade years that user selects

An example of an expression that you would create to identify repeat customers in this setup would be something like this:

If(Count(transactionId) > 0 and Count({alt} transactionId) > 0, Count(transactionId), 0)

and this one:

If(Count(transactionId) > 0 and Count({alt} transactionId) > 0, Count({alt} transactionId), 0)

 

Once you have the alternate state created and filters added, these two expressions will generate a table like this:

howdash_0-1756854063102.png

The users will then be able to select any trade year and a comparison trade year and see the repeat customers for selected years.

For reference, attached is the app and the spreadsheet that I've used in the video.

View solution in original post

BrunPierre
Partner - Master II
Partner - Master II

You may need to create separate measures for that. For instance, for customers from 2022 that also appear in 2025, you could do something like this:

=Count(DISTINCT {
<TRADE_DATE = {">=$(=YearStart(Today()))<=$(=YearEnd(Today()))"},
PAN_NUMBER = P({<TRADE_DATE = {">=$(=YearStart(AddYears(Today(), -3)))<=$(=YearEnd(AddYears(Today(), -3))"}>} PAN_NUMBER)
>} PAN_NUMBER)

View solution in original post

11 Replies
Chanty4u
MVP
MVP

Try this 

Count(DISTINCT

  {<

    TRADE_YEAR = {"=$(=Max(TRADE_YEAR))"}

  >}

  PAN_NUMBER *

  {<

    TRADE_YEAR = {"=$(=Max(TRADE_YEAR)-1)"}

  >}

  PAN_NUMBER

)

BrunPierre
Partner - Master II
Partner - Master II

=Count(DISTINCT {<TRADE_DATE={$(=Max(TRADE_DATE))}, PAN_NUMBER = p({<TRADE_DATE={$(=Max(TRADE_DATE)-1)}>} PAN_NUMBER)>} PAN_NUMBER)

howdash
Creator II
Creator II

To show count of distinct PAN_NUMBER for a year, you could write an expression like this:

Count(distinct PAN_NUMBER)

Then have a user select a year in the Year filter to show count of distinct PAN_NUMBER for selected year.

 

To show total count of PAN_NUMBER for a year, you would simply remove the distinct keyword from the expression, like this:

Count(PAN_NUMBER)

When user will select a year in a Year filter, they will get the total PAN_NUMBER for selected year.

 

To help you come up with an expression that calculates how many Customers repeated in current year, I would need more details on what you mean by "PAN_NUMBER will repeat as the customer comes again".

  • Is there a CUSTOMER_ID field?
  • Are values in PAN_NUMBER field supposed to be IDs that holds a representation of customer ID and an order number?
  • Can a PAN_NUMBER value be the same for different customers?
  • What is PAN_NUMBER?

Without understanding these details, it's hard to come up with accurate expression to answer the "how many customers repeated in current year?" and "how many customers from previous year were repeated in current year?" questions.

If you can provide some additional insight around what PAN_NUMBER is and how "a customer" is identified in the data, I can see what I can do to help with creating expressions to answer the last two questions.

qliksenseadminthomascook
Contributor III
Contributor III

Hello Howdash,

Thank you for your response. Please find the below requested information and feel free to ask if anything else is needed:

1. No separate Customer ID field is available.

2. PAN_NUMBER is the supposed customer ID which is alpha numerical and is unique to each customer.

Every time a customer visits, it will be recorded with PAN_NUMBER and TRADE_DATE.

So, lets say if a customer named 'Qlik1' with PAN_NUMBER 'ABC000' visited previous year for 5 times and current year for 8 times

customer named 'Qlik2' with PAN_NUMBER 'ABC011' visited previous year for 6 times and current year for 10 times,

customer named 'Qlik3' with PAN_NUMBER 'ABC024' visited only in current year for 9 times,

 

then we have to show:

RepeatCustomerCYPY_Repeat_in_CY
Qlik185
Qlik2106
Qlik390

 

If we can get it from front end then it is okay and if we can get it from the script it will be even more helpful

Thanks in advance

qliksenseadminthomascook
Contributor III
Contributor III

Hello BrunPierre,

Thanks for the response. However, this is not working and it is giving null values.

BrunPierre
Partner - Master II
Partner - Master II

 

Dimension 

Aggr(If(Count({$<TRADE_DATE = {">=$(=YearStart(Today(), -1))<=$(=YearEnd(Today(), -1))"}>} PAN_NUMBER) > 0, 'Repeat Customer', 'New Customer'), PAN_NUMBER)

Measures

CYV = Count({$<TRADE_DATE = {">=$(=YearStart(Today()))<=$(=Today())"}>} PAN_NUMBER)

PYV= Count({$<TRADE_DATE = {">=$(=YearStart(Today(), -1))<=$(=YearEnd(Today(), -1))"}>} PAN_NUMBER)

howdash
Creator II
Creator II

Script

Calculating current year's and last year's in the script can be done like this:

// load customers
customers:
Load *
Inline [
	Customer Name, PAN_NUMBER
    Qlik1, ABC000
    Qlik2, ABC011
    Qlik3, ABC024
]
;


// load visits log
customerVisitLog:
LOAD
    PAN_NUMBER,
    TRADE_DATE
FROM [lib://howdash/Qlik Community/customerVisitsLog.xlsx]
(ooxml, embedded labels, table is Sheet1)
;


// caclculate current year's visits per customer
aggregatedVisitLog:
Load PAN_NUMBER,
	Count(PAN_NUMBER)							as currentYearNumOfVisits
Resident customerVisitLog
Where Year(TRADE_DATE) = Year(Today())
Group By PAN_NUMBER
;


// caclculate last year's visits per customer
Left Join(aggregatedVisitLog)
Load PAN_NUMBER,
	Count(PAN_NUMBER)							as lastYearNumOfVisits
Resident customerVisitLog
Where Year(TRADE_DATE) < Year(Today())
Group By PAN_NUMBER
;

 

This will generate a data model like this with current and last year's counts available in the aggregatedVisitLog table:

howdash_0-1756137466057.png

 

Front-End Expressions

If you'd like to instead have the current and last year's values be calculated in the front-end, you can add the table object with either customer name or PAN_NUMBER as the dimension and the following two measures:

Current Year's Visits:

Count({<TRADE_DATE = {">=$(=YearStart(Today()))<=$(=YearEnd(Today()))"}>} TRADE_DATE)

 

Last Year's Visits:

Count({<TRADE_DATE = {">=$(=YearStart(AddYears(Today(), -1)))<$(=YearStart(Today()))"}>} TRADE_DATE)

 

This will create a table like this:

howdash_2-1756138500913.png

 

qliksenseadminthomascook
Contributor III
Contributor III

Dear Howdash,

Thank you for this response. This will give me the data of how many times the customer visited this year and how many times the customer visited in the previous year. However, what I am looking for is how many customers from previous year were repeated in current year and their count.

Thanks for your assistance.

qliksenseadminthomascook
Contributor III
Contributor III

Hello BrunPierre,

Thank you for your response. This is working really good. Can we use this for 3 to 4 years of data as well.

Like this is showing Current Year Count, Previous Year Repeated in Current Year Count.

Can I also see 2025 Count, 2024 Repeated in 2025 Count, 2023 repeated in 2025 Count, 2022 repeated in 2025 Count

Thanks