Skip to main content
Announcements
Qlik Connect 2024! Seize endless possibilities! LEARN MORE
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

Javascript number of Dimensions or Measures

I am building an extension with allowing multiple dimensions and measurements:

  <Dimension Label="Dimensions" DropTarget="Dimensions" Require="true" Multiple="true"  />

  <Measurement Multiple="true" />

I would like to know the number of dimensions and the number of measurements inside the javascript code. I can get the total number of columns out of Data.HeaderRows[0].length, but I can't seem to find the number of dimensions and measurements (or if each column is tagged with dimension/measurement).  Any help would be appreciated. Thanks

7 Replies
rbecher
MVP
MVP

Hi Peter,

I'm looking for the same and cannot find any solution. One workaround (dirty though) would be to check if Data.HeaderRows[0].[index] contains a paranthesis "(" coming from an aggregation function like sum( ) to identify it as measure, else as dimension. However, I wonder why there is not API function to obtain type of column..

- Ralf

Astrato.io Head of R&D
Not applicable
Author

Yea i was afraid there was no good solution to this....Thanks Ralf

rbecher
MVP
MVP

Yes, but this works in most of the cases, hopefully.. 😉

Astrato.io Head of R&D
Alexander_Thor
Employee
Employee

As Ralf said you can check for brackets but there are edge cases where a user might just reference a field name and the only() function is implied that stated causing brackets checks to fail.

But if my memory serves me right cell rows that has the color property are expressions and vice versa.

this.Data.Rows[0][1].color <- if exists it's a expression.

Here is a helper function that I use whenever I want to enhance our data object with columnar data and some more meta data.

if (!Qva.Public.Wrapper.prototype.getData) {

  Qva.Public.Wrapper.prototype.getData = function() {

  var data = {},

  header = this.Data.HeaderRows[0];

  data.Rows = this.Data.Rows;

  data.Column = Object.keys(data.Rows[0]).map(function(c) {

       return data.Rows.map(function(r) {

            return r;

       });

  });

  data.Column.forEach(function(element, index) {

       element.type = element[0].color === undefined ? "expression" : "dimension";

       element.label = header[index].text;

  });

       return data;

  };

}

rbecher
MVP
MVP

Hi Alexander,

thanks, this seems to be workable. However, I do see color and value properties only for dimensions and not for expressions (and I wonder what the value means here).. So, what is wrong?

- Ralf

Astrato.io Head of R&D
rbecher
MVP
MVP

And what's even more strange, the color code changes randomly..

- Ralf

Astrato.io Head of R&D
rbecher
MVP
MVP

Hi Alexander,

thanks for the color hint. Now I use this simple line of code to determine the amount of dimensions:

   // get amount of Dimensions
  

var nDimensions = this.Data.Rows[0].filter(function(col){return !(col.color == undefined);}).length;

- Ralf

Astrato.io Head of R&D