Skip to main content
Woohoo! Qlik Community has won “Best in Class Community” in the 2024 Khoros Kudos awards!
Announcements
Join us at Qlik Connect for 3 magical days of learning, networking,and inspiration! REGISTER TODAY and save!
cancel
Showing results for 
Search instead for 
Did you mean: 
microwin88x
Creator III
Creator III

Flag with 3 Consecutive Values = 0

Hello,

I have the following table:

IDPeriodValue
1201501300
1201502500
1201503800
12015040
12015050
12015060
2201301100
2201302100
2201303100
3201612100
32017010
32017020
32017030
42017010
42017020

Is there any way to add a column as expression with a flag having 3 consecutive Values = 0? Like this:

IDPeriodValueFilter
12015013000
12015025000
12015038000
120150401
120150501
120150601
22013011000
22013021000
22013031000
32016121000
320170101
320170201
320170301
420170100
420170200

Thank you!!!

1 Solution

Accepted Solutions
sunny_talwar

May be like this

Table:

LOAD ID,

  Date(Date#(Period, 'YYYYMM'), 'YYYYMM') as Period,

  Value;

LOAD * INLINE [

    ID, Period, Value

    1, 201501, 300

    1, 201502, 500

    1, 201503, 800

    1, 201504, 0

    1, 201505, 0

    1, 201506, 0

    2, 201301, 100

    2, 201302, 100

    2, 201303, 100

    3, 201612, 100

    3, 201701, 0

    3, 201702, 0

    3, 201703, 0

    4, 201701, 0

    4, 201702, 0

];

Left Join (Table)

LOAD ID,

  Date(AddMonths(Period, -1), 'YYYYMM') as Period,

  Value as Value2

Resident Table;

Left Join (Table)

LOAD ID,

  Date(AddMonths(Period, -2), 'YYYYMM') as Period,

  Value as Value3

Resident Table;

TempTable:

LOAD ID,

  Period,

  Value,

  If(ID = Previous(ID), If(Value = 0 and (Previous(Flag) = 1 or Previous(Previous(Flag)) = 1), 1, Flag), Flag) as Flag;

LOAD ID,

  Period,

  Value,

  If(Value + Value2 + Value3 = 0, 1, 0) as Flag

Resident Table

Order By ID, Period;

DROP Table Table;

View solution in original post

1 Reply
sunny_talwar

May be like this

Table:

LOAD ID,

  Date(Date#(Period, 'YYYYMM'), 'YYYYMM') as Period,

  Value;

LOAD * INLINE [

    ID, Period, Value

    1, 201501, 300

    1, 201502, 500

    1, 201503, 800

    1, 201504, 0

    1, 201505, 0

    1, 201506, 0

    2, 201301, 100

    2, 201302, 100

    2, 201303, 100

    3, 201612, 100

    3, 201701, 0

    3, 201702, 0

    3, 201703, 0

    4, 201701, 0

    4, 201702, 0

];

Left Join (Table)

LOAD ID,

  Date(AddMonths(Period, -1), 'YYYYMM') as Period,

  Value as Value2

Resident Table;

Left Join (Table)

LOAD ID,

  Date(AddMonths(Period, -2), 'YYYYMM') as Period,

  Value as Value3

Resident Table;

TempTable:

LOAD ID,

  Period,

  Value,

  If(ID = Previous(ID), If(Value = 0 and (Previous(Flag) = 1 or Previous(Previous(Flag)) = 1), 1, Flag), Flag) as Flag;

LOAD ID,

  Period,

  Value,

  If(Value + Value2 + Value3 = 0, 1, 0) as Flag

Resident Table

Order By ID, Period;

DROP Table Table;