Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 
momofiore
Creator
Creator

how to calculate a value using dates filtred by a calendar.

Hey gays:

i had a table with 3 field, car id , DATE and mileage and i would like to calculate the substruction of mileage between two dates selected by a calendar. or the nearest dates to the selected one.

  car id               DATE                    mileage

CQ863NW02/07/201868963
CQ863NW26/07/201870085
CQ863NW02/08/201870356
CQ863NW07/08/201870569
CQ863NW27/08/201871389
CQ863NW04/09/201871842
CQ863NW05/09/201871902
CQ863NW10/09/201871947
CQ863NW13/09/201872315

for example:

if i select by a calendar: 13/09/2018  and 10/09/2018  i will get 72315-71947 = 368.

i use two variables to store the date selected by 2 calendars.

sc.PNG

or the nearest date selected:

for example:

if i select by a calendar: 14/09/2018  and 9/09/2018  i will get 72315-71947 = 368. becaus the r the nearest.

i attached a excel file.


1 Solution

Accepted Solutions
Vegar
MVP
MVP

Another solution where you don't need to use the variables is to let the calculation be based on your your highest and lowest MILAGE per car in your selection. I assume that the MILEAGE always increases and never decreases per car.

MAX(MILEAGE) will always represent the latest datapoint per car

MIN(MILEAGE) will represent the earliest datapoint per car.

sum(aggr(MAX(MILEAGE)-Min(MILEAGE),[CAR ID]))

View solution in original post

8 Replies
momofiore
Creator
Creator
Author

Vegar
MVP
MVP

It could look something like this:

Sum( {<Date = {"$(=max({<Date={"<=$(varMax)”} >} Date)"}>} Milage)

-

Sum( {<Date = {"$(=min({<Date={"<=$(varMin)”} >} Date)"}>} Milage)

I personally depreciate using variables for date picking in my apps hence I loose important Qlik association (Green Grey White) by not using selections in the date fields.

momofiore
Creator
Creator
Author

@Vegar Lie Arntsen

thank Vegar. i tried your answer but i still get errors regarding the expression.

here is my file. i created 2 variables varMin and varMax toget the dates from the calendar. !

Vegar
MVP
MVP

I'm a bit busy today, but I look at it a bit later. Maybe someone else in the community are able to help you adjust the SET in the meanwhile.

momofiore
Creator
Creator
Author

i hope  that someone helps vegar.lie.arntsen‌ . thabk u all

Vegar
MVP
MVP

This expression will do the trick.

=Sum( {<DATE = {"$(=max( {<DATE = {"<$(=date($(varMax)))"}>} DATE) )"}>}MILEAGE)

-Sum( {< DATE = {"$(=min( {<DATE = {">$(=date($(varMin)))"}>} DATE) )"} >}MILEAGE)

Vegar
MVP
MVP

Another solution where you don't need to use the variables is to let the calculation be based on your your highest and lowest MILAGE per car in your selection. I assume that the MILEAGE always increases and never decreases per car.

MAX(MILEAGE) will always represent the latest datapoint per car

MIN(MILEAGE) will represent the earliest datapoint per car.

sum(aggr(MAX(MILEAGE)-Min(MILEAGE),[CAR ID]))

momofiore
Creator
Creator
Author

thanks vegar !!