Skip to main content
Announcements
July 15, NEW Customer Portal: Initial launch will improve how you submit Support Cases. IMPORTANT DETAILS
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

Comparing Current Month and Previous Month Sales

Hi,

I wanted to compare the total sales amount of the current month to the total sales amount of the previous month.

I tried using the below expression, but the previous month script does not seems to work. It will still display as the current month figure:

SET ThousandSep=',';

SET DecimalSep='.';

SET MoneyThousandSep=',';

SET MoneyDecimalSep='.';

SET MoneyFormat='$#,##0.00;($#,##0.00)';

SET TimeFormat='h:mm:ss TT';

SET DateFormat='MM/DD/YYYY';

SET TimestampFormat='MM/DD/YYYY h:mm:ss[.fff] TT';

SET MonthNames='Jan;Feb;Mar;Apr;May;Jun;Jul;Aug;Sep;Oct;Nov;Dec';

SET DayNames='Mon;Tue;Wed;Thu;Fri;Sat;Sun';

LOAD Year,

     Month,

     Product,

     Sales,

     Date(Date#(Left(FileBaseName(), 10), 'MM/DD/YYYY')) As FileDate

FROM

(biff, embedded labels, table is Sheet1$);

Current Month Expression:

= sum({<FileDate = {">=$(=MonthStart(Today()))<= $(=MonthEnd(Today()))"}>}Sales)

Previous Month Expression:

= sum({<FileDate = {">=$(=MonthStart(AddMonths(Today(),-1)))<= $(=MonthEnd(AddMonths(Today(),-1)))"}>}Sales)

Please help. Appreciate your time and help in this.

Thank you.

1 Solution

Accepted Solutions
stevedark
Partner Ambassador/MVP
Partner Ambassador/MVP

Hi Andy,

You look like you had got yourself in a bit of an over complicated situation there.  There was a significant bug in the INLINE load - causing the field names to be mangled.

All of the derivation of date parts is much more easily done in script, rather than joining inline tables anyway.  Also, you only need the one Date field as you can derive everything else from that.  The load script then simply becomes:

ExpenseData:

LOAD

     Date,

     Month(Date) as Month,

     Date(MonthStart(Date), 'MMM-YYYY') as [Month Year],

     Year(Date) as Year,

     Expenses,

     Act_Amt

FROM [.\Data2.xls]

(biff, embedded labels, table is Sheet3$);

You can add a whole lot of other Date derivations if you like (quarter etc.) you will find code for these on the Community forum - or in the examples I posted links to above.

For simple month to date comparison you need only two variables set:

set vMaxMonthYear = =Date(max([Month Year]), 'MMM-YYYY');

set vPriorMonthYear = =Date(addmonths(max([Month Year]), -1), 'MMM-YYYY');

And then the expressions for the current month and prior month columns are simply:

Sum({<[Month Year]={'$(vMaxMonthYear)'},Year=,Month=,Date=>}Act_Amt)

and

Sum({<[Month Year]={'$(vPriorMonthYear)'},Year=,Month=,Date=>}Act_Amt)

The short coming of this is that the current month will most likely only be month to date (as you can't look into the future) and the prior month will be a full month.  As all of your dates are in as day one then this is the best you can do anyway.  If you had days in the data you would also want to derive the day number, in the load:

Day(Date) as Day,

Find the day for the max month:

set vMaxDay = =Day(Max(Date));

And then include that in the expressions also:


Sum({<[Month Year]={'$(vPriorMonthYear)'},Day={'<=$(vMaxDay)'},Year=,Month=,Date=>}Act_Amt)

The only thing you would need to worry about there is months with fewer days, ie. on the 28th of Feb you would probably want to compare with the 31st of Jan - not the 28th.

You then also get into seasonality and number of weekends in any given month - and things start getting a lot more complicated.  I digress.

Hopefully the load script amendments, variables and expressions at the top of this post will solve your immediate issues.

I've attached a working example as well.

Regards,

Steve

View solution in original post

42 Replies
nilesh_gangurde
Partner - Specialist
Partner - Specialist

Hi,

The above expressions are correct. just check the format of date Field. Ideally it should be in Number.

Try using below expression.

LOAD Year,

     Month,

     Product,

     Sales,

     Date(Date#(Left(FileBaseName(), 10), 'MM/DD/YYYY')) As FileDate1,

     num(Date(Date#(Left(FileBaseName(), 10), 'MM/DD/YYYY'))) As FileDate

FROM

(biff, embedded labels, table is Sheet1$);

Current Month Expression:

= sum({<FileDate1 = {">=$(=MonthStart(Today()))<= $(=MonthEnd(Today()))"}>}Sales)

Previous Month Expression:

= sum({<FileDate1 = {">=$(=num(floor(MonthStart(AddMonths(Today(),-1)))))<= $(=num(floor(MonthEnd(AddMonths(Today(),-1)))))"}>}Sales)

-Nilesh

SunilChauhan
Champion II
Champion II

See the attached file.

You can apply this in ur application

hope this helps

Sunil Chauhan
Not applicable
Author

Hi Nilesh,

Thank you for your reply.

The date format is in number but it still doesn't work.

But thanks for helping out, really appreciate it.

Not applicable
Author

Hi Sunil,

Thank you very much for taking your time to create a dummy file. It is very helpful.

It works now.

Thank you!

Not applicable
Author

Hi Sunil,

If I want to display the months from Jan-Dec, without Duplicate. How should I go about doing it?

Currently, it will display as follow:

Capture.PNG.png

Attached a working file and data file for your reference:

PrashantSangle

Hi,

You can separate your month field from your date.

use Month(yourdatefield) as Month

Great dreamer's dreams never fulfilled, they are always transcended.
Please appreciate our Qlik community members by giving Kudos for sharing their time for your query. If your query is answered, please mark the topic as resolved 🙂
SunilChauhan
Champion II
Champion II

see the attached file

I THING Requirement chart is what you looking for.

hope this helps

Sunil Chauhan
Not applicable
Author

Hi max,

Could you show an example using my working file attached?

Thank you!

Not applicable
Author

Hi Sunil,

No, i wasn't looking for a requirement chart. In fact i need to create a dashboard, so the Month field ideally should not be an extensive list.

I've tried this as well, it display the months field properly but there are 2 issues:

1. I need to display the month number in a text month format, but when i tried to change it to the month format, data does not display properly.

2. The data for previous month of Jan i.e. Dec does not appear.

Maybe you have a solution to the one above?