Running Total Function in GoodData Platform (Sum vs Runsum)

  • 1 September 2021
  • 4 replies
  • 47 views

Userlevel 1

Running Total Functions

Running Total Functions allow you to keep track of a metric from the beginning of a period until now or calculate the statistics in-betweens rows, such as total revenue year-to-date or sum of revenue between the last 2 periods and now. In the GoodData Platform, you may use this function to calculate sum, average, minimum, maximum, variance, and standard deviation with the following syntax:

 

Running Total Function

Syntax

Sum

runsum()

Average

runavg()

Minimum

runmin()

Maximum

runmax()

Variance

runvar()

Standard Deviation

runstdev()

Variance (Uncorrected)

runvarp()

Standard Deviation (Uncorrected)

runstdevp()

Table 1: List of Running Total Functions available in GoodData Platform

 

The Running Total Functions are a great tool to calculate moving averages or accumulated summation. In this article, we will explore how to use the running total function on defining metrics in the GoodData Platform with the logical data model (LDM) is below:

ffIE7bDHiVvShA3ueY1oIaoqaR9ZQQQ_-m0yXDKvneWJZ3uaKsNNfI1PEYOnoBV0XEeLIHBcSQp37RDyoxttj9kHBPjGGliOrw8iogdLgqXEhvV1c7shGMBYHKWG0Mh3bjp_IORf=s0

Diagram 1: LDM of Sales

 

Sum vs.Runsum

What makes runsum different from sum is that runsum accumulates the summation made in the previous period or dimension. To demonstrate the difference, let's define 2 metrics using both syntaxes to calculate the revenue:

 

Select SUM(Price*Quantity) where Order Status = Delivered - Syntax 1

Select RUNSUM(Price*Quantity) where Order Status = Delivered - Syntax 2


O7V-2zMCeT9J-kVY4wgj-oo39h3vZZyMRfcL7XJ3XxNuJL2unM6dQt2PFfgkqxptBG3jhGWUc7zPNCb63pMZc-J_Hb7JCLn33yuiGZlrzjeBWdIvvGdBV-18SdLfAp5z1bxmRJqm=s0
Diagram 2:
Revenue (Syntax 1) in Metric Editor

 

kA3xq2MjC7wDgZd09VvjAs0UkyTKDiFIAosVgYuKXiWzP70L9ZjKzUA4lqCjpjCga79rbNSm45cGikkaUGUf2ICt_0U_-M2-LZE20QwuDiNwqjqWqK61tdo6hJKtT37HhOste-3F=s0

Diagram 3: Total Revenue since First Month (Syntax 2) in Metric Editor
 

I have named the metrics with Revenue and Total Revenue since First Month for Syntax 1 and Syntax 2, respectively. Let’s put these metrics in a table in Analytical Designer segmented by Date and observe the difference between Syntax 1 and Syntax 2:

r-kzle_P_kFZDuTAcmAXDlasuTY3VHjsPnJg2UflIoxKgxCn9eOfej-hceij4wSQrySUDBeiD4Kny09_goMRnxHR5I6DlIt9GWR2WT84X9i-NcVq3E1lmcTCqR-Nt66e91UN7LGo=s0

Diagram 3: Sum vs. Runsum Sliced by Year

Once the data is segmented, we can see the Total Revenue since First Month accumulates the previous month's revenue. In contrast, Revenue only displays the revenue of the respective month/year. We did not specify the timeframe for Syntax 2 to accumulate the revenue. Therefore, Total Revenue since First Month accumulates the revenue from the first available period, Oct 2017. 

And, of course, it works the same with other statistical metrics. If you are interested in minimum or maximum revenue, replace runsum with runmin or runmax.

 

Running Total Within a Specific Timeframe

A limitation of Syntax 2 is that the metric includes all historical data; it may be more useful to calculate the given metric within a specific timeframe for reporting purposes. For example, I think the user may be more interested in daily revenue and Year-to-date accumulated daily revenue (Summation of revenue from the beginning of the year to the current date); it means we have to modify Syntax 2 only to sum the daily revenue within each year. To do this, we need to specify the timeframe using the keyword within() the syntax. The keyword only accepts date attributes. Syntax 3 below is the modified syntax for year-to-date revenue:

Select RUNSUM(Price*Quantity) within(Year(Date)) where Order Status = Delivered - Syntax 3

 

