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

Set Analysis with multiple variables

Hi all,

I am trying to make a set analysis  with multi set analysis's, including variables.

I have tried several options/methods, the latest is maybe the easiest to read, see below.

 

Note: In my table not every Row is acceptable due to wrong milliage.

Thats why I have build a qualifier (MeasureAcceptable). Can somebody tell me how I can solve this?

 

=Sum({
$<
TRANSACT_DATE={
">$(=min({$<MeasureAcceptable = {'J'}, $PRODUCT_CODE={'1'}>}TRANSACT_DATE))"

"<=$(=max({$<MeasureAcceptable = {'J'}, $PRODUCT_CODE={'1'}>}TRANSACT_DATE))"

}
> }TotalGallons)

Labels (1)
2 Solutions

Accepted Solutions
crusader_
Partner - Specialist
Partner - Specialist

Hi,

Firstly I encourage you to install QS Feb 2019 release as soon as it has amazing feature to "expand formulas", e.g. to show what exactly is passed to the engine as per picture below:

1554105#M9119.PNG

Another problem you had was separating search string... for some reason if you write

 

{"
<$(=...)

works differently to

 

 

{"<$(=...)...

One more thing: never apply function on field in set analysis like you did in example:

 

 

...
{< Date(TRANSACT_DATE)={....}>}

Above function Date() will never work.

Next, values returned inside set analysis should have exactly the same format as your field, e.g. if your field is loaded as number 43523, then in set analysis you must have the same numeric format, if it's a date, then it should be a date and so on.

The last, but not the least, from performance point of view NEVER keep timestamps in your Qlik data model, because you won't get compression on that field as soon as every value will be unique (as every moment in our life 🙂 ) and for the same day, say March, 9th, you can get 86400 unique timestamps on "seconds" level (or 86400000 if you have ms included). So better to split Date and Time separately, if you don't need to report on time - get rid of it, if you need hours, keep only 24 hours, etc..

PFA example with working formula.

Hope this helps

und Vielen Glück!

//Andrei

 

View solution in original post

crusader_
Partner - Specialist
Partner - Specialist

Never hesitate to ask, how otherwise you could learn 🙂 ?

First of all, have a look into 3 expression like on picture below. #1 is a manual one which explains what we want to achieve, #2 and #3 are exactly the same thing, just made with different approaches. (All of them are exactly the same)

Expressions.PNG

From Manual (#1) expression you would notice that SUM(...) part is exactly the same except vehicleID, as soon as we don't want to write it manually and moreover maintain it manually, we're creating a parameterised variable vExpression  (<you can call it whatever name, but don't forget to refer to it properly>) where defining business rule

Variable_definition.PNG

You can pass a parameter into variable instead of value like on below screenshot

Variable_description.PNG

Once you defined variable you would want to call it in your chart like $(vExpresssion(13133)) :Expression_trick.PNG

As you see we're using vExpression - text as a prototype for future variable usage (# sign is used to prevent immediate calculation - because we need to build full expression first). Concat() function - just concatenates values into single string, so our interim goal to get a text line as below:

This expression evaluated into below stringThis expression evaluated into below string

Our interim goalOur interim goal

And the last step is just replace #-sign with $-sign - allowing the text become a real variable and calculate results.

 

I've added manual expressions for mileage in attached example, so you can try to generate it in the same way as it's done for Gallons.

NOTE: I'm using [TRN DATE] field because in set analysis number is rounded to nine digits after comma, however in load script it has 10, so TRANSACTION_DATE in numeric format will never match exactly, because 43495.516666667 is not equal to 43495.516666667, however it works in <= / >= expressions just because smaller/bigger indeed.

If the data from vehicles is not read twice (several times) a day, remove time from your data model (If you need to report on it - keep it separate)

Hope this helps

//Andrei

 

View solution in original post

16 Replies
crusader_
Partner - Specialist
Partner - Specialist

Hi,

Try to split your problem into chunks and solve small tasks...

"Search between" could be done like below:

=Sum({
$<
TRANSACT_DATE={
">$(=<formula>)<=$(=<formula>)"

}
> }TotalGallons)

Remember, that MIN() and MAX functions will return numeric values of your date, e.g. instead of 08/03/2019 you'll get 43532 and if your TRANSACT_DATE field formatted as 'DD/MM/YYYY' (any date format) your search won't work, so you need to apply exactly the same date format as your TRANSACT_DATE  field.

I could guess, that you need to write something like below:

=Sum({
$<
TRANSACT_DATE={
">$(=DATE(min({$<MeasureAcceptable = {'J'}, $PRODUCT_CODE={'1'}>}TRANSACT_DATE)))<=$(=DATE(max({$<MeasureAcceptable = {'J'}, $PRODUCT_CODE={'1'}>}TRANSACT_DATE)))"

}
> }TotalGallons)

Hope this helps.

//Andrei

aronvermeulen
Contributor III
Contributor III
Author

Thx for your help, but it still doesn't work.

Probably i am doing something wrong..

I made a small demo, maybe you can try to  see what a do wrong.

 

Many thx in advance! 

PS,

fyi;  in my original app, the field 'TRANSACT_DATE', is a datetime field.

I dont need to format it to present it is a date or time. Please see below.

This single set analysis working just fine, but the dynamic sum set analysis however....  😞

 

(Max({ <VerbruikMeetbaar={'J'}, PRODUCT_CODE={'1'}> } TRANSACT_DATE))

ExampleMAxSA.JPG

crusader_
Partner - Specialist
Partner - Specialist

Hi,

Firstly I encourage you to install QS Feb 2019 release as soon as it has amazing feature to "expand formulas", e.g. to show what exactly is passed to the engine as per picture below:

1554105#M9119.PNG

Another problem you had was separating search string... for some reason if you write

 

{"
<$(=...)

works differently to

 

 

{"<$(=...)...

One more thing: never apply function on field in set analysis like you did in example:

 

 

...
{< Date(TRANSACT_DATE)={....}>}

Above function Date() will never work.

Next, values returned inside set analysis should have exactly the same format as your field, e.g. if your field is loaded as number 43523, then in set analysis you must have the same numeric format, if it's a date, then it should be a date and so on.

The last, but not the least, from performance point of view NEVER keep timestamps in your Qlik data model, because you won't get compression on that field as soon as every value will be unique (as every moment in our life 🙂 ) and for the same day, say March, 9th, you can get 86400 unique timestamps on "seconds" level (or 86400000 if you have ms included). So better to split Date and Time separately, if you don't need to report on time - get rid of it, if you need hours, keep only 24 hours, etc..

PFA example with working formula.

Hope this helps

und Vielen Glück!

//Andrei

 

aronvermeulen
Contributor III
Contributor III
Author

Thx for your help!
aronvermeulen
Contributor III
Contributor III
Author

I cheered to soon.. 

((MAX({ <VerbruikMeetbaar={'J'}, PRODUCT_CODE={'1'}> } VerbruikKM))-(MIN({ <VerbruikMeetbaar={'J'}, PRODUCT_CODE={'1'}> } VerbruikKM)))/(Sum({
$<TRANSACT_DATE={">$(=min({$<VerbruikMeetbaar = {'J'}, PRODUCT_CODE={'1'}>}TRANSACT_DATE)) <=$(=max({$<VerbruikMeetbaar = {'J'}, PRODUCT_CODE={'1'}>}TRANSACT_DATE))"}> }VerbruikAantalLiter))

 

The formula above is working perfectly when I have selected only 1 vehicle,
but without a vehicle selection QS generates a sum with all the galons from a vehicle from the minimum date (correct) , without checking the maximum date. 

One vehicle selection:

TotalVehicle.JPG

No Vehicle selection :

TotalsVehicleGroup.JPGAnalyseVehicleGroup.JPG

 

Maybe you know how I can solve this? 

 

 

 

 

 

 

crusader_
Partner - Specialist
Partner - Specialist

How are you limiting your min/max dates?

Generally speaking expressions in set analysis inside $(= ... ) are working on a global set, which means this min/max numbers are calculated disregarding any dimensions in your chart, when you selecting 1 car  - basically there is no other possible values (except correct:) ), however when you don't have selections applied it will calculate max() on entire data set limited by other selections. 

So, taking your previous example with inline table, expression evaluates in below:

129340_1.PNG

which is correct on global data set

129340_2.PNG

as you see, vehicles are different, so further your MeasureAcceptable nor PRODUCT_CODE are not considered in calculations at all... then you can add this separately:

=Sum({
<TRANSACT_DATE={
">=$(=min({$<MeasureAcceptable = {'J'}, PRODUCT_CODE={'1'}>}TRANSACT_DATE))<=$(=max({$<MeasureAcceptable = {'J'}, PRODUCT_CODE={'1'}>}TRANSACT_DATE))"
}, MeasureAcceptable = {'J'}, PRODUCT_CODE={1}> }Gallons)

However, in this case it's not quite clear to me why do you need these dates in set analysis?

129340_3.PNG

Hope this helps.

UPD: Forgot attachment. (PFA)

//Andrei

aronvermeulen
Contributor III
Contributor III
Author

Thx for your reply!

The objective is relative simple, but the method to achieve it on the other hand... 😞
For each vehicle I want a dynamic calculation of the total gallons (period based on selection);

I want to calculate the sum of gallons within 2 reliable (Dimension: Measureacceptable) transactions/moments.
The gallons transactions within this min and max transaction should all be in calculation, disregarding the value of measureacceptable of this .

But important to keep in mind that the transaction moment is different for each vehicle.  
Thx to your reply i understand i cannot calculate it with this set analysis due to the expression.

Do you  know a other solution to do this?
Or is it not possible and should i calculate it within scripting?

Thx in advance!

 

 

 

crusader_
Partner - Specialist
Partner - Specialist

Hi,

It's pretty easy to do in script with flags, please find attached example.

Check comments in script carefully, in case of any questions feel free to ask.

Hope this helps.

//Andrei

aronvermeulen
Contributor III
Contributor III
Author

Wow, that was new for me..  Thx! 🙂
It costed me some time to understand, but I am sure these principles will be useful some time.
Unfortunately I doubt if I can use it for this purpose.

Just a example from the demo app. As I said I wanted to calculate the performance in different (dynamic) time selections.
I THINK that this method of flags and borders are to static for this.

See small example below. This first row has a '1' as flag, but for a right calculation the flag should be a 0. 


Example of wrong execution of flag and border.JPG