Skip to main content
Woohoo! Qlik Community has won “Best in Class Community” in the 2024 Khoros Kudos awards!
Announcements
Nov. 20th, Qlik Insider - Lakehouses: Driving the Future of Data & AI - PICK A SESSION
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

Set Analysis with Max Date with Time-based Dimension

I'm trying to get the latest non null value of a fields in my QVW.

My expression is:   sum({$<[Month Value] = {"$(=vMaxUnitCostMonth)"}>} [Value])

where vMaxUnitCostMonth is: Max(if([Quantity] > 0 and [Value] > 0, [Month Value], Null()))


This works fine when the dimension is not time based.  When it's time based, it will give me the sum(Value) and seems to ignore the set analysis entirely.

When I modify the sum to: sum({$<[Month Value] = {"$(=num($(vMaxUnitCostMonth)))"}>} [Value])

it returns the correct value, but only for the max dimension. For example, if the dimension is Year, the only value that shows up is the year of the max([Month Value]). It's as though the variable is evaluating first, then checking each dimension for it instead of evaluating in relation to the dimension.

Is there a way to get the variable to evaluate in relation to the dimension? Given:

YearMonth ValuesMax([Month Value])
2008200808, 200809, 200810200810
2009200908, 200909, 200911200911
2010201007, 201008, 201009201009
2011201101, 201102, 201103201103
2012null-
2013201301, 201302, 201305, 201312201312

If my Dimension is Year, the only value that populates is 2013 as it's got the highest date of all of them, so I get the unit cost only for that year. When I put just the variable as an expression column, it gives the the correct max([Month Value]) in relation to the dimension. Why does it not do the same when it's inside set analysis?

I've also tried sum(aggr(if( [Month Value] = $(vMaxUnitCostMonth), sum(Value), 0), [Month Value], Year)), but it gives the sum(Value) of the entire year and not the latest value. When I make another chart with Dimensions of Year, [Month Value] and use the aggr(if( [Month Value] = $(vMaxUnitCostMonth), sum(Value), 0), [Month Value], Year), it gives appropriate data, but for each [Month Value] instead of only the max.

Thoughts?

1 Solution

Accepted Solutions
rubenmarin

Ok, seems it's working with:

Sum({<Date={$(=Chr(39) & Concat(Aggr(Date(max({<Value={"*"}, Quantity={"*"}>} Date)), Year), Chr(39) & ',' & Chr(39)) & Chr(39))}>} Value)

/ Sum({<Date={$(=Chr(39) & Concat(Aggr(Date(max({<Value={"*"}, Quantity={"*"}>} Date)), Year), Chr(39) & ',' & Chr(39)) & Chr(39))}>} Quantity)

View solution in original post

6 Replies
rubenmarin

Hi Alex, set analisys is calculated for the whole table, so it will not filter different values for each row, maybe you can try:

sum(if([Month Value] = Max([Month Value]), sum(Value), 0)

If that doesn't works a sample data will be nice for testing.

FYI: In a table, if you remove the colum title, you can see the expression calculated, with $-Values expanded. Check if they are what you are looking.

Not applicable
Author

Still no dice. Attaching a QVW.

Tried modifying it to take into account non-null values:  sum(if([Month Value] = Max(if([Inventory Quantity] > 0 and [Inventory Value] > 0, [Month Value])), sum([Inventory Value]), 0)) and that didn't work either.

rubenmarin

Ok, seems it's working with:

Sum({<Date={$(=Chr(39) & Concat(Aggr(Date(max({<Value={"*"}, Quantity={"*"}>} Date)), Year), Chr(39) & ',' & Chr(39)) & Chr(39))}>} Value)

/ Sum({<Date={$(=Chr(39) & Concat(Aggr(Date(max({<Value={"*"}, Quantity={"*"}>} Date)), Year), Chr(39) & ',' & Chr(39)) & Chr(39))}>} Quantity)

rubenmarin

I think I did it in the wrong table, for the table with year-month it should be:

Sum({<Date={$(=Chr(39) & Concat(Aggr(Date(max({<Value={"*"}, Quantity={"*"}>} Date)), Year, [Month Value]), Chr(39) & ',' & Chr(39)) & Chr(39))}>} Value)

/ Sum({<Date={$(=Chr(39) & Concat(Aggr(Date(max({<Value={"*"}, Quantity={"*"}>} Date)), Year, [Month Value]), Chr(39) & ',' & Chr(39)) & Chr(39))}>} Quantity)

Not applicable
Author

This only works when it's a 1:1 relationship between the dimension and the max(Date). For example, if you were to use a dimension such as product in which the max date for one product was 12-31-13 and the max date for another product was 3-31-11, it'd do the sum(<Date = {"12/31/2013","3/31/2011"} Value), which would not work in the instance that product one has a value for both 12-31-13 and 3-13-11.

For example, given:

ProductDateValue
Product 103-31-201139.8
Product 112-31-201340
Product 203-31-201112

the resulting table would be:

ProductValue
Product 179.8
Product 212

instead of

ProductValue
Product 140
Product 212

because the expression is: sum(<Date = {"12/31/2013","3/31/2011"} Value)

Thoughts?

rubenmarin

I don't have the data to debug, but for product it should be:

Sum({<Date={$(=Chr(39) & Concat(Aggr(Date(max({<Value={"*"}, Quantity={"*"}>} Date)), Product), Chr(39) & ',' & Chr(39)) & Chr(39))}>} Value)

/ Sum({<Date={$(=Chr(39) & Concat(Aggr(Date(max({<Value={"*"}, Quantity={"*"}>} Date)), Product), Chr(39) & ',' & Chr(39)) & Chr(39))}>} Quantity)

Set analisys is calculated for the entire table, not for row, so this expression sets the Date with all the dates needed for the table, dimension (Product in this case) will filter to the date needed for each row.