Skip to main content
Solved

Running total

  • December 18, 2019
  • 6 replies
  • 113 views

Hi there,

 

I have two attributes (i.e. date and a field called wastage). Wastage is numeric field. I want to calculate the Running total of field wastage. I am using statisticsCalculator. I have attached the configuration of the statisticcalculator. Date is organised w.r.t. date. The problem is that calculated running total is the same for all dates (i.e. sum of all values of wastage). I mean it just calculate sum, its not the cumulative sum. Could anyone guide me where am I making the mistake?

 

Best answer by bwn

Coincidentally, you might want to take a look at the post/blog from @mark2atsafe in which he talks about instead using AttrbuteCreator in "Adjacent Feature" mode.

https://knowledge.safe.com/questions/104425/question-of-the-week-variables-adjacent-features-a.html

What you could do is run the features through a Sorter (by Date).

Then run these through an AttributeCreator, but with Adjacent Features turned On. Number of Prior Features = 1.

Then you can use an Attribute Value in AttributeCreator that uses a formula like:

AccumulatedWastage@Evaluate(@Value(feature[-1].AccumulatedWastage)+@Value(Wastage))

The only bizarre thing I found in FME2018 was that I needed to create "AccumulatedWastage" as an Attribute with a Default value first in a preceding AttributeCreator before I could run an AttributeCreator on this in Adajcent Feature Mode that updated its value with the Cumulative amount.

View original
Did this help you find an answer to your question?

6 replies

bwn
Evangelist
Forum|alt.badge.img+26
  • Evangelist
  • Best Answer
  • December 18, 2019

Coincidentally, you might want to take a look at the post/blog from @mark2atsafe in which he talks about instead using AttrbuteCreator in "Adjacent Feature" mode.

https://knowledge.safe.com/questions/104425/question-of-the-week-variables-adjacent-features-a.html

What you could do is run the features through a Sorter (by Date).

Then run these through an AttributeCreator, but with Adjacent Features turned On. Number of Prior Features = 1.

Then you can use an Attribute Value in AttributeCreator that uses a formula like:

AccumulatedWastage@Evaluate(@Value(feature[-1].AccumulatedWastage)+@Value(Wastage))

The only bizarre thing I found in FME2018 was that I needed to create "AccumulatedWastage" as an Attribute with a Default value first in a preceding AttributeCreator before I could run an AttributeCreator on this in Adajcent Feature Mode that updated its value with the Cumulative amount.


redgeographics
Celebrity
Forum|alt.badge.img+49

Try the Cumulative output port of the StatisticsCalculator, it will keep your running total per feature:


Forum|alt.badge.img+2
  • December 18, 2019
bwn wrote:

Coincidentally, you might want to take a look at the post/blog from @mark2atsafe in which he talks about instead using AttrbuteCreator in "Adjacent Feature" mode.

https://knowledge.safe.com/questions/104425/question-of-the-week-variables-adjacent-features-a.html

What you could do is run the features through a Sorter (by Date).

Then run these through an AttributeCreator, but with Adjacent Features turned On. Number of Prior Features = 1.

Then you can use an Attribute Value in AttributeCreator that uses a formula like:

AccumulatedWastage@Evaluate(@Value(feature[-1].AccumulatedWastage)+@Value(Wastage))

The only bizarre thing I found in FME2018 was that I needed to create "AccumulatedWastage" as an Attribute with a Default value first in a preceding AttributeCreator before I could run an AttributeCreator on this in Adajcent Feature Mode that updated its value with the Cumulative amount.

This approach will be a lot faster than using the StatisticsCalculator


mark2atsafe
Safer
Forum|alt.badge.img+44
  • Safer
  • December 18, 2019
bwn wrote:

Coincidentally, you might want to take a look at the post/blog from @mark2atsafe in which he talks about instead using AttrbuteCreator in "Adjacent Feature" mode.

https://knowledge.safe.com/questions/104425/question-of-the-week-variables-adjacent-features-a.html

What you could do is run the features through a Sorter (by Date).

Then run these through an AttributeCreator, but with Adjacent Features turned On. Number of Prior Features = 1.

Then you can use an Attribute Value in AttributeCreator that uses a formula like:

AccumulatedWastage@Evaluate(@Value(feature[-1].AccumulatedWastage)+@Value(Wastage))

The only bizarre thing I found in FME2018 was that I needed to create "AccumulatedWastage" as an Attribute with a Default value first in a preceding AttributeCreator before I could run an AttributeCreator on this in Adajcent Feature Mode that updated its value with the Cumulative amount.

There's an exercise demonstrating this in the current FME Desktop (advanced) training course: https://s3.amazonaws.com/gitbook/Desktop-Advanced-2019/DesktopAdvanced1Attributes/1.Exercise3.html

I think the part about creating the attribute might be resolved by using the Substitute Missing Values parameter (basically the first feature can't have feature[-1] because it doesn't exist, so it's necessary to have a substitute value).


mark2atsafe
Safer
Forum|alt.badge.img+44
  • Safer
  • December 18, 2019
redgeographics wrote:

Try the Cumulative output port of the StatisticsCalculator, it will keep your running total per feature:

Just be aware that there was a problem with this in various 2019.1 builds, where the cumulative port wasn't working. As long as you have the latest FME installed, you will be fine.


bwn
Evangelist
Forum|alt.badge.img+26
  • Evangelist
  • January 6, 2020
mark2atsafe wrote:

There's an exercise demonstrating this in the current FME Desktop (advanced) training course: https://s3.amazonaws.com/gitbook/Desktop-Advanced-2019/DesktopAdvanced1Attributes/1.Exercise3.html

I think the part about creating the attribute might be resolved by using the Substitute Missing Values parameter (basically the first feature can't have feature[-1] because it doesn't exist, so it's necessary to have a substitute value).

@mark2atsafe Finally did a bit more testing on this with FME2018.1.1.2 and there does indeed appear to be a small bug here. When enabling "Adjacent Features" mode on AttributeCreator, Workbench will say that the Transformer is "Invalid" if that Attribute has not been already created by a previous transformer ie. In Adjacent Features mode, Workbench will not validate if trying to simultaneously create and calculate the Attribute within the one AttributeCreator transformer. It seems to get confused with trying to validate the schema in thinking you can't update an Attribute that doesn't already exist from upstream transformer processes.

It appears to be a Workbench schema validation tester bug rather than a bug with the actual Transformer. If you blow through the warning of "running the workspace with invalid transformers" the AttributeCreator transformer in Adjacent Features mode will work just fine and both create and calculate the Attribute within the one Transformer!

Not sure if the bug has continued through to 2019+

You are right in that you do have to use a Substitute Default value to give a starting value for Feature at "Index -1", and the transformer is clever enough to create a temporary "dummy" feature before the first real feature with this default value, and then this starting value will get used for the real first Feature at Index 0.

 


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings