EducationSoftwareStrategy.com
StrategyCommunity

Knowledge Base

Product

Community

Knowledge Base

TopicsBrowse ArticlesDeveloper Zone

Product

Download SoftwareProduct DocumentationSecurity Hub

Education

Tutorial VideosSolution GalleryEducation courses

Community

GuidelinesGrandmastersEvents
x_social-icon_white.svglinkedin_social-icon_white.svg
Strategy logoCommunity

© Strategy Inc. All Rights Reserved.

LegalTerms of UsePrivacy Policy
  1. Home
  2. Topics

KB47068: How to manipulate a column that returns second values and convert it to a timestamp in MicroStrategy


Community Admin

• Strategy


This article describes how to convert second column values into timestamps

There may be a scenario when a user wants to convert a column returning second values and convert it to a timestamp. This article incorporates formatting considerations as well as subtotaling and dynamic aggregation considerations for a timestamp metric that uses the out-of-the-box Strategy functions.
Strategy has three functions: Hour, Minute, and Second. These functions will pull the respective components from a timestamp value and return an integer for the hour, minute, or second components of a timestamp. There may be scenarios where converting a metric with just seconds into correct hours, minutes, and seconds are necessary.
 
LOGIC:
For the purposes of this article, the metric containing second data returning an integer value will be denoted as "X". Note it is important that this metric is not based on a string column and based on a number column.
There are three possible outputs for a timestamp which can be used and vary depending on the number of seconds:

  1. Minutes and Seconds (00:00)
  2. Hours, Minutes, and Seconds (00:00:00)
  3. Days, Hours, Minutes, and Seconds (00:00:00:00)

Depending on individual needs and the number of seconds returned, a user can use the first output for second values less than 3600, the second output for second values less than 86,400, and the third output for all others.
 
The following sections contain metric definitions that will properly calculate the values depending on the output chosen.
For the base logic, two out-of-the-box Strategy functions will be used: Quotient and Mod. Mod (or modulus) is equivalent to the remainder. Their function definitions are as follows:
Mod(Argument, Divisor)

  • Argument is a metric representing a list of real numbers.
  • Divisor is the number used to divide the value(s) of Argument.

Quotient(Numerator, Denominator)

  • Numerator is a metric representing a list of real numbers to be used as the dividend.
  • Denominator is a metric representing a list of real numbers to be used as the divisor.

A diagram of how these functions fit together with the dividend and divisor is as follows:
 

ka04W000000ObIuQAK_0EM440000002CMh.png

 
OUTPUT 1:
To return only minutes and seconds, create two metrics defined as follows:

  • Minutes: "Quotient(X,60)"
  • Seconds: "Mod(X,60)"

If X was 500, each metric would calculate as follows:
Minutes: Quotient(500,60) = 8
Seconds: Mod(500,60)=20
Based on the above, the values returned are 8 minutes and 20 seconds..
 
OUTPUT 2:
To return hours, minutes, and seconds, create three metrics defined as follows:

  • Hours: "Quotient(X,3600)"
  • Minutes: "Quotient(Mod(X,3600),60)"
  • Seconds: "Mod(Mod(X,3600),60)"

If X was 3715, each metric would calculate as follows:
Hours: Quotient(3715,3600)=1
Minutes: Quotient(Mod(X,3600),60)=Quotient(115,60)=1
Seconds: Mod(Mod(X,3600),60)=Mod(115,60)=55
Based on the above, the values returned are 1 hour, 1 minute and 55 seconds.
 
OUTPUT 3:
To return days, hours, minutes, and seconds, create four metrics defined as follows:

  • Days: "Quotient(X,86400)"
  • Hours: "Quotient(Mod(X,86400),3600)"
  • Minutes: "Quotient(Mod(Mod(X,86400),3600),60)"
  • Seconds: "Mod(Mod(Mod(X,86400),3600),60)" 

If X was 100000, each metric would calculate as follows:
Days: Quotient(100000,86400)=1
Hours: Quotient(Mod(100000,86400),3600)=Quotient(13600,3600)=3
Minutes: Quotient(Mod(Mod(100000,86400),3600),60)=Quotient(Mod(13600,3600),60)=Quotient(2800,60)=46
Seconds: Mod(Mod(Mod(X,86400),3600),60)=Mod(Mod(13600,3600),60)=Mod(2800,60)=40
Based on the above, the values returned are 1 day, 3 hours, 46 minutes and 40 seconds.
 
FORMATTING CONSIDERATIONS:
Each metric on its own will return the appropriate values for the components of the timestamp, but one may want to merge the calculation into one metric column. This can be done using the Concat function in Strategy, which is defined as follows:
Concat(Argument1, Argument2, ..., ArgumentN)
Each argument represents either a fact, metric, column, or string value.
The Concat function can be used in each output scenario as follows:

  • Concat(Minutes,":",Seconds)
  • Concat(Hours,":",Minutes,":",Seconds)
  • Concat(Days,":",Hours,":",Minutes,":",Seconds)

This returned correct results that could be subtotaled and could dynamically aggregate by replacing Days, Hours, Minutes, and Seconds with their appropriate formulas listed in the output sections above.
For each of the scenarios outlined above (500 seconds, 3715 seconds, and 100000 seconds), the concatenation function will behave as follows:

  • 500 seconds returns "8:20"
  • 3715 seconds returns "1:1:55"
  • 100000 seconds returns "1:3:46:40"