4AYRhY-sFvNhGxzWrSnnRXs1PFMomsvF_e_SwUf3xwDElaaszsHTzztPNWTjzNrw3XvQJ2qA-NREzu52KeDrMVqK0LWGqR8itKy8BBH1p8aVvcvuiVEs-oIs_Eqi8dzwLHqqB5-o=s0

Diagram 4: Total Revenue YTD (Syntax 3) in Metric Editor
 

RF88EdcQ5HTyIa6_g4f_YufNyU_vZsNystOlIyL7m7mst90_HmFmudEowLXiWxE_9nqlBS0IDR5Lf6pDbL-ory6pN2jxwXshUz2gZR0RDjzXagisx3rVOodjmTw-2iMwGO3GJrXw=s0

Diagram 5: Revenue and Total Revenue YTD

 

As we expected, the metrics only accumulate the revenue within each year, resetting the calculation in January 2018. Total Revenue YTD can be segmented in other date granularity lower than Year, like Quarter: 

XeiF7BHaLJHfjjijQBQT26J5fgcPVTUQS5FRRg5g8EdkoNvKct90k9kDWg7BguGz9tHQ0JsxS1mpBZ_KbCn3_IoNir1nO_Z63s9Lz8dBh7aCHRLidUrQ26-iLjzXSro0hDDtFCU9=s0

Diagram 6: Revenue and Total Revenue YTD sliced by Quarter

However, this functionality does not work when segmented by the same date granularity (like year in our example) or any non-chronicle date granularity (Like month of year or day of month).

 

Rolling Window

GoodData.CN offers the ability to calculate statistics within a window frame, such as moving averages. The format of the rolling window is Select runsum(<attribute/fact/metric>) Rows between <Specific row(s)> and <Specific row(s)>. You may place N preceding, N following, current row to represent N previous rows, N following rows, and current row, respectively. For example, if I would like to have 5 days moving average of revenue (Take an average of the last 4 days and today), I may have the following MAQL:

 

Select RUNAVG(Revenue) Rows Between 4 Preceding and Current Row - Syntax 4

 

DGbCL9_Ves4BG0-tqUmw1nOlADVtbwc6H49pqr0S2asrTBDk4E8d4oxN2xcHMmoRhtekHlkUOIKdEa6iEfYtPJDSnLsNGH_HzVPBkXBAJblM2qrp98zQ5a-dUq8JhYEAm-OLcB8S=s0

Diagram 7: Revenue 5-Days Moving Average (Syntax 4) in Metric Editor
 

This MAQL query takes the average of revenue defined in Syntax 1 for the last 5 days (Including today). Here is what a line chart will look like if we limit the data to only this year:

qoCrkyRUbxiHdjr3EYaoHxC5on_MCKMQCIQdUfWIqGSR5AkRnp_zLwEaX3Td6b1ld5rmrbkbKDi3_j2B6ZmmvIVYR4R904VuoHNjnvSTCKpyy2-_M-oxUfWEVOHn84PMaj76B3Jx=s0

Diagram 8: Revenue 5 Days Moving Average

 

The blue line represents the actual daily revenue, while the orange line represents the 5 Days moving average revenue. As we can see in Diagram 4, the orange line is smoothened and displaying the 5 days trend of revenue. You may also combine the within keyword to limit the time frame within a specific date granularity. If we are interested in obtaining the 5 days moving average using only the same year data, we may modify Syntax 4 to the following:

Select RUNAVG(Revenue) within(Year(Date)) Rows Between 4 Preceding and Current Row - Syntax 5

 

Conclusion

Running Total Function is a great way to calculate a metric across a specific time frame. We have explored the difference between sum and runsum and how flexible it is to calculate your metrics with different time frames. 

 

Syntax format:

Select RUN___(<metric>) WITHIN (<Date Granularity>}) Rows Between _____ and ____ where <attribute/metric/fact> = <some condition>


 

(Tip) Note on Calculating Moving Average

Going back to our example on calculating Revenue 5 days moving average, you may realize the flaw of the MAQL query on Syntax 5 is that the entries of the first 4 days do not fully take 5 days of average (ie, the moving average for the entry on Jan 1, 2020 does is the same as the revenue), like below:

ZX-NxMhxgO23wBrlVIXBJ2q3jF-gHEENDl_LBX0V0e80TnJPLcQ6mQ86uDini5e3l64VRvbrJRYGUNVlvm9QtEZG1rBJSxmB_F5HqJq-7QuJ06m7OLucbR55ILajWbw6A9i1WEWW=s0

