Skip to main content
Announcements
Qlik Connect 2025: 3 days of full immersion in data, analytics, and AI. May 13-15 | Orlando, FL: Learn More
cancel
Showing results for 
Search instead for 
Did you mean: 
shivram_l
Contributor
Contributor

Using filters while using a TOTAL statement

There's a table that is created that has 5 columns:

Date | Category | Merchant | Type | Tickets | Transactions

In the above table, Tickets is unique for a combination of Date-Category-Merchant-Type while Transaction is unique for a combination of Date-Category-Merchant (Meaning there will be duplicate values)

One of the metric we need is Ticket/Transactions. In this case, ticket is additive so we just do a sum. For transactions, I use the below statement:

sum(total <date, merchant> if(category = 'category1', aggr(max(transactions), cs_date, merchant)))

The above statement helps in getting the distinct transactions for a date-merchant combination which is then summed to get total transactions.

 

The above example is working in terms of the intended effect, but we came across a certain edge case where the logic is failing.

DateCategoryMerchantTypeTransactionTicket
01-JanC1M1T11005
01-JanC1M1T210010
01-JanC1M2T330015
01-JanC2M1T440020
01-JanC2M4T550025

 

In the above example, ideally for C1 - M1, the number of transaction based on the above TOTAL statement should be 100 and for C2 - M1 it should be 400

But I'm getting the total for C1 - M1 to be 400 and C2 - M1 to also be 400.

Beginner to Set and Total statements - so any help is appreciated. Thanks.

Labels (3)
1 Solution

Accepted Solutions
sunny_talwar

Try this

Sum(Aggr(
  Max({<category = {'C1'}>} transactions)
, cs_date, merchant))

View solution in original post

6 Replies
sunny_talwar

Have you just tried this

Sum(Aggr(Max(transactions), cs_date, merchant))

Why do you need the if statement? I am guessing category1 = C1 and category2 = C2.... If that is true and you want to see a number for C2-M1 combination, why would you need an if statement? Also, guessing cs_date = Date?

I am not sure if the above expression serves the purpose or not, but it would be easier if you can provide a better example where sample data and the field names in your expression match because right now we have some guess work to do.

shivram_l
Contributor
Contributor
Author

Yeah. I can understand why the confusion might be there. Pasting the right table and query below for reference:

 

datecategorymerchanttypetransactionticket
01-JanC1M1T11005
01-JanC1M1T210010
01-JanC1M2T330015
01-JanC2M1T440020
01-JanC2M4T550025

 

sum(total <date, merchant> if(category = 'C1', aggr(max(transaction), date, merchant)))

 

So let me try and explain why I need to have an if condition. In my dashboard, I need to have one view for each separate category. Tab-1 will be called C1 where the above statement and other such expressions will have to be only for "C1". Similar for C2, C3, and so on...

 

@sunny_talwar - I tried running the query that you shared. Facing the same problem where for C1-M1 combination I get 400, instead of 100.

This is the expression that I used

sum(if(category = 'C1' , Aggr(Max(transaction), date, merchant)))

sunny_talwar

Try this

Sum(Aggr(
  Max({<category = {'C1'}>} transactions)
, cs_date, merchant))
shivram_l
Contributor
Contributor
Author

Thanks, Sunny. Just out of curiosity. Will the same formula work if I need to do a date based filter? Something like today() - 1 or today() - 7?

sunny_talwar

It should, just make sure to use correct date format when comparing date to Today() - 1 or Today()-7 using the Date function

shivram_l
Contributor
Contributor
Author

Hey @sunny_talwar  - So I am trying to use the base solution that you'd suggested above and was trying to expand that further to the need that I have in the dashboard.

 

So, here are some of the nuances:

- The variables that I'm trying to group by in the aggr() function is dependent on the category. So for category = 'C1', might have to aggr based on date and merchant, but for category = 'C2', might have to aggr based only on date.
In this case, a nested If might work is what I'm thinking

 

- There's also a date filter I need to apply. Can't use this in the dimension since for the same metric I need to take yesterday's value and benchmark against the last 2 weeks' value. I tried the below expression but it doesn't seem to be right.

Sum(Aggr(
Max({<category = {'C1'} and date = {today() - 2} >} transactions)
, cs_date, merchant))