{"id":2751,"date":"2016-08-16T22:42:48","date_gmt":"2016-08-17T02:42:48","guid":{"rendered":"http:\/\/kenhamady.com\/cru\/?p=2751"},"modified":"2016-08-16T23:29:39","modified_gmt":"2016-08-17T03:29:39","slug":"adding-a-ratio-calculation-to-a-cross-tab","status":"publish","type":"post","link":"https:\/\/kenhamady.com\/cru\/archives\/2751","title":{"rendered":"Adding a ratio calculation to a cross-tab"},"content":{"rendered":"<p>Twice in the past 2 weeks I have had to take two different summary fields in a cross-tab and calculate a ratio of one to the other.\u00a0 The most common example would be when one summary is &#8220;Sales&#8221; and the other is &#8220;Cost of Goods&#8221; and you want to calculate a Gross Profit Percent from them.\u00a0 This type of calculation got much easier with CR 2008, because you can now add calculations that draw values from other cells in the cross-tab.\u00a0 The functions that do this is can get pretty complex but this example is not that bad.<\/p>\n<p>In our example the row and column fields can be anything.\u00a0 All we need are two different summary fields:<\/p>\n<p style=\"padding-left: 30px;\">Sum of Sales<br \/>\nSum of Cost of Goods<\/p>\n<p>And for this example the summary fields will be arranged vertically, with the Sales on the top and the Cost of Goods below it.\u00a0 The steps are:<\/p>\n<p>1) Right click on the Cost of Goods Summary in any cell and select<\/p>\n<p style=\"padding-left: 30px;\">Embedded Summary &gt; Insert Embedded Summary<\/p>\n<p>This should add a third summary row in each cell of the cross-tab. The will all say &#8220;Edit this formula&#8221;.<\/p>\n<p>2) Right click on that phrase and select<\/p>\n<p style=\"padding-left: 30px;\">Embedded Summary &gt; Edit Calculation Formula<\/p>\n<p>3) Paste in the following formula:<\/p>\n<p><code>Local NumberVar\u00a0 Sales :=<br \/>\nGridValueAt (CurrentRowIndex, CurrentColumnIndex , CurrentSummaryIndex -2 );<\/code><\/p>\n<p><code>Local CurrencyVar COGS :=<br \/>\nGridValueAt (CurrentRowIndex, CurrentColumnIndex , CurrentSummaryIndex -1 );<\/code><\/p>\n<p><code>if Sales = 0 then 0 else<br \/>\n(Sales - Cogs) % Sales<\/code><\/p>\n<p>Here is how the above formula works.\u00a0 The first two rows read the cross-tab value from 2 rows up (Sum of Sales) and hold that value in a local variable called Sales.\u00a0 That way I can refer to that value several times simply without having to repeat the entire function each time.\u00a0 The next two rows do the same thing with Sum of COGS which is pulled from 1 row up.\u00a0 The last two lines do the actual calculation, including a check to make sure that Sales are not zero.\u00a0 That prevents the &#8216;divide by zero&#8221; error.\u00a0\u00a0 (Note, if your Sales and and COGS fields are currency fields the formula will give you an error, so change the variables to CurrencyVar).<\/p>\n<p>4) Save the formula and the third summary row in every cell should calculate the ratio based on the two numbers immediately above.\u00a0 The cross-tab will perform the calculation in all group levels of the cross-tab including the grand total rows and columns.<\/p>\n<p>If you need help deploying this formula or need something slightly different, let me know and we can schedule a session to get you what you need.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Twice in the past 2 weeks I have had to take two different summary fields in a cross-tab and calculate a ratio of one to the other.\u00a0 The most common example would be when one summary is &#8220;Sales&#8221; and the other is &#8220;Cost of Goods&#8221; and you want to calculate a Gross Profit Percent from [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,7],"tags":[],"class_list":["post-2751","post","type-post","status-publish","format-standard","hentry","category-formulas","category-method"],"_links":{"self":[{"href":"https:\/\/kenhamady.com\/cru\/wp-json\/wp\/v2\/posts\/2751","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kenhamady.com\/cru\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kenhamady.com\/cru\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kenhamady.com\/cru\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kenhamady.com\/cru\/wp-json\/wp\/v2\/comments?post=2751"}],"version-history":[{"count":0,"href":"https:\/\/kenhamady.com\/cru\/wp-json\/wp\/v2\/posts\/2751\/revisions"}],"wp:attachment":[{"href":"https:\/\/kenhamady.com\/cru\/wp-json\/wp\/v2\/media?parent=2751"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kenhamady.com\/cru\/wp-json\/wp\/v2\/categories?post=2751"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kenhamady.com\/cru\/wp-json\/wp\/v2\/tags?post=2751"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}