This is not ideal as it is common to see a 0 in front of these values if the values are less than 9. In order to display the 0 in front of each individual metric, the Case function must be used with another Concat function. The definition for Case is as follows:
Case(Condition1, ReturnValue1, Condition2, ReturnValue2, ..., DefaultValue)
Each condition is a qualification composed of metrics, comparison and logical operators, and constants. It's corresponding ReturnValue will return the value if the condition is satisfiedd. Otherwise, the default value will be returned.
To use this to display values correctly, the following syntax will be needed:

  • Case((Days<10),Concat("0", Days), Days)
  • Case((Hours<10),Concat("0", Hours), Hours)
  • Case((Minutes<10), Concat("0", Minutes), Minutes)
  • Case((Seconds<10), Concat("0", Seconds), Seconds)

Replacing each corresponding metric with the above expressions will return the following:

  • 500 seconds returns "08:20"
  • 3715 seconds returns "01:01:55"
  • 100000 seconds returns "01:03:46:40"

The metrics should be built as follows for each of the three output scenarios expanded in terms of Days, Hours, Minutes, and Seconds:

  • Concat(Case((Minutes<10), Concat("0", Minutes), Minutes),":",Case((Seconds<10), Concat("0", Seconds), Seconds))
  • Concat(Case((Hours<10),Concat("0", Hours), Hours),":",Case((Minutes<10), Concat("0", Minutes), Minutes),":",Case((Seconds<10), Concat("0", Seconds), Seconds))
  • Concat(Case((Days<10),Concat("0", Days), Days),":",Case((Hours<10),Concat("0", Hours), Hours),":",Case((Minutes<10), Concat("0", Minutes), Minutes),":",Case((Seconds<10), Concat("0", Seconds), Seconds))

The metrics in terms of X should be built as follows:

  • Concat(Case((Quotient(X, 60) < 10), Concat("0", Quotient(X, 60)), Quotient(X, 60)), ":", Case((Mod(X, 60) < 10), Concat("0", Mod(X, 60)), Mod(X, 60)))
  • Concat(Case((Quotient(X, 3600) < 10), Concat("0", Quotient(X, 3600)), Quotient(X, 3600)), ":", Case((Quotient(Mod(X, 3600), 60) < 10), Concat("0", Quotient(Mod(X, 3600), 60)), Quotient(Mod(X, 3600), 60)), ":", Case((Mod(Mod(X, 3600), 60) < 10), Concat("0", Mod(Mod(X, 3600), 60)), Mod(Mod(X, 3600), 60)))
  • Concat(Case((Quotient(X, 86400) < 10), Concat("0", Quotient(X, 86400)), Quotient(X, 86400)), ":", Case((Quotient(Mod(X, 86400), 3600) < 10), Concat("0", Quotient(Mod(X, 86400), 3600)), Quotient(Mod(X, 86400), 3600)), ":", Case((Quotient(Mod(Mod(X, 86400), 3600), 60) < 10), Concat("0", Quotient(Mod(Mod(X, 86400), 3600), 60)), Quotient(Mod(Mod(X, 86400), 3600), 60)), ":", Case((Mod(Mod(Mod(X, 86400), 3600), 60) < 10), Concat("0", Mod(Mod(Mod(X, 86400), 3600), 60)), Mod(Mod(Mod(X, 86400), 3600), 60)))

From here, every instance of X can be replaced with the appropriate metric name by using a text editor and using Find/Replace. Include square brackets around the metric name if it is more than 1 word.
 
SUBTOTALING AND DYNAMIC AGGREGATION:
When using any of the metrics (in terms of X) above and ensure that they are smart metrics, the Analytical Engine will perform subtotaling on the base metric X before performing the concatenation, so all out-of-the-box subtotals will work.
Dynamic aggregation will also apply as well in this scenario, since the subtotal is being calculated on the raw data before any other calculation by default.
An example is provided to demonstrate both. A metric has been defined as the sum of a time fact at the report level:

ka04W000000ObIuQAK_0EM440000002CMp.png

 
Included in a report with the Region attribute will look like this:

ka04W000000ObIuQAK_0EM440000002CMi.png

 
After this, a derived metric is added to the report that is defined as follows (this is using the third output scenario):

ka04W000000ObIuQAK_0EM440000002CMd.png

 
Here is how the formula calculates these values:

ka04W000000ObIuQAK_0EM440000002CMg.png

 
Using a conversion tool available on the Internet, it is easy to verify that 851921 seconds is 9 days, 20 hours, 38 minutes, and 41 seconds.

ka04W000000ObIuQAK_0EM440000002CMj.png

 
For purposes of demonstrating dynamic aggregation, Country is added to the report:

ka04W000000ObIuQAK_0EM440000002CMl.png

 
If Region is removed from the template, the values will dynamically aggregate:

ka04W000000ObIuQAK_0EM440000002CMf.png

Note: The timestamp metric can be either a derived metric or a smart standalone metric. 
 
WARNING:
The third-party product(s) discussed in this article is manufactured by vendors independent of Strategy. Strategy makes no warranty, express, implied or otherwise, regarding this product, including its performance or reliability.


Comment

0 comments

Details

Knowledge Article

Published:

April 17, 2017

Last Updated:

April 17, 2017