13 Replies Latest reply: Sep 25, 2017 12:49 AM by Apoorva Dhulehole

# Firstsortedvalue function with sum in QlikView

Hello Everyone,

I am using firstsortedvalue function to pick up last value in date field for a particular month. But I would like to combine this with sum function. What I want is, in the attached example, when more than two plants are selected, my graph should display value which is the sum of last value of date field of that particular month combining both plants (sum). How can I achieve this?

Any suggestion?

Thank you.

• ###### Re: Firstsortedvalue function with sum in QlikView

Hi, you can try with:

Sum(Aggr(FirstSortedValue(DISTINCT{<[ACT vs AOP]={'>0'}, Date={'<=\$(=Today())'}>}[ACT vs AOP],-Date), [\$(=GetCurrentField(@Time))], Plant))

• ###### Re: Firstsortedvalue function with sum in QlikView

Just suggest to add the set analysis from the inner aggregation to the outer one

Sum({<Date={'<=\$(=Today())'}>} Aggr(FirstSortedValue(DISTINCT{<[ACT vs AOP]={'>0'}, Date={'<=\$(=Today())'}>}[ACT vs AOP],-Date), [\$(=GetCurrentField(@Time))], Plant))

• ###### Re: Firstsortedvalue function with sum in QlikView

I didn't checked but, shouldn't be all set analysis?

Sum({<[ACT vs AOP]={'>0'}, Date={'<=\$(=Today())'}>} Aggr(FirstSortedValue(DISTINCT{<[ACT vs AOP]={'>0'}, Date={'<=\$(=Today())'}>}[ACT vs AOP],-Date), [\$(=GetCurrentField(@Time))], Plant))

• ###### Re: Firstsortedvalue function with sum in QlikView

Ya that's true

• ###### Re: Firstsortedvalue function with sum in QlikView

Whose answer should I mark as right one now?

• ###### Re: Firstsortedvalue function with sum in QlikView

It's working! Thank you very much Ruben

• ###### Re: Firstsortedvalue function with sum in QlikView

Quick question on this again. How will I get this to work in an "if" "else" statement written below?

if(GetFieldSelections(WeekShort)>=month(Date#(WeekShort,'MMM')) or  Month(Date#(POPER_NEW, 'MMM'))>=month(today())

,FirstSortedValue(DISTINCT {<KPI={"Inventory_New"}>}[Risk/ Challenge - Bank Builds],-Date)

,if(GetFieldSelections(WeekShort)<month(Date#(WeekShort,'MMM')) or Month(Date#(POPER_NEW, 'MMM'))<month(today())

,FirstSortedValue(DISTINCT {<KPI={"Inventory_New"}>}[BB ACT monthly LC]-[BB FC monthly LC],-Date)))

Thank you.

• ###### Re: Firstsortedvalue function with sum in QlikView

Hi Apoorva, I'm not sure if taking the aggr outside the expression can work, and what dimensions should be used as parameters in that case. Can you upload a sample to test?

Btw, I don't think "GetFieldSelections(WeekShort)>=month(Date#(WeekShort,'MMM'))" is working, what has to do this filter?

• ###### Re: Firstsortedvalue function with sum in QlikView

Hi Ruben,

WeekShort field has dates as its values. What that condition does is, when a value selected in WeekShort is greater than or equal to current month then do so and so...Complete expression is available in the attached sample.

Thank you.

• ###### Re: Firstsortedvalue function with sum in QlikView

Hi, this chart needs to select a week, and each week has one date, can this much simpler expression work?:

If(Month(Date#(POPER_NEW, 'MMM'))>=month(today())

,Sum({<KPI={"Inventory_New"}>}[w/o BB])

,Sum({<KPI={"Inventory_New"}>}[ACT Inventory weekly LC])-Sum({<KPI={"Inventory_New"}>}[AOP Inventory monthly LC])

)

• ###### Re: Firstsortedvalue function with sum in QlikView

Thanks for the suggestion.

But this expression should also work when month is selected. When month is selected, data of last week should be shown in the graph. That's the reason Firstsortedvalue function was used. And when more than two plants are selected, sum of all values of last dates of a month of those plants has to be displayes.

How to achieve this? Any idea?

Thank you.

• ###### Re: Firstsortedvalue function with sum in QlikView

Another approach to check first date of the month:

If(Month(Date#(POPER_NEW, 'MMM'))>=month(today()) or WeekShort>=Today()

,Sum(Aggr(If(WeekShort=Min(TOTAL <Plant> WeekShort), Sum({<KPI={"Inventory_New"}>}[w/o BB]))

, Plant, WeekShort))

,Sum(Aggr(

If(WeekShort=Min(TOTAL <Plant> WeekShort),

Sum({<KPI={"Inventory_New"}>}[ACT Inventory weekly LC])-Sum({<KPI={"Inventory_New"}>}[AOP Inventory monthly LC]))

, Plant, WeekShort))

)

• ###### Re: Firstsortedvalue function with sum in QlikView

Hi Ruben,

This expression is working

I have changed "Min" to "Max" as I wanted last date of the month and the expression is working as expected.

Thank you very much!