Skip to main content
Announcements
Qlik Acquires Upsolver to Advance Iceberg Solutions: READ MORE
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

Help with looking up values in the same table

Hi All!

I have a question concerning the lookup() function or any other function that can assist me in attaining the following.

In one concatenated table is a column with cummulative values. I want to add a column to my table stating the non-cummulative values.

To establish this the value in month X from the origninal column should be deducted by the value of month X-1

Therefore, first I already established an extra column calculating month -1.  e.g. This has been performed. See example below

Month   |   Value   |    Month-1  

02               100          01                        

03               200          02

04               300          03                             

Now I want to add a column looking up the value for month-1 in the same table, but how can I establish that? Lookup(), ApplyMap()? See example below

Month   |   Value   |    Month-1    |   ValueMonth-1

02               100          01                         50

03               200          02                        100

04               300          03                         200

Thanks in advance!

1 Solution

Accepted Solutions
Not applicable
Author

Here it goes

TmpFact:

LOAD Month,

     Value,

     (Month-1) as PreMonth

From (Excel file / QVD);

MapFact:

Mapping LOAD

     Month as ID,

     Value

Resident TmpFact;

Fact:

LOAD Month,

     Value,

     PreMonth,

     ApplyMap('MapFact',PreMonth,0) as PreValue

Resident TmpFact;

Drop Table TmpFact;

View solution in original post

2 Replies
Not applicable
Author

Here it goes

TmpFact:

LOAD Month,

     Value,

     (Month-1) as PreMonth

From (Excel file / QVD);

MapFact:

Mapping LOAD

     Month as ID,

     Value

Resident TmpFact;

Fact:

LOAD Month,

     Value,

     PreMonth,

     ApplyMap('MapFact',PreMonth,0) as PreValue

Resident TmpFact;

Drop Table TmpFact;

Peter_Cammaert
Partner - Champion III
Partner - Champion III

Actually, if you have Cumulative values, you should have actual values somewhere too?

In your case, forget about Month-1 and use Previous() function.

For example:

Facts:  // Only cumulative value
LOAD * INLINE [
Month, CumulValue, PrevMonth
02, 100, 01
03, 200, 02
04, 300, 03
05, 350, 04
06, 425, 05
07, 525, 06
08, 600, 07
09, 625, 08
10, 725, 09
11, 800, 10
12, 900, 11
]
;

QUALIFY *;

NewFacts: 
// With absolute value
NOCONCATENATE
LOAD *,
   
CumulValue - if(IsNull(Previous(CumulValue)),0, Previous(CumulValue)) as Value  // Account for Missing preceding record
RESIDENT Facts;

UNQUALIFY *;

Best,

Peter