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

How to use Complex dynamic computation of value based on selection in Qlik Sense?

Hello,

I have a table like this

Schermata 2022-01-13 alle 11.08.46.png

Now, consider that the user can select a period of time. I want to add a column where there must be the most recent available cost per product. So, for example, if the user select Feb21 and March21, the table should be like the following.

Schermata 2022-01-13 alle 11.12.11.png

I tried to compute lastCost using a set analysis where I tried to select the cost corresponding to max(time) per each product, but this is not ok because this automatically filter the table, leaving only the rows corresponding to max time, per product.

I need lastCost to build also some charts, using this cost to compute costs based on the most recent cost. I cannot do this in the etl because the most recent cost changes depending on the period selected by the user.

How could I accomplish such task?

Thanks

Labels (1)
2 Solutions

Accepted Solutions
Scotlyvictor
Partner - Contributor
Partner - Contributor

This would help you to achieve the desired result.

LOAD *, DATE#(Time,'DD-MM-YYYY') AS DATEFORMAT;
Load * inline [
Product ,Time , Cost
A,01-01-2021,1
A,01-02-2021,2
A,01-03-2021,4
B,01-01-2021,5
B,01-03-2021,3
C,01-03-2021,6
C,01-02-2021,7
C,01-01-2021,8

];

Expression:

Sum(Total <Product> {<DATEFORMAT={"$(=DATE(MAX(DATEFORMAT),'DD-MM-YYYY'))"}>}Cost)

View solution in original post

Scotlyvictor
Partner - Contributor
Partner - Contributor

Hi,

What is the formula used  in Cost ?

Can you please use sum(Cost) and check.

View solution in original post

8 Replies
rbartley
Specialist II
Specialist II

Hi,

My preference would be to add the "last cost flag" field in your load script and then just use that in the set analysis, e.g.

=Sum({<"last cost flag"={1}>}cost)
danette
Contributor III
Contributor III
Author

Hi,

last cost varies depending on the period that is selected by the user, so it cannot be pre-determined inside the load script. I don't need the absolute last cost, but the last cost based on the selection.

Scotlyvictor
Partner - Contributor
Partner - Contributor

This would help you to achieve the desired result.

LOAD *, DATE#(Time,'DD-MM-YYYY') AS DATEFORMAT;
Load * inline [
Product ,Time , Cost
A,01-01-2021,1
A,01-02-2021,2
A,01-03-2021,4
B,01-01-2021,5
B,01-03-2021,3
C,01-03-2021,6
C,01-02-2021,7
C,01-01-2021,8

];

Expression:

Sum(Total <Product> {<DATEFORMAT={"$(=DATE(MAX(DATEFORMAT),'DD-MM-YYYY'))"}>}Cost)
danette
Contributor III
Contributor III
Author

Hi,

this is what I already tried, but this automatically filter the rows, leaving only the rows corresponding to the max date. See following image.

Schermata 2022-01-14 alle 10.57.59.png

I want to keep also the others rows and in every row there should be the most recent costs (that are the ones selected in the set analysis above, but also the other rows must continue to appear). Like this:

Schermata 2022-01-14 alle 11.04.22.png

 

Scotlyvictor
Partner - Contributor
Partner - Contributor

Hi,

What is the formula used  in Cost ?

Can you please use sum(Cost) and check.

danette
Contributor III
Contributor III
Author

This way it works! Thanks

rbartley
Specialist II
Specialist II

It works if all products have the same last date, but not if they don't, e.g. if product A has a last date of 01-02-2021, but B and C have a last date of 01-03-2021, you don't get a latest value for B:

rbartley_0-1642175055054.png

 

danette
Contributor III
Contributor III
Author

Yes, I believe this is because in the set analysis the max date is 'general', I mean that the last date that is selected is the absolute max date, not by product.

To get the last date grouping by product I believe we should use AGGR. 

If you have any suggestions, that would be greatly appreciated.