COGNOiSe.com - The IBM Cognos Community

IBM Cognos 10 Platform => Cognos 10 BI => Report Studio => Topic started by: willg on 09 Jul 2013 05:46:24 AM

Title: Sort values by month & year in a chart.
Post by: willg on 09 Jul 2013 05:46:24 AM
Hi Everyone,

I'm currently trying to produce a chart which sorts the x-axis by the month&year of a column. I've tried using the data format to only display the information I want, however then the chart is not grouped, and just displays multiple values of each (eg. Jan 2011, Jan 2011, Jan 2011, Feb 2011, March 2011, March 2011 etc etc).

The only way I've managed to actually get the data to group is by using the extract() function. Specifically;


case extract(month,[date])
when 1 then 'January'
when 2 then 'February'
when 3 then 'March'
when 4 then 'April'
when 5 then 'May''
when 6 then 'June'
when 7 then 'July'
when 8 then 'August'
when 9 then 'September'
when 10 then 'October'
when 11 then 'November'
when 12 then 'December'
else 'error'
end


Though the problem with the above code is that cognos then just groups the data to each month, regardless of the year. For example, grouping data from January 2011 & January 2012 in the same column. Has anybody had any experience with this, or knows how I could sort the x-axis (and display it) by month & year instead of just by month.

Thanks in advance,
Will.
Title: Re: Sort values by month & year in a chart.
Post by: pricter on 09 Jul 2013 06:15:33 AM
Create a data item with the combination of year month
for example 201201
and sort the x-axis based on this data item
Title: Re: Sort values by month & year in a chart.
Post by: willg on 09 Jul 2013 06:35:33 AM
Thanks for the reply, how exactly would I make a data item with the combination of year/month such as 'January 2011'?
Title: Re: Sort values by month & year in a chart.
Post by: pricter on 09 Jul 2013 06:39:24 AM
One option will be:
extract(year, [Date]) *100 + extract(month, [Date])

Title: Re: Sort values by month & year in a chart.
Post by: willg on 09 Jul 2013 06:57:31 AM
Brilliant, that works well thanks a lot. Any idea on how I could edit the format (201203, 201204 etc) to look like an actuall date format? (March 2012, April 2012 etc)?

Title: Re: Sort values by month & year in a chart.
Post by: pricter on 09 Jul 2013 07:04:52 AM
Maybe I was so clear.
You create two data items
One for showing what you want for example January 2013 (each database has some functions to achieve this format)
and one in order to sort the x - labels for example 201301
Title: Re: Sort values by month & year in a chart.
Post by: willg on 09 Jul 2013 07:14:30 AM
Sorry to keep asking questions, but then how do I sort the x-axis by one data item, but then make it display a different one?
Title: Re: Sort values by month & year in a chart.
Post by: Lynn on 09 Jul 2013 07:59:37 AM
Quote from: willg on 09 Jul 2013 07:14:30 AM
Sorry to keep asking questions, but then how do I sort the x-axis by one data item, but then make it display a different one?

Questions are a big part of what makes this forum useful  ;D

Go to the page layout and click on the data item that you dragged into the x-axis of your chart. That one should be the query item you want to use for display. Once you've selected that in the layout, look for the sorting property in the properties pane and click the ellipses. Specify the data item that pricter described to give you YYYYMM format. That data item should already be in the query in order for you to select it.

