How to calculate the difference between two dates as DMN input?

First, I’m not a developer. Whatever seems obvious for a dev, it might be exactly what I’m looking for.

I need to check if the time we took to execute two different actions was within the SLA and score it accordingly. For me the easiest way is a DMN.

So far, we have:

  • The date the instance was created (which indicated when the process started), named startProcessDateAndTime
  • The date the event was completed, named eventDateAndTime
  • The SLA is 5 days
  • If the event was completed within the SLA the score will be 100, otherwise it will be 0

Okay so I need to:
1- Calculate the difference between two date variables and set the result as another variable
2- Use this second variable as a DMN input, check if it’s below the set SLA value and as output I have the score

The DMN I’m using:

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="https://www.omg.org/spec/DMN/20191111/MODEL/" xmlns:dmndi="https://www.omg.org/spec/DMN/20191111/DMNDI/" xmlns:dc="http://www.omg.org/spec/DMN/20180521/DC/" xmlns:camunda="http://camunda.org/schema/1.0/dmn" xmlns:di="http://www.omg.org/spec/DMN/20180521/DI/" id="Definitions_085zzt2" name="DRD" namespace="http://camunda.org/schema/1.0/dmn" exporter="Camunda Modeler" exporterVersion="4.11.1">
  <decision id="Decision_1q2le9j" name="Score">
    <informationRequirement id="InformationRequirement_0gcspvs">
      <requiredDecision href="#Decision_0fmymyn" />
    </informationRequirement>
    <decisionTable id="DecisionTable_0gij6h8">
      <input id="Input_1" label="Period" camunda:inputVariable="period">
        <inputExpression id="InputExpression_1" typeRef="string">
          <text></text>
        </inputExpression>
      </input>
      <output id="Output_1" label="Score" typeRef="string" />
      <rule id="DecisionRule_1d0m9h0">
        <description>within SLA</description>
        <inputEntry id="UnaryTests_1i5pdng">
          <text>period &lt; 5</text>
        </inputEntry>
        <outputEntry id="LiteralExpression_1vo7weh">
          <text>100</text>
        </outputEntry>
      </rule>
      <rule id="DecisionRule_0c655ev">
        <description>outside SLA</description>
        <inputEntry id="UnaryTests_1t45o97">
          <text></text>
        </inputEntry>
        <outputEntry id="LiteralExpression_1gwubpp">
          <text>0</text>
        </outputEntry>
      </rule>
    </decisionTable>
  </decision>
  <decision id="Decision_0fmymyn" name="Calculate the difference bewteen the events">
    <variable id="InformationItem_0x7f6fy" name="period" typeRef="integer" />
    <literalExpression id="LiteralExpression_0rse4us" expressionLanguage="javascript">
      <text>Math.ceil(Math.abs(eventDateAndTime - startProcessDateAndTime) / (1000 * 60 * 60 * 24))</text>
    </literalExpression>
  </decision>
  <dmndi:DMNDI>
    <dmndi:DMNDiagram>
      <dmndi:DMNShape dmnElementRef="Decision_1q2le9j">
        <dc:Bounds height="80" width="180" x="160" y="100" />
      </dmndi:DMNShape>
      <dmndi:DMNShape id="DMNShape_03fqghl" dmnElementRef="Decision_0fmymyn">
        <dc:Bounds height="80" width="180" x="-70" y="100" />
      </dmndi:DMNShape>
      <dmndi:DMNEdge id="DMNEdge_0jx6d6p" dmnElementRef="InformationRequirement_0gcspvs">
        <di:waypoint x="110" y="140" />
        <di:waypoint x="140" y="140" />
        <di:waypoint x="160" y="140" />
      </dmndi:DMNEdge>
    </dmndi:DMNDiagram>
  </dmndi:DMNDI>
</definitions>

Tried using a literal expression but still having trouble.

Hi @Neeniih
I’ve created a simple example for you that might give you some ideas. Please see attached.simulation.dmn (1.5 KB)

You can upload this DMN to Camunda DMN-Simulator, Simply drag your DMN file on the decision table area in this website and play with it.
For example, you can enter Input A as “2022-02-02” and Input B as “2022-02-01”.
The output “Output A” will be “PT24H” which is 24 hours or one day.

I used “date(InputA) - date(InputB)” to calculate the difference between dates.
Also, you can find other options there - Temporal Expressions | FEEL-Scala

Hope it helps.

1 Like

Hi Alex!

I’m again in need of the same thing but now both the inputs are in date format, is there a way?