Skip to main content
Announcements
Qlik Connect 2024! Seize endless possibilities! LEARN MORE
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

Help with max() - set analysis

I have an expression in one of my table columns

sum ($ {<year = {'> = 2006'}, month = {'>=$(=(max ({$<year = {$(=max(year))}>} months) - (v_month - 1), 00 ))'}>} consumption)

the expression works, but inside the SUM it does not consider the dimension of my table (drugs),in other words the expression get the Max month of every drug and not the Max of each drug.

If I select only one product in the table, it will get the correct corresponding months of this drug.

I tried to include the drug in the expression

sum ($ {<year = {'> = 2006'}, month = {'>=$(= (max ({$ year = <drugs, {$(=max(ano))}>} months) - (v_month - 1), 00 ))'}>} consumption)

and also tried to put the max (month) in the script, but most of the values is null.

Any Idea???



Sorry for my English mistakes, I'm from Brazil and google translator does not help;.

1 Solution

Accepted Solutions
johnw
Champion III
Champion III

Your example says, "If you select only one product, you will see the right value." But that seems wrong.

If I select ds_drug = x, then max(month_num) = 12. But since there's no connection between this month and the dt_consumption, we always sum all consumption for x regardless of the month. The same is true of selecting the other drugs individually - they always sum all consumption regardless of month.

In other words, if you DO see the right values if you select only one product, then this expression solves the problem:

sum(consumption)

It gives you the values you say you want.

I would GUESS that what you want is to instead only sum the consumption where the dt_consumption is in the given month regardless of year. If so, you need to fix the data model as a first step. You would want to replace this:

consumption2:
load cod_drug ,
month(dt_consumption) as month_drug,
Right(left(dt_consumption,5),2) as month_num,
year(dt_consumption) as year_drug
resident consumption;

With this:

LEFT JOIN (consumption)
LOAD
cod_drug
,dt_consumption
,month(dt_consumption) as month_drug
,num(month(dt_consumption),'00') as month_num
,year(dt_consumption) as year_drug
RESIDENT consumption;

I then guess you want actual results like this:

x has max month of 12, and we have one dt_consumption in that month, for 4789
y has max month of 5, and we have one dt_consumption in that month, for 1005
z has max month of 8, and we have two dt_consumption in that month, for 582 + 3065 = 3647

With the data model changes, you can then get those results like this:

aggr(if(month_num=max(total <ds_drug> month_num),sum(consumption)),ds_drug,month_num)

One way to see what that is doing is to turn the aggr() into a table. Your dimensions would be ds_drug and month_num. Add expressions for max(total <ds_drug> month_num) and sum(consumption). Hopefully you can then see how the aggr() works to produce the right result.

It's possible that there's a better solution, but that's what I came up with.

View solution in original post

7 Replies
johnw
Champion III
Champion III

You cannot refer directly to the dimension value within set analysis. A set is evaluated once for the entire chart, not once per row in the chart. There's a fairly complicated way around this...

http://community.qlik.com/wikis/qlikview-wiki/evaluating-sets-in-the-context-of-a-dimension.aspx

...but I wouldn't generally recommend it unless you have no other solution or performance requires it.

I suspect there's a better solution for your case, but I don't understand your data or requirements well enough to really know.

Can you post a QVW with a LOAD INLINE of sample data, and the chart you want to see, along with what values you want to appear in that chart? That would make it much easier to test possible solutions.

Not applicable
Author

Thanks John, it will help me too!!!

johnw
Champion III
Champion III


jealvesici wrote:Thanks John, it will help me too!!!


I'll attach an example then, but I do urge caution. For the example, the only reason that solution would be required is if we need to keep the two customer dimensions unassociated in our data model. There are normally better solutions. Normally, you'd handle something like this with data relationships, and everything would be much simpler and faster.

Anonymous
Not applicable
Author

http://community.qlik.com/media/p/156485/download.aspx

Hi John, thanks for your clues, I can't open your file 'cause I'm using the test version of Qlikview... But I downloaded.

I sending you a example of my problem, please take a look on it.

I know that I can fix this problem using SQL, but I'm trying to do using Qlikview.

Thanks again...

johnw
Champion III
Champion III

Your example says, "If you select only one product, you will see the right value." But that seems wrong.

If I select ds_drug = x, then max(month_num) = 12. But since there's no connection between this month and the dt_consumption, we always sum all consumption for x regardless of the month. The same is true of selecting the other drugs individually - they always sum all consumption regardless of month.