Diagram 9: The flaw of Syntax 5

 

It happens because the entries needed are filtered out. In order to fix, you need to include “WITH PF Except Date” in Syntax 5, so that the metric is able to include the entries from the previous year in the calculation, with the following Syntax:

Select RUNAVG(Revenue) Rows Between 4 Preceding and Current Row with PF except Date(Date)- Syntax 6



NdzSNgRGkRmw8G5O0Z7WKPHG1tDwUr2wdTrLb8sUhuEjVlZ9OTFQJwb_UiPBnHJZ_9mAwoPJswYkQnAaibHErpi2Bhy-cPjEWYJKeM9ZjTeiDnaGRjNxhlgOeRwxpqRxg3HaVt8O=s0

Diagram 10: Syntax 6 in Metric Editor

 

Once you have utilized the new metrics, it will look like this:IMER1mHVi7bkczbBxk1bduvBo4KW_1raXlVdBBIog7RkWAe1QUlUblKSOWzjauBrI9FcR4IBtvEVCCII2EY90uaBF-5KqB_ChC-CnI44SlXWP4MsqPbiF6h3hl-zQggu6GyKNbFN=s0

Diagram 11: The 5-days Moving Average with the Correct Calculation

 

Once you have created this insight, be sure to test whether this is the desired metric you would like to display on a dashboard and adjust the filtering dependence between this insight and the dashboard.

 

Reference
Documentation of Running Total Function:
https://help.gooddata.com/doc/enterprise/en/dashboards-and-insights/maql-analytical-query-language/maql-expression-reference/granularity-keywords/running-total-functions/running-totals-for-core-functions

https://help.gooddata.com/doc/enterprise/en/dashboards-and-insights/maql-analytical-query-language/maql-expression-reference/granularity-keywords/running-total-functions


 


4 replies

Hey Jacques and thank you for the article!

I have a challenge to calculate a TTM Revenue (trailing twelve months). I created a metric with the following syntax:
SELECT RUNSUM (select Inv. Amount (Converted) Where Month/Year (Worked Date) BETWEEN THIS - 11 AND THIS)

But when I'm adding a report into dashboard, GD cuts the data and displays only month (depends on the Data filter)

Basically, if I take your Diagram 5: Revenue and Total Revenue YTD, as an example, I need the sum Revenue for the period (11-2017 - 10-2018). And by using a Data filter (Month/Year (Worked Date)) get different values for the last 12 months (incl. selected month).

Please advise. Thanks, 
/Yurii B

Userlevel 1

Hello Yurii,

Thank you for reaching to me and I am happy to help!

When using runsum, we should not use where clause to limit the timeframe. If you want to calculate TTM Revenue in my example, the easiest way to do is declare the following metric and put it in an insight and sliced by Month/Year:

select runsum(revenue) rows between 11 preceding and current row with pf except Date(Order Date)

This syntax allows you to add 12 revenue entries (Or 12 months) when you slice by Month/Year. But you must have the insight sliced by Month/Year in order to work. The “pf except Date” allows the metric to work even if you have data filter in the insight.

Please let me know if it works on your side.

Jacques

Thanks for a quick reply but it doesn't work on my end. And I can't use the pf except option, as I need to roll data using this attribute on board to get different TTM figures 🙂 The row data coming from the stores on daily basis. In this case, I assume the Row in this syntax will mean each Day, and not Month as I need. 

I did a very simple formula in the past, which just add up twelve month in row. But is there a smart way of doing the same? :thinking:

SELECT ( (SELECT REVENUE) + (SELECT REVENUE FOR Previous (Month (Worked Date))) + (SELECT REVENUE FOR Previous (Month (Worked Date), 2)) + (SELECT REVENUE FOR Previous (Month (Worked Date), 3)) + …

/Yurii B

Userlevel 1

Hello Yurii,

I have sent you a private message to follow up your question but I have not received your reply yet. Sorry for the late follow up. I don’t think MAQL allows you to sum/average with a rolling time frame, but I think my suggested MAQL query should work unless you do not wish to segment by month/year. I believe there is misunderstanding from my side, I strongly recommend you to join our community slack channel and there are specialists timely replied any questions about GoodData products related general and technical questions with the following link: gooddataconnect.slack.com

Jacques

Reply