Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Qlik Open Lakehouse is Now Generally Available! Discover the key highlights and partner resources here.
cancel
Showing results for 
Search instead for 
Did you mean: 
spividori
Specialist
Specialist

Calc Last and First data

Hi.

I have the following table and I need to calculate the last "Peso" - the first "Peso" of each IdAnimal in a KPI. This has to be dynamic. In the first example I have selected the years 2021 and 2022 and in the second example I only have the year 2021 selected.

Captura.PNGCaptura2.PNG

Regards.

Labels (1)
1 Solution

Accepted Solutions
Digvijay_Singh

Try changing this property to sum - 

Digvijay_Singh_0-1662558732498.png

OR expression to as below - 

sum(Aggr(FirstSortedValue(Peso,-FechaEvPS)-FirstSortedValue(Peso,FechaEvPS),IdAnimal))

View solution in original post

5 Replies
sidhiq91
Specialist II
Specialist II

@spividori  Please look at the Script used in the back end. Have a look at it and let me know if this something that you are looking for:

NoConcatenate
Temp:
Load IdAnimal,
Date(Date#(Date,'DD/MM/YYYY'),'MM/DD/YYYY') as Date,
Peso
Inline [
IdAnimal, Date, Peso
30695,13/05/2021,500
30695,19/08/2021,259
30695,22/09/2021,370
30695,03/12/2021,346
30695,02/08/2022,414
30772,21/01/2021,314
30772,15/04/2021,403
30772,29/06/2021,436
30772,19/08/2021,432
30772,15/10/2021,465
30772,25/11/2021,469
];

NoConcatenate
Temp1:
Load IdAnimal,

FirstSortedValue(Peso,Date) as FirstValue,
FirstSortedValue(Peso,-Date) as lastValue

Resident Temp
group by IdAnimal;

NoConcatenate
Temp2:
Load IdAnimal,
FirstValue,
lastValue,
lastValue-FirstValue as Difference

Resident Temp1;

Drop table Temp,Temp1;

Exit Script;

sidhiq91_0-1662516932109.png

 

Digvijay_Singh

May be in the front-end like this - 

Digvijay_Singh_0-1662517163192.png

 

spividori
Specialist
Specialist
Author

Thanks for answering.
The total by IdAnimal is correct but I need the general total to be the sum of the result of each IdAnimal. in the example of the image it should be: 69, not 100.

Captura.PNG

Digvijay_Singh

Try changing this property to sum - 

Digvijay_Singh_0-1662558732498.png

OR expression to as below - 

sum(Aggr(FirstSortedValue(Peso,-FechaEvPS)-FirstSortedValue(Peso,FechaEvPS),IdAnimal))

spividori
Specialist
Specialist
Author

Hi.

Yes. It's what I needed. Thank you very much!.

Regards.