In other words, if you DO see the right values if you select only one product, then this expression solves the problem:

sum(consumption)

It gives you the values you say you want.

I would GUESS that what you want is to instead only sum the consumption where the dt_consumption is in the given month regardless of year. If so, you need to fix the data model as a first step. You would want to replace this:

consumption2:
load cod_drug ,
month(dt_consumption) as month_drug,
Right(left(dt_consumption,5),2) as month_num,
year(dt_consumption) as year_drug
resident consumption;

With this:

LEFT JOIN (consumption)
LOAD
cod_drug
,dt_consumption
,month(dt_consumption) as month_drug
,num(month(dt_consumption),'00') as month_num
,year(dt_consumption) as year_drug
RESIDENT consumption;

I then guess you want actual results like this:

x has max month of 12, and we have one dt_consumption in that month, for 4789
y has max month of 5, and we have one dt_consumption in that month, for 1005
z has max month of 8, and we have two dt_consumption in that month, for 582 + 3065 = 3647

With the data model changes, you can then get those results like this:

aggr(if(month_num=max(total <ds_drug> month_num),sum(consumption)),ds_drug,month_num)

One way to see what that is doing is to turn the aggr() into a table. Your dimensions would be ds_drug and month_num. Add expressions for max(total <ds_drug> month_num) and sum(consumption). Hopefully you can then see how the aggr() works to produce the right result.

It's possible that there's a better solution, but that's what I came up with.

Anonymous
Not applicable
Author

Thanks John, you're right, my example was wrong... I got the idea of 'aggr'.

Regards.

Anonymous
Not applicable
Author

I have a doubt,

I have an average daily consumption, which depends on my variable 'v_month'

if the variable is 3 for example:

consumption is related to last month + the last two months earlier ...
(Last month = 8, consumption refers to the months 6,7,8) shared by a time period.

expression:

/ / 1st part (following the example that the variable value is 3 and the last month is the month in August, this part of the expression demonstrates the intake covering the months June, if not have consumption in month 6, the consumption value will be 0)



if (aggr (if (year_drug = max (total <ds_drug> year_drug) if (month_num = max ({<year_drug {'> = = 2006 '}>} total <ds_drug> month_num) - (v_month - 1), sum (= {<year_drug {'> = 2006 '}>} consumption))), ds_drug, month_num, year_drug)
, Aggr (if (year_drug = max (total <ds_drug> year_drug) if (month_num = max ({<year_drug {'> = = 2006 '}>} total <ds_drug> month_num) - (v_month - 1), sum ( = {<year_drug {'> = 2006 '}>} consumption))), ds_drug, month_num, year_drug)
, 0)

+

/ / 2nd part (following the same example, this part of the expression should show the consumption for the months longer than the six, if does'nt exist greater months the value will be 0)

if (
aggr (if (year_drug = max (total <ds_drug> year_drug) if (month_num> max ({<year_drug {'> = = 2006 '}>} total <ds_drug> month_num) - (v_month - 1), sum ({ <year_drug {'> = = 2006 '}>} consumption))), ds_drug, month_num, year_drug)
, Aggr (if (year_drug = max (total <ds_drug> year_drug) if (month_num> max ({<year_drug {'> = = 2006 '}>} total <ds_drug> month_num) - (v_month - 1), sum ( = {<year_drug {'> = 2006 '}>} consumption))), ds_drug, month_num, year_drug)
, 2000)

/

/ / 3rd party (it's a period of time between the last date - the variable of months)

if (aggr (if (year_drug = max (total <ds_drug> year_drug) if (month_num = max ({<year_drug {'> = = 2006 '}>} total <ds_drug> month_num) - (v_month - 1), sum (= {<year_drug {'> = 2006 '}>} consumption))), ds_drug, month_num, year_drug)
, Aggr (if (year_drug = max (total <ds_drug> year_drug) if (month_num = max ({<year_drug {'> = = 2006 '}>} total <ds_drug> month_num) - (v_month - 1), sum ( = {<year_drug {'> = 2006 '}>} consumption))), ds_drug, month_num, year_drug)
, 0)

if the variable is 1 or 2, the expression works .... if the variable is greater than 3, does not work ...

(The problem is in the 2nd part ... if it is to consider the term more than one month, it does not work)

Also I have to increase the expression, a way that, if consumption is for the months 6,7,8.
and 7 months, for example, there is no consumption, adding only the consumption of the other months, which have (6.8)



I sending an example to help you to understanding my problem...

http://community.qlik.com/media/p/156732/download.aspx