Skip to main content
Announcements
Live today at 11 AM ET. Get your questions about Qlik Connect answered, or just listen in. SIGN UP NOW
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

How to compare two fields using set analysis?

Hello,

I want to compare two fields in my set analysis expression.

Assume I have a table with sales data, and it contains information such as:

   

DateAgentIDSalesAmount
1/1/201611000
1/2/201612000
1/3/201611200
1/3/201621400
1/4/20162900

I also have another table with data about my sales agents:

     

AgentIDAgentNameTeamAgentTeamStartDateAgentTeamEndDate
1John DoeTeam 11/1/20161/2/2016
1John DoeTeam 21/3/201612/31/9999
2Mary JaneTeam 11/1/201612/31/9999

Agents can belong to multiple teams over time and their duration of time on a team will be reflected by the AgentTeamStartDate and AgentTeamEndDate values.

Assume that I have a list box for Team, as well as for Date, and an object with a set expression that is summing the Sales Amount (as well as filtering on non-relevant fields/values).

Suppose I filter down to date range 1/1/2016 - 1/3/2016 in my Date listbox, and for Team 1 in my Team listbox. Currently I would get all sales records from 1/1/2016 - 1/3/2016, not only those that were made by agents who belonged to Team 1 at the time. This is because I am joining my Sales table to my Agents table on AgentID, rather than any date field, and doesn't take into consideration the AgentTeamStartDate and AgentTeamendDate field values.

How can I write an expression so that when a user filters on a specific Team and Date range, Sales Amount is summed for only the duration that the date selection falls within?

I am thinking it would be something like this but I can't figure out the exact syntax:

Sum({<

Date = {">=AgentTeamStartDate"}

,Date = {"<=AgentTeamEndDate"}

>} SalesAmount)

Additional nuances of my application require me to use set expressions, rather than an IF statement to sum the sales amounts, and also do not allow me to change the structure of the data model.

2 Replies
swuehl
MVP
MVP

I would try to resolve this in the data model.

What your coping with is a so called slowly changing dimensions.

Please have a look at:

Slowly Changing Dimensions

IntervalMatch

IntervalMatch and Slowly Changing Dimensions

Creating Reference Dates for Intervals

edit: So one possible solution might be using IntervalMatch (added after loading the two tables in your script):

JOIN (YourSalesAgentTable)

INTERVALMATCH (Date, AgentID)

LOAD AgentTeamStartDate, AgentTeamEndDate, AgentID

RESIDENT YourSalesAgentTable;

Not applicable
Author

You can try this formula:

sum({$<AgentTeamStartDate={"<=$(=min(Date))"},AgentTeamEndDate={">=$(=max(Date))"}>} SalesAmount)

Though changing the data model , advised by swuehl, seems to be much better solution.