Salvatore
Salvatore

Reputation: 1

Webdatarocks - Difference from two date

I would calculate difference from 2 date in some dataset? Is it Possibile? Another calculate is age? One date is in the dataset and diff with today Is it Possibile? Im test calculate formula but I not have found nothing

Thanks

Upvotes: -1

Views: 59

Answers (1)

nadia khodakivska
nadia khodakivska

Reputation: 76

You can assign the today's date in milliseconds to the variable, for example:

var today = new Date().getTime();

Then, use this variable in the calculated value. Feel free to check the following code snippet illustrating such an approach:

var today = new Date().getTime();
var pivot = new WebDataRocks({
  container: "#wdr-component",
  toolbar: true,
  height: 490,
  report: {
    dataSource: {
      data: getData()
    },
    slice: {
      rows: [
        {
          uniqueName: "Date"
        }
      ],
      measures: [
        {
          uniqueName: "Year difference",
          formula: `(${today}-max(\"Date\")) / (1000 * 60 * 60 * 24 * 365.25)`,
          caption: "Year difference"
        }
      ]
    },
    formats: [
        {
            name: "",
            decimalPlaces: 0,
        }
    ],
    options: {
      grid: {
        type: "flat"
      }
    }
  }
});

function getData() {
  return [
    {
      Date: {
        type: "date string"
      }
    },
    {
      Date: "2002-01-22",
      Income: 174
    }
  ];
}
<link href="https://cdn.webdatarocks.com/latest/webdatarocks.min.css" rel="stylesheet"/>
<script src="https://cdn.webdatarocks.com/latest/webdatarocks.toolbar.min.js"></script>
<script src="https://cdn.webdatarocks.com/latest/webdatarocks.js"></script>
<div id="wdr-component"></div>

Upvotes: 0

Related Questions