Skip to main content
Announcements
See what Drew Clarke has to say about the Qlik Talend Cloud launch! READ THE BLOG
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

Sum Values in a month based on the last data of each element of the minimum granularity

Hello.

This is a hard question, or at least far from a basic one.

Requirement: Time Travel. "To position myself in any point of time and look at the data that what up to date at that time"

Extra info: For the non historic table we already have the important info: Booked nights (when where they reserved, which nights in calendar are they in), Amount paid and more).

We have modificationdate of course. I can pull out that data with set analysis... BUT I'm still not able to pick the "newest" record at that time for each reservation.

Either:

a) I end up summing all records and data multiplies, or

b) I try to get the latest date but I get only the max date for all of them.

Example of data:

idreservationSist.Fecha (modified.date)Sum(Nights)
10May 55
10May 296
10May 317
20May 614
20May 87
30May 102
40May 312

What I'm using now:

 

sum({1}Aggr(
sum( {<
 
IDStatusReservación={'1','2','3'},
 
Reservaciones.EsActual={0} , //Historical Data
 
Sist.MesID={$(=vMaxReservMesID-12)},Reservación.MesID={$(=vMaxReservMesID-12)},
 
Reservación.Semana=,Reservación.Mes=,Reservación.Año=  
  ,
Sist.Fecha={$(=Max(    {1< Sist.MesID={$(=vMaxReservMesID-12)} >}   Sist.Fecha))}     //It's giving me may 31 for all, even it's aggr by each idreservation         


>}
Nights)
,
idreservacion))

 

What I'm getting:

For id 10 on may 31: 7

For id 10 on may 31: 0

For id 10 on may 31: 0

For id 10 on may 31: 2

TOTAL 9

What I should Get:

Most recent for id 10: 7

Most recent for id 20: 7 (which is the value on the most recent date for that id reservation, in may 8th)

Most recent for id 30: 2 (same as above)

Most recent for id 40: 2

Total 18

Note:

I could Aggr in script with Last Value, in a loop for each month... but they want to place themselves at any date of time. It may be May 7th, or the 8th.

1 Solution

Accepted Solutions
sunny_talwar

Try this, it seems to work:

sum({1<IDStatusReservación*={'1','2','3'},      //Sum of sums
                      Reservaciones.EsActual={0} ,
Sist.MesID={'38'},Reservación.MesID={'38'},
Reservación.Semana=,Reservación.Mes=,Reservación.Año={2014},Sist.Año={2014}>}aggr(
if(Only({1<IDStatusReservación*={'1','2','3'},      //Sum of sums
                      Reservaciones.EsActual={0} ,
Sist.MesID={'38'},Reservación.MesID={'38'},
Reservación.Semana=,Reservación.Mes=,Reservación.Año={2014},Sist.Año={2014}>}Sist.Fecha) =

max({1< IDStatusReservación*={'1','2','3'},    //Max Sist.Fecha for each
                      Reservaciones.EsActual={0} ,
Sist.MesID={'38'},Reservación.MesID={'38'},
Reservación.Semana=,Reservación.Mes=,Reservación.Año={2014},Sist.Año={2014}

>} total <idreservacion, Noches.Año,Noches.Dia > Sist.Fecha),
sum({1< IDStatusReservación*={'1','2','3'},          //sum of 1's
                      Reservaciones.EsActual={0} ,
Sist.MesID={'38'},Reservación.MesID={'38'},
Reservación.Semana=,Reservación.Mes=,Reservación.Año={2014},Sist.Año={2014}

>}  Nights)
)
,idreservacion, Noches.Año,Noches.Dia,Sist.Fecha))

//


HTH


Best,

Sunny

View solution in original post

19 Replies
sunny_talwar

Would you be able to share a sample application where you are trying to do this? Without data, it would be difficult to know what will work and what wont.

Best,

Sunny

sunny_talwar

Not sure if this will work for you or not, but can you try aggregate with NODISTINCT:

