Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Qlik Open Lakehouse is Now Generally Available! Discover the key highlights and partner resources here.
cancel
Showing results for 
Search instead for 
Did you mean: 
jagannalla
Partner - Specialist III
Partner - Specialist III

Successive count for each month

Hi,

I've data like this

MonthYrColorAccountId
Apr 2015Red1
Apr 2015Red2
Apr 2015Green3
Apr 2015Green

4

Apr 2015Yellow5
May 2015Red6
May 2015Yellow7
May 2015Green8
Jun 2015Yellow

9

Jun 2015Green10
Jun 2015Green11

Now I need o/p:

Apr-15Red3
Green2
Yellow1
15-MayRed2
Green1
Yellow1
Jun-15Red0
Green2
Yellow1

- The count of Red is based on start month to end month. from Apr to jun we had 3 reds, from May to Jun we had 2 and in jun we don't have any red's

- I want to use only 2 dimension with 1 exp (Dim are MonthYr,Color and exp as count(AccountId)

- Every thing needs to done in script level only.

1 Solution

Accepted Solutions
MayilVahanan

Hi Jagan,

Try like this

ColorTemp:

LOAD *, MonthYr&Color as ColorKey INLINE [

    MonthYr, Color, AccountId

    Apr 2015, Red, 1

    Apr 2015, Red, 2

    Apr 2015, Green, 3

    Apr 2015, Green, 4

    Apr 2015, Yellow, 5

    May 2015, Red, 6

    May 2015, Yellow, 7

    May 2015, Green, 8

    Jun 2015, Yellow, 9

    Jun 2015, Green, 10

    Jun 2015, Green, 11

];

//Cross Join

ColorTemp2:

LOAD Distinct MonthYr Resident ColorTemp;

Join LOAD Distinct Color Resident ColorTemp;

//Concatenate the missed color to corresponding month

Concatenate (ColorTemp)

LOAD MonthYr,Color Resident ColorTemp2 Where not Exists(ColorKey, MonthYr&Color) ;

DROP Table ColorTemp2;

DROP Field ColorKey;

//Date format

Color:

LOAD *, Month(Date) as Month, Month(Date)*1 as MonthNum;

LOAD *, Date(Date#(MonthYr, 'MMM YYYY')) as Date Resident ColorTemp;

DROP Table ColorTemp;

//Find the max month

MaxMonthTable:

LOAD Max(Month) as MaxMonth Resident Color;

Let vMaxMonth = Peek('MaxMonth', 0, 'MaxMonthTable');

DROP Table MaxMonthTable;

//Join the month from small to large

Join(Color)

load Distinct

Month as Month_AsOf,

MonthNum + IterNo()-1 as Month

Resident Color

while MonthNum+IterNo()-1 <= $(vMaxMonth);

//only for Red

FinalTable:

LOAD MonthYr, Color, AccountId, Month_AsOf as Month Resident Color Where Color = 'Red';

LOAD Distinct MonthYr, Color, AccountId, Month Resident Color Where Color <> 'Red';

DROP Table Color;

EXIT Script;

Then use Month, Color as Dimension. =Count(AccountId) as Measure.

Hope it helps you.

Thanks & Regards, Mayil Vahanan R
Please close the thread by marking correct answer & give likes if you like the post.

View solution in original post

5 Replies
Not applicable

Hi Jagan,

I think the easiest way to do this is to setup one count for each color and the group the table load on Year-Month.

Good luck!

jagannalla
Partner - Specialist III
Partner - Specialist III
Author

Could you give me one example script

MayilVahanan

Hi Jagan,

Try like this

ColorTemp:

LOAD *, MonthYr&Color as ColorKey INLINE [

    MonthYr, Color, AccountId

    Apr 2015, Red, 1

    Apr 2015, Red, 2

    Apr 2015, Green, 3

    Apr 2015, Green, 4

    Apr 2015, Yellow, 5

    May 2015, Red, 6

    May 2015, Yellow, 7

    May 2015, Green, 8

    Jun 2015, Yellow, 9

    Jun 2015, Green, 10

    Jun 2015, Green, 11

];

//Cross Join

ColorTemp2:

LOAD Distinct MonthYr Resident ColorTemp;

Join LOAD Distinct Color Resident ColorTemp;

//Concatenate the missed color to corresponding month

Concatenate (ColorTemp)

LOAD MonthYr,Color Resident ColorTemp2 Where not Exists(ColorKey, MonthYr&Color) ;

DROP Table ColorTemp2;

DROP Field ColorKey;

//Date format

Color:

LOAD *, Month(Date) as Month, Month(Date)*1 as MonthNum;

LOAD *, Date(Date#(MonthYr, 'MMM YYYY')) as Date Resident ColorTemp;

DROP Table ColorTemp;

//Find the max month

MaxMonthTable:

LOAD Max(Month) as MaxMonth Resident Color;

Let vMaxMonth = Peek('MaxMonth', 0, 'MaxMonthTable');

DROP Table MaxMonthTable;

//Join the month from small to large

Join(Color)

load Distinct

Month as Month_AsOf,

MonthNum + IterNo()-1 as Month

Resident Color

while MonthNum+IterNo()-1 <= $(vMaxMonth);

//only for Red

FinalTable:

LOAD MonthYr, Color, AccountId, Month_AsOf as Month Resident Color Where Color = 'Red';

LOAD Distinct MonthYr, Color, AccountId, Month Resident Color Where Color <> 'Red';

DROP Table Color;

EXIT Script;

Then use Month, Color as Dimension. =Count(AccountId) as Measure.

Hope it helps you.

Thanks & Regards, Mayil Vahanan R
Please close the thread by marking correct answer & give likes if you like the post.
Not applicable

Hi,

Check the attached file

T1:

LOAD * INLINE [

    MonthYr,Color

    Apr 2015, Red

  Apr 2015, Red

  Apr 2015, Green

  Apr 2015, Green

  Apr 2015, Yellow

  Jun 2015, Yellow

  Jun 2015, Green

  Jun 2015, Green

  May 2015, Red

  May 2015, Yellow

  May 2015, Green

   

];

T2:

LOAD

  MonthYr,

  count(if(Color = 'Red',1)) as Red,

  count(if(Color = 'Yellow',1)) as Yellow,

  count(if(Color = 'Green',1)) as Green

Resident T1

Group By MonthYr;

Kushal_Chawda

Please see the attached.

There only one red from May 2015 to Jun 2015 instead of two as you have mentioned.