Hope this helps!
Title: Re: Sort values by month & year in a chart.
Post by: willg on 09 Jul 2013 10:13:07 AM
Still doesn't work as intended  :-\ but thanks all for the help anyway
Title: Re: Sort values by month & year in a chart.
Post by: willg on 24 Jul 2013 03:56:28 AM
Hi once again, I thought I'd revive this post as I have come back to work on my project and still cannot figure this out. Currently I have two ways of displaying the date, one is as follows;
(http://i.imgur.com/uwOhUPb.jpg)

Though the issue with this is that every date field is displayed separately, where I want them to group the values by month.

The other method I have to display the date is via 'extract(year, [date])*100 + extract(month, [date])' which returns the following;
(http://i.imgur.com/Vg0jYqh.jpg)

As you can see this does group the values by month, although the issue is that my date field has now been converted into an integer field. And I would much prefer a layout of '07-2012/July-2012' rather than '201207'.

If anyone can please help me with this it would be greatly appreciated..
Title: Re: Sort values by month & year in a chart.
Post by: BigChris on 24 Jul 2013 04:57:27 AM
Have you tried something along the lines of datename({mm},[date]) + '/' + extract(year,[date])? You might need to play around a bit with it, but that should give you something close to what you're looking for...

C
Title: Re: Sort values by month & year in a chart.
Post by: CognosPaul on 24 Jul 2013 05:49:28 AM
Try the following:

cast (_first_of_month([date]),date)

This will always return the first day of the month as a date without time.  In the chart, set the source type of the x-axis to Data Item Value and use the Data Format to format it the way you want.
Title: Re: Sort values by month & year in a chart.
Post by: Michael75 on 24 Jul 2013 05:53:48 AM
I just had a similar requirement - to display months in a chart as Apr-13, May-13, Jun-13 etc. My input field is [BALANCE MONTH], in the same YYYYMM format as yours, and I came up with this:

concat (concat (to_char (to_date (BALANCE_MONTH, 'YYYYMM'), 'Mon'), '-'), substr(BALANCE_MONTH, 3, 2))

You could adapt this to your particular requirements.
Title: Re: Sort values by month & year in a chart.
Post by: willg on 24 Jul 2013 06:01:22 AM
Thanks Chris, that looks promising, the only problem is I get an error when I try to use both 'datename' and 'extract' in the same expression, I can do them in different data items and it works fine however;
(http://i.imgur.com/Se3LPlK.jpg)

And the errors recieved when trying to place in the same expression;
(http://i.imgur.com/FzGpA87.jpg)



And Paul thanks for the reply, that expression successfully returns the first day without time (eg. 01-05-2011) but if I then go into data format and change the display, no matter what I change the date format does not get altered. Why do you think this is?

I feel like this is something that should be easily accomplished but it has given me far too much hassle.
Title: Re: Sort values by month & year in a chart.
Post by: BigChris on 24 Jul 2013 06:16:24 AM
Ok - it looks like the extract element is bringing back an integer for the year...have you tried casting it as char(4)? There's a Year function in Cognos, but I think it brings back the same data as the extract function.
Title: Re: Sort values by month & year in a chart.
Post by: willg on 24 Jul 2013 06:24:26 AM
Success! Thanks a lot Chris, using the cast worked. Also Michael thanks for the reply, though I got an error and never got round to trying it properly.  :)
Title: Re: Sort values by month & year in a chart.
Post by: CognosPaul on 24 Jul 2013 07:50:50 AM
The data format won't affect a member caption. In the Chart Node Member set the Source Type to Data Item Value. Do that and you'll be able to set the data format to however you want. Since it will still be in date format, you can still use that to do your sorting, and any future formatting changes will be significantly easier.
Title: Re: Sort values by month & year in a chart.
Post by: willg on 24 Jul 2013 08:19:26 AM
I'd had that problem in the past, and as such I had already set the source type to 'data item value'. So I'm not sure why it doesn't want to work that way
Title: Re: Sort values by month & year in a chart.
Post by: CognosPaul on 24 Jul 2013 08:43:39 AM
That's really weird.

I just tried it on one of the samples, and it works perfectly. See the attached report.
Title: Re: Sort values by month & year in a chart.
Post by: willg on 24 Jul 2013 10:33:01 AM
Do you know of any possible reasons why that could be?

-The database/table/query I'm using?
-The install version I'm using?
-etc

Also, when I tried to open your report into cognos using 'Open Report From Clipboard' I got the following error;
(http://i.imgur.com/Z13I2j6.jpg)

Though I am using Cognos V10.1.1 so I'm surprised I got that error.
Title: Re: Sort values by month & year in a chart.
Post by: CognosPaul on 24 Jul 2013 11:54:56 AM
Based on the error you posted before, I could tell that it's an SQL server. SQL Server has some oddities when it comes to date datatypes. Try casting back to datetime or timestamp.

I'm pretty sure I've done this in 10.1, so I don't think it's an issue with the version.

I created my example on 10.2.1. Downgrading the report is a simple matter of switching the 9 to an 8. Downgrading to earlier versions will be much harder.