sum({1}Aggr(NODISTINCT
sum( {<
 
IDStatusReservación={'1','2','3'},
 
Reservaciones.EsActual={0} , //Historical Data
 
Sist.MesID={$(=vMaxReservMesID-12)},Reservación.MesID={$(=vMaxReservMesID-12)},
 
Reservación.Semana=,Reservación.Mes=,Reservación.Año
  ,
Sist.Fecha={$(=Max(    {1< Sist.MesID={$(=vMaxReservMesID-12)} >}  Sist.Fecha))}    //It's giving me may 31 for all, even it's aggr by each idreservation         


>}
Nights)
,
idreservacion))

swuehl
MVP
MVP

Your set expression will only be evaluated once per chart, not per dimension value, hence you will get the same date for all idreservations.

I think you can use advanced aggregation / aggr() function in combination with FirstSortedvalue() and a set expression that limits to a date range, not a single date:

=Sum({<[Sist.Fecha (modified.date)]={"<=$(=max([Sist.Fecha (modified.date)]))"} >}

     Aggr(

          FirstSortedValue(

               {<[Sist.Fecha (modified.date)]={"<=$(=max([Sist.Fecha (modified.date)]))"} >}

               [Sum(Nights)] ,-[Sist.Fecha (modified.date)])

          ,idreservation)

     )

See also attached sample file.

sunny_talwar

or may this is what you want?

Capture.PNG

If yes, I am attaching the qvw for reference.

Best,

Sunny

Anonymous
Not applicable
Author

Thanks @sunindia and @swuehl, but I'm afraid I did not explain correctly. I don't have one aggregated valur. I need to sum values. In the table in my Opening Post i entered Sum(Nights). And that's a sum of N "1", one for each Night.

The granurality of the table is by date, for example reservation 10:

   

idreservModifDate(Sist.Fecha)NightsDate of StayNightofStay
1005/may/2014127/feb/20151
1005/may/2014128/feb/20152
1005/may/2014101/mar/20153
1005/may/2014102/mar/20154
1005/may/2014103/mar/20155
1029/may/2014127/feb/20151
1029/may/2014128/feb/20152
1029/may/2014101/mar/20153
1029/may/2014102/mar/20154
1029/may/2014103/mar/20155
1029/may/2014104/mar/20156
1031/may/2014127/feb/20151
1031/may/2014128/feb/20152
1031/may/2014101/mar/20153
1031/may/2014102/mar/20154
1031/may/2014103/mar/20155
1031/may/2014104/mar/20156
1031/may/2014105/mar/20157

So, you see, there's N firstsorted values for Nights, so that's why I want to sum nights  for the first sorted value of Modified Date.

I need to have it like this because I want to be able to calculate occupancy for each day or month, in this example, they have nights in February, and nights in march. If I aggregate in the script it would be hard to calculate which dates belong to what month.

Note:

I don't have the field "NighOfStay" I entered it to better explain the table. In may 5th, they made the reservation. In may29th they called asking for one more night, and on may 31st, a seventh night.

Let me create a qvw. But I'm afraid I'll take a while.

sunny_talwar

May be this in that case:

=FirstSortedValue(Aggr(Max(NightofStay), idreserv, ModifDate(Sist.Fecha)), -ModifDate(Sist.Fecha)) or

=FirstSortedValue(Aggr(Sum(Nights), idreserv, ModifDate(Sist.Fecha)), -ModifDate(Sist.Fecha))

Anonymous
Not applicable
Author

sunindia‌,

swuehl

Hi. Here's the qvw

Thanks very much. I appreciate the time you are using.

swuehl
MVP
MVP

There's section access applied to the QVW, so I can't open it. Please update the QVW with section access removed.

If possible, try reducing the size, too (e.g. by reducing the data after selecting a subset).

Anonymous
Not applicable
Author

There we go.

Here's a new one, also with Section Access but here's the logons:

USER, sunindia
USER, swuehl

It was already reduced but I'm sending it a bit more reduced now.