Formula Question - Sum like items in a list

Hi- I have a multivalue field with the following items:

Office1~2000

Office1~1000

Office2~1000

Office1~500

Office2~300

Using formula language I would like to sum the values for Office1 and Office2, with a result like this:

Office1~3500

Office2~1300

Anyone have any ideas?

Subject: Formula Question - Sum like items in a list

totalTags := @Unique(@Left(Fieldname;“~”)) + “~”;

That returns all of the unique “tags”

finalList := “”;

That’s a seed list to build on in the @For

@For(i := 1; i < @Elements(totalTags); i := i + 1;

finalList := finalList : (totalTags[i] + @Sum(@ToNumber(@Trim(@Right(FieldName;totalTags[i])))));

Now trim the initial “” from the list

@Trim(finalList)

Subject: RE: Formula Question - Sum like items in a list

Stan- Thanks a lot this works very well; however, it strips out the items in the list that do not have any duplicate entries. Any way to keep those intact? For example:

Office1~200

Office2~100

Office1~100

It sums Office1 but strips out Office2 from the list. I would like the result to be:

Office1~300

Office2~100

Thanks.

Subject: RE: Formula Question - Sum like items in a list

Sorry – it’s the i < @Elements; it should be i <= @Elements. It’s only a coincidence that it was the single value that was dropped. Here’s a more robust version:

@If(Fieldname=“”;@Return(“”);“”);

totalTags := @Unique(@Left(Fieldname;“~”)) + “~”;

finalList := “”;

@For(i := 1; i <= @Elements(totalTags); i := i + 1;

finalList := finalList : (totalTags[i] + @Text(@Sum(@ToNumber(@Trim(@Right(Fieldname;totalTags[i])))))));

@Trim(finalList)

Subject: RE: Formula Question - Sum like items in a list

Thank you! Worked perfectly. You taught me a new way to use @Right.

Subject: Formula Question - Sum like items in a list

If it’s only those “keys” (Office1 and Office2), it’s quite simple:sum1:=@Sum(@TextToNumber(@Right(field; “Office1~”)));

sum2:=@Sum(@TextToNumber(@Right(field; “Office2~”)));

but if the “keys” are a bit dynamic, it gets a bit tricky. If you know that the first part (“Office”) are static, you could loop with a @For loop from 1 to 999 and do the formula above.

If the keys are completely dynamic, you have to do some more looping, probably two nested loops. One for all keys, and one for elements in the list, and one for all found keys…

  • Johan

johan@kanngard.net

http://dev.kanngard.net