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

Calculate Against First Value?

Hiya Guys,

I have a need to be able to calculate the "Price Index" of a Product (which is how much it was sold for over several months - compared to it's starting price in the first month selected).

Therefore, if the dates selected were Jan-Apr 2013 (Year_Month >= 201301 and Year_Month <= 201304), I want to be able to compare the selling price of Feb, Mar, Apr, against what the Product was sold for in Jan.

The formula I am currently using is:

=Avg({$<Year_Month={201301}, Product={Product1}>} Price)

However, this doesn't work as it only gives the Average price for the first selected month, eg:

Year_MonthProductPriceFirst Month
  Price
201301Product118,456.7818456.78
201302Product117,198.50-
201303Product118,528.82-
201304Product117,623.13-

(in the above example I was expecting to see the "Jan Price" in each row - therefore enabling me to compare that against the "current month price").

This sounds like a job for Set Analysis - but I can't seem to get it to work.

Does anyone have any ideas?

Cheers,

Steve.

1 Solution

Accepted Solutions
Not applicable
Author

Thanks Nicole and Paul!

You both pointed me in the right direction to find a solution, although unfortunately I was unable to get either of your suggestions to work.

Nicole: My Set Expression is actually a lot more complex than the example - and, unfortunately, I was unable to get the "FirstSortedValue" function to even return a result!  More likely my fault than yours, and thank you for the .qvd example!

Paul:  Unfortunately by using "TOTAL" it seemed to return the Average of EVERYTHING, not just the first selected month (ie: (18,456.78 + 17,198.50 + 18,528.82 + 17,623.13) / 4 = 17,951.81 from the example, above).

The expression which I have now come-up with is using the "Aggr" function:

=Aggr(Avg({$<Year_Month={201301}, Product={Product1}>} TOTAL Price, Product, Year_Month)

This gives me the desired result:

Year_MonthProductPriceFirst Month
  Price
201301Product118,456.7818456.78
201302Product117,198.5018456.78
201303Product118,528.8218456.78
201304Product117,623.1318456.78

Many thanks to both of you for pointing me in the right direction!

Cheers,

Steve.

View solution in original post

4 Replies
Nicole-Smith

How about:

FirstSortedValue(total <Product> Price, Year_Month)

Example file also attached.

Not applicable
Author

Try this:

=Avg({$<Year_Month={201301}, Product={Product1}>} TOTAL Price)

Not applicable
Author

Thanks Nicole and Paul!

You both pointed me in the right direction to find a solution, although unfortunately I was unable to get either of your suggestions to work.

Nicole: My Set Expression is actually a lot more complex than the example - and, unfortunately, I was unable to get the "FirstSortedValue" function to even return a result!  More likely my fault than yours, and thank you for the .qvd example!

Paul:  Unfortunately by using "TOTAL" it seemed to return the Average of EVERYTHING, not just the first selected month (ie: (18,456.78 + 17,198.50 + 18,528.82 + 17,623.13) / 4 = 17,951.81 from the example, above).

The expression which I have now come-up with is using the "Aggr" function:

=Aggr(Avg({$<Year_Month={201301}, Product={Product1}>} TOTAL Price, Product, Year_Month)

This gives me the desired result:

Year_MonthProductPriceFirst Month
  Price
201301Product118,456.7818456.78
201302Product117,198.5018456.78
201303Product118,528.8218456.78
201304Product117,623.1318456.78

Many thanks to both of you for pointing me in the right direction!

Cheers,

Steve.

Not applicable
Author

Awesome! Great to hear.

In addition, if you wanted to make the above expression more dynamic, you should be able to do something like:

Aggr(Avg({$<Year_Month={"'=min(Year_Month)"},.....))

This will always select either the min (or max) month in the data or the earliest month in the range selected. It might help you as you continue to load more data.

Thanks for posting your final solution!