简体   繁体   中英

How to aggregate sum of past 2 values + current value + next 2 values with set analysis?

I want to calculate the aggregate sum of past 2 values + current value + next 2 values with set analysis . With or Without set analysis it works as expect . It works as expected with or without set analysis ( "{<Week=>}" ). But when zooming the values of the graph changes accordingly. So I used set analysis. Even though it's not working for my case.

But, when I zoom the chart we are not getting the actual result. Since I used set analysis.

I need the following. Consider this is my data,

|Week    | Value   |
|------------------|
|01/2011 |  256    |
|02/2011 |  2056   |
|03/2011 |  112    |
|04/2011 |  95     |
|05/2011 |  1069   |
|07/2011 |  125    |
|08/2011 |  73     |
--------------------

I need to plot data for 04/2011 would be (2056+112+95+1069+125) in the same we need to calculate for each date

I am trying with the following expression:

It works fine : RangeSum(Below(Sum(Value),1,6)) + RangeSum(Above(Sum(Value),1,6)) + Sum(Value)

For zooming I used set analysis as follows,

RangeSum(Below(Sum({<Date=>}Value),1,6)) + RangeSum(Above(Sum({<Date=>}Value),1,6)) + Sum({<Date=>}Value)

This is not working as expected.

This should work as you want:

Backend Code:

Inline:
LOAD * INLINE [
    WeekKey,Value
    01/2011,256    
    02/2011,2056   
    03/2011,112    
    04/2011,95     
    05/2011,1069   
    07/2011,125    
    08/2011,73 
];

dateTemp:
LOAD
WeekKey,
Right(WeekKey,4)&Left(WeekKey,2) as SortOrder,
Left(WeekKey,2) as Week,
Right(WeekKey,4) as Year
Resident Inline;

LOAD
RowNo() as Row,
*
Resident dateTemp
Order By SortOrder ASC;
DROP Table dateTemp;

Your expression:

Sum({<Week=,WeekKey=,Row = {'>=$(=max(Row)-2)<=$(=max(Row)+2) '}>} Value)

I did the following assumptions to the open questions in the comments:

  1. The sum for a certain range should not be changed when zooming in.
  2. The preceding value of 07/2011 is 0 (as the value of 06/2011)

So the calculation has to be done on all values depending on the weeks. May be that can be done with set analysis, but I guess these comparisons and lookups will be difficult if not impossible.

I would prefer a solution with pre-calculating the values in the load script:

BaseData:
Load 
  oWeek,
  value,
  left(oWeek,2) as nWeek,
  right(oWeek,4) as nYear,
  MakeWeekDate(right(oWeek,4), left(oWeek,2)) as weekstart
inline
[
  oWeek, value
  01/2011, 256
  02/2011, 2056
  03/2011, 112
  04/2011, 95
  05/2011, 1069
  07/2011, 125
  08/2011, 73
];

First calculate the weekstarts of all reqired weeks:

TEMP_1:
Load 
  oWeek, value, nWeek, nYear, weekstart,
  WeekStart(weekstart, -1) as weekstart_p1,
  WeekStart(weekstart, -2) as weekstart_p2,
  WeekStart(weekstart, 1) as weekstart_n1,
  WeekStart(weekstart, 2) as weekstart_n2
resident BaseData;
drop Table BaseData;

Lookup the values for the weeks:

TEMP_2:
Load 
  oWeek, value, nWeek, nYear, weekstart,
  weekstart_p1, weekstart_p2, weekstart_n1, weekstart_n2,
  lookup('value', 'weekstart', weekstart_p1, 'TEMP_1') as value_p1,
  lookup('value', 'weekstart', weekstart_p2, 'TEMP_1') as value_p2,
  lookup('value', 'weekstart', weekstart_n1, 'TEMP_1') as value_n1,
  lookup('value', 'weekstart', weekstart_n2, 'TEMP_1') as value_n2
resident TEMP_1;
drop Table TEMP_1;

The fields value_p1 ... may contain 'null' so they must be convertet into 0.

TEMP_3:
Load 
  oWeek, value, nWeek, nYear, weekstart,
  weekstart_p1, weekstart_p2, weekstart_n1, weekstart_n2,
  value_p1, value_p2, value_n1, value_n2,
  if (isnull(value_p2), 0, value_p2) as val_p2,
  if (isnull(value_p1), 0, value_p1) as val_p1,
  if (isnull(value_n1), 0, value_n1) as val_n1,
  if (isnull(value_n2), 0, value_n2) as val_n2
resident TEMP_2;
drop Table TEMP_2;

The fields val_p1 now contains valid numeric values.

Data:
Load 
  oWeek, value, nWeek, nYear, weekstart,
  weekstart_p1, weekstart_p2, weekstart_n1, weekstart_n2,
  val_p1, val_p2, val_n1, val_n2,
  val_p2 + val_p1 + value + val_n1 + val_n2 as mySum 
resident TEMP_3;
drop Table TEMP_3;

In my dashboard I have one tablebox:

具有预先计算值的tablebox

And a diagram:

图表mySum over oWeek

Hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM