title: "Total rows" in custom Power BI visualizations
body: <p>I have a question about creating the <a href="https://github.com/Microsoft/PowerBI-visuals" rel="nofollow noreferrer">custom visualization</a> in Power BI.</p>
  
  <p>I want to implement a "total row" functionality which is available in the built-in matrix visualization. The main concept is to automatically sum-up every value and group it by the rows. This is how it's looks like on the matrix visualization:
  <a href="https://i.stack.imgur.com/rt1XZ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rt1XZ.png" alt="Built-in matrix &quot;total rows&quot; functionality"></a></p>
  
  <p>But, to be honest, I don't know how to achieve this. I try different things but I can't receive this grouped values in the dataViews.</p>
  
  <p>I tried to analyze the built-in <a href="https://github.com/Microsoft/PowerBI-visuals/blob/master/src/Clients/Visuals/visuals/matrix.ts" rel="nofollow noreferrer">matrix.ts code</a> but it's quite different that the custom visualizations code. I found the <a href="https://github.com/Microsoft/PowerBI-visuals/blob/master/src/Clients/Visuals/visuals/matrix.ts#L918" rel="nofollow noreferrer">customizeQuery</a> method which set the subtotalType property to the rows and columns - I tried to add this in my code but I don't see any difference in the dataViews (I don't found the grouped value).</p>
  
  <p>Currently my capabilities.dataViewMappings is set like this:</p>
  
  <pre><code>            dataViewMappings: [
              {
                  conditions: [
                      { 'Rows': { max: 3 } }
                  ],
                  matrix: {
                      rows: {
                          for: { in: 'Rows' },
                      },
                      values: {
                          for: { in: 'Values' }
                      },
                  },
              }
          ]
  </code></pre>
  
  <p>Does anyone know how we could achieve this "total row" functionality?</p>
  
  <p><strong>UPDATE 1</strong></p>
  
  <p>I already found the solution: when we implement the <code>customizeQuery</code> method (in the same way as the <a href="https://github.com/Microsoft/PowerBI-visuals/blob/master/src/Clients/Visuals/visuals/matrix.ts#L918" rel="nofollow noreferrer">customizeQuery</a> method in the matrix.ts code), and then add the reference to it in the <code>powerbi.visuals.plugins.[visualisationName+visualisationAddDateEpoch].customizeQuery</code> then it works as expected (I receive in the <code>dataViews[0].matrix.row.root</code> children elements that has the total values from row).</p>
  
  <p>The only problem now is that I don't know exactly how to add correctly this reference to the <code>customizeQuery</code> method. For example the <code>[visualisationName+visualisationAddDateEpoch]</code> is <code>Custom1451458639997</code>, and I don't know what those number will be (I know only the name). I created the code in my visualisation constructor as below (and it's working):</p>
  
  <pre><code>    constructor() {
          var targetCustomizeQuery = this.constructor.customizeQuery;
          var name = this.constructor.name;
  
          for(pluginName in powerbi.visuals.plugins) {
              var patt = new RegExp(name + "[0-9]{13}");
              if(patt.test(pluginName)) {
                  powerbi.visuals.plugins[pluginName].customizeQuery = targetCustomizeQuery;
                  break;
              }
          }
      }
  </code></pre>
  
  <p>But in my opinion this code is very dirty and inelegant. I want to improve it - what is the correct way to tell the Power BI that we implement the custom <code>customizeQuery</code> method and it should use it?</p>
  
  <p><strong>UPDATE 2</strong></p>
  
  <p>Code from <strong>update 1</strong> works only with the Power BI in the web browser (web based). On the Power BI Desktop the <code>customizeQuery</code> method isn't invoked. What is the correct way to tell the Power BI to use our custom <code>customizeQuery</code> method? In the code from <a href="https://github.com/Microsoft/PowerBI-visuals" rel="nofollow noreferrer">PowerBI-visuals repository</a> using PowerBIVisualPlayground we could declare it in the <a href="https://github.com/Microsoft/PowerBI-visuals/blob/master/src/Clients/Visuals/plugins.ts#L258" rel="nofollow noreferrer"><code>plugin.ts</code></a> file (in the same way like the matrix visual is done):</p>
  
  <pre><code>export let matrix: IVisualPlugin = {
      name: 'matrix',
      watermarkKey: 'matrix',
      capabilities: capabilities.matrix,
      create: () =&gt; new Matrix(),
      customizeQuery: Matrix.customizeQuery,
      getSortableRoles: (visualSortableOptions?: VisualSortableOptions) =&gt; Matrix.getSortableRoles(),
  };
  </code></pre>
  
  <p>But, in my opinion, from the Power BI Dev Tools we don't have access to add additional things to this part of code. Any ideas?</p>