{"id":205,"date":"2006-10-31T14:46:40","date_gmt":"2006-10-31T21:46:40","guid":{"rendered":"http:\/\/www.nicholasgoodman.com\/bt\/blog\/2006\/10\/31\/trend-lines-in-mondrian\/"},"modified":"2006-10-31T14:46:40","modified_gmt":"2006-10-31T21:46:40","slug":"trend-lines-in-mondrian","status":"publish","type":"post","link":"http:\/\/www.nicholasgoodman.com\/bt\/blog\/2006\/10\/31\/trend-lines-in-mondrian\/","title":{"rendered":"Trend Lines in Mondrian"},"content":{"rendered":"<p>I often mouth off on the importance and power of getting your data into a star-schema and Mondrian; the power you have to respond to time variant and analytic needs of your users is immense.&nbsp; In the next few weeks I&#8217;ll cover more about these powers in a more concrete form, showing specific examples instead of just alluding to them.<\/p>\n<p>Starting with a relatively straightforward implementation of a Trend Line.&nbsp; Traditionally a trend line is built using a good old fashion linear regression on a set of data and then used to calculated current and future X and Y coordinates.&nbsp; This usually involves some knowledge about building the linear regression formula, and then calculating points based on it.&nbsp; Fortunately for us, we can skip most of this tedious process and just use an MDX function, LinRegPoint, to sort out most of that difficulty and we&#8217;ll just enjoy a beautiful trend line on our graph.<\/p>\n<p>Let&#8217;s start with the output, so it&#8217;s clear what we&#8217;re talking about:<br \/><img decoding=\"async\" src=\"http:\/\/www.nicholasgoodman.com\/entry_images\/mondrian_lin_reg.png\" \/><\/p>\n<p>The RED is the data set, and the BLUE is the trend line we&#8217;ve built using MDX.<\/p>\n<p>The only thing you need to run through this tip is the <a href=\"http:\/\/www.pentaho.org\/download\/latest\">Pentaho Demo download<\/a>, available at <a href=\"http:\/\/www.pentaho.org\/download\/latest\">http:\/\/www.pentaho.org\/download\/latest<\/a>.&nbsp; I used 1.2RC2 for this example, but it should work on versions more recent than that.&nbsp; It&#8217;s zero installation and starts up with everything you need for this tip.<\/p>\n<p>Start up pentaho (start-pentaho.bat) and hop into your web browser (http:\/\/localhost:8080).&nbsp; Navigate to the &#8220;Samples&#8221; section and into &#8220;Steel Wheels.&#8221;&nbsp; Steel Wheels is an example we&#8217;re shipping with the demo installation now which provides some great time variant data examples (needed to do interesting things with OLAP).&nbsp; Steel Wheels data is the sample data provided by the <a href=\"http:\/\/www.eclipse.org\/birt\/phoenix\/\">BIRT folks at Eclipse<\/a>, actually.<\/p>\n<p>Navigate to the Analysis folder, and then to &#8220;01. Territory Analysis by Year.&#8221;&nbsp; It doesn&#8217;t really matter which one, we just need to get into JPivot on our Steel Wheels cube.&nbsp; <\/p>\n<p>Click on the MDX button and paste the following MDX fragment to get a base &#8220;sales view&#8221; and hit Apply:<b><br \/><\/b><\/p>\n<blockquote><p><b>select {[Measures].[Sales]} ON COLUMNS,<\/b><br \/><b>&nbsp; {[Time].[Months].Members} ON ROWS<\/b><br \/><b>from [SteelWheelsSales]<\/b><br \/><b>where [Customers].[All Customers]<\/b><\/p><\/blockquote>\n<p>You should get a result that looks like this:<br \/><img decoding=\"async\" src=\"http:\/\/www.nicholasgoodman.com\/entry_images\/mondrian_trend_line_base_mdx_table.jpg\" \/><\/p>\n<p>Ok&#8230; Now it&#8217;s time to build our Calculated Member.&nbsp; This is kind of hairy: it requires some technical prowess to get the MDX calculation correct.&nbsp; Just remember, once you&#8217;ve got the calcuation working properly you can <a href=\"http:\/\/www.nicholasgoodman.com\/bt\/blog\/2006\/06\/09\/mondrian-olap-power-at-your-users-fingertips\/\">include it as part of the Cube<\/a> so your business users (using JPivot or <a href=\"http:\/\/www.pentaho.org\/docs\/pentaho_spreadsheet_services.pdf\">Pentaho Spreadsheet Services<\/a>) don&#8217;t see that complexity.<\/p>\n<p>We&#8217;re going to use an MDX function, named <a href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/ms144752.aspx\">LinRegPoint<\/a> (reference link). I think the best online tutorial was done by Mosha Pasumanksy in his blog entitled &#8220;<a href=\"http:\/\/sqljunkies.com\/WebLog\/mosha\/archive\/2004\/12\/21\/5689.aspx\">Using Linear Regression MDX functions for forecasting<\/a>&#8221;&nbsp; I used his tutorial to help build the regression below!&nbsp; I won&#8217;t get into the details of linear regressions; you can read the reference or do some other googling for Linear Regressions.&nbsp; <\/p>\n<p>Basically, you rank Time to get straight numbers (X coordinates: 1,2,3,4,5), use your measure Sales as your value to regress (Y coordinates: 129754, 140836, &#8230;) and then you get it the ranked time as INPUT to your Linear Regression (which time is this) and it CALCULATES the Y output based on the Linear Regression it&#8217;s built.<\/p>\n<p>Our LinRegPoint MDX formula comes down to:<\/p>\n<blockquote><p><b>LinRegPoint(<\/b><br \/><b>&nbsp; Rank(<\/b><br \/><b>&nbsp;&nbsp;&nbsp;&nbsp; [Time].CurrentMember,<\/b><br \/><b>&nbsp;&nbsp;&nbsp;&nbsp; [Time].CurrentMember.Level.Members),<\/b><br \/><b>&nbsp; {[Time].CurrentMember.Level.Members},<\/b><br \/><b>&nbsp;&nbsp; [Measures].[Sales],<\/b><br \/><b>&nbsp;&nbsp; Rank(<\/b><br \/><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [Time].CurrentMember, <\/b><br \/><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [Time].CurrentMember.Level.Members)<\/b><br \/><b>)<\/b><\/p><\/blockquote>\n<p>Enter the following MDX Fragment into the MDX editor to see the results of the Linear regression on Steel Wheels.<\/p>\n<blockquote><p><b>with member [Measures].[Line] as<\/b><br \/><b>&#8216;LinRegPoint(Rank([Time].CurrentMember,<\/b><br \/><b>[Time].CurrentMember.Level.Members),<\/b><br \/><b>{[Time].CurrentMember.Level.Members}, [Measures].[Sales],<\/b><br \/><b>Rank([Time].CurrentMember, [Time].CurrentMember.Level.Members))&#8217;<\/b><br \/><b>select Crossjoin({[Markets].[All Markets]}, {[Measures].[Sales], [Measures].[Line]}) ON COLUMNS,<\/b><br \/><b>&nbsp; {[Time].[Months].Members} ON ROWS<\/b><br \/><b>from [SteelWheelsSales]<\/b><br \/><b>where [Customers].[All Customers]<\/b><\/p><\/blockquote>\n<p>And you should see the following output:<br \/><img decoding=\"async\" src=\"http:\/\/www.nicholasgoodman.com\/entry_images\/mondrian_trend_line_table_with_graph.jpg\" \/><br \/><i>Note: if you want to see the graph on the right, change the chart settings (icons at top of page) to be a Horizontal Line chart, Width = 300 and Height = 600.<\/i><\/p>\n<p>The steel wheels only has data extending to [2005].[May].&nbsp; If we had &#8220;time&#8221; members extending beyond our data set the line would extend to the future.&nbsp; Careful; a simple linear regression is not best practice for doing forecasting on MANY things.&nbsp; However, business users like to see the overall trend, and slope.<\/p>\n<p>Was this helpful?&nbsp; What would you like to see next?&nbsp; Rolling Averages?&nbsp; It&#8217;s VERY IMPORTANT to note that in most circumstances &#8220;MDX examples&#8221; for Microsoft Analysis Services works with Pentaho.&nbsp; There&#8217;s a <strike>dirth<\/strike> BUNCH of articles about MDX on MSAS&#8230; That&#8217;s a wealth of tutorials that apply to your work with Pentaho.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I often mouth off on the importance and power of getting your data into a star-schema and Mondrian; the power you have to respond to time variant and analytic needs of your users is immense.&nbsp; In the next few weeks I&#8217;ll cover more about these powers in a more concrete form, showing specific examples instead [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[16,11],"tags":[],"_links":{"self":[{"href":"http:\/\/www.nicholasgoodman.com\/bt\/blog\/wp-json\/wp\/v2\/posts\/205"}],"collection":[{"href":"http:\/\/www.nicholasgoodman.com\/bt\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.nicholasgoodman.com\/bt\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.nicholasgoodman.com\/bt\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.nicholasgoodman.com\/bt\/blog\/wp-json\/wp\/v2\/comments?post=205"}],"version-history":[{"count":0,"href":"http:\/\/www.nicholasgoodman.com\/bt\/blog\/wp-json\/wp\/v2\/posts\/205\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.nicholasgoodman.com\/bt\/blog\/wp-json\/wp\/v2\/media?parent=205"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.nicholasgoodman.com\/bt\/blog\/wp-json\/wp\/v2\/categories?post=205"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.nicholasgoodman.com\/bt\/blog\/wp-json\/wp\/v2\/tags?post=205"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}