Filemaker - Summing a field based on another field - filemaker

In Filemaker Pro 12, I am trying to write a formula for a calculation field that will sum a field in a related table based on another field in that same related table. The normal Filemaker sum equation would look like this:
Sum (Assets::Asset Quantity)
However, I need to specify that only quantities that are related to a field named Asset Type with a value of "Building" will be used to filter the values in Asset Quantity that will be used in the sum.

There are a couple of ways that you could do this:
A new Calculated field
First, you could add a new Calculation field to your Assets table called, say, Building Quantity, with a Calculated Value of:
If (Asset Type = "Building" ; Asset Quantity ; 0)
And then you can use the sum of this new Building Quantity just like you were using Sum(Assets::Asset Quantity) before.
A new relationship
Second, you could add a new Calculated field to your main table with the value always equal to "Building" and then add a new table occurrence of the Assets table. We'll call it "BuildingAssets" and set the relationship so that your IDs match and also your new "Building" field matches the Asset Type
Summary ID \____________/ BuildingAssets::Summary ID
BuildingText / \ BuildingAssets::Asset Type
Then you will use
Sum (BuildingAssets::Asset Quantity)
instead of Sum (Assets::Asset Quantity) so that you only pull the Building types through.
ExecuteSQL
Finally, FileMaker 12 introduced the ExecuteSQL step. This may be the most elegant way to do the above because it doesn't involve changing any schema. The statement would be somethign like:
SELECT
SUM (Asset Quantity)
FROM
Assets
WHERE
Summary ID = ID AND
Asset Type = Building
For more information check out FileMaker's page: http://www.filemaker.com/12help/html/func_ref3.33.6.html
Also check out the FileMaker SQL Sugar ("#") Module for help building queries: http://www.modularfilemaker.org/2013/03/filemaker-sql-sugar/

Related

Dynamics CRM + Plugin Code to Calculate Sum of fields across records and update in the another record

I have the below requirement to be implemented in a plugin code on an Entity say 'Entity A'-
Below is the data in 'Entity A'
Record 1 with field values
Price = 100
Quantity = 4
Record 2 with field values
Price = 200
Quantity = 2
I need to add the values of the fields and update it in a new record. Example shown below -
Record 3
Price = 100 + 200 = 300
Quantity = 4 + 2 = 6
Entity A has a button named "Perform Addition" and once clicked this will trigger the plugin code.
I need some ideas/pseudocode on how can i implement this. The example explained above is just a simpler version of my entity. In Real the entity has more than 60 fields on it , and for each of the fields i need to perform the sum and update in the third one. Also the number of records on which the addition would be performed can be beyond 2.
Hence more the no of records, more i will have to loop through each records and perform sum, I would like to know if there are simpler and better ways to writing this logic.
Just need guidance on how the logic should be written.
Any help would be appreciated.
Solution :
Tried the below code and it worked as suggested by the answer -
AttributeList is the list of fields i need to perform sum on. All fields are decimal
Entity EntityA = new EntityA();
EntityA.Id = new Guid({"Guid String"});
var sourceEntityDataList = service.RetrieveMultiple(new FetchExpression(fetchXml)).Entities;
foreach (var value in AttributeList)
{
EntityA[value]= sourceEntityDataList.Sum(e => e.Contains(value) ? e.GetAttributeValue<Decimal>(value) : 0);
}
service.Update(EntityA);
I recently did something similar for a client. We looked into using rollup fields, but they want the results more quickly than 12 hours.
The records containing the values that we're summing are the child records and the record containing the results is the parent.
The way it works is:
From the plugin's target entity, get the parent id.
Retrieve the parent.
Retrieve its children (via LINQ, FetchXML, or a QueryExpression).
Sum the values from the children. Since you have a lot of fields to sum, you could create a list of the field names, then use a method like this (assuming they're all Money fields)
private decimal sum(string field) => Records.Sum(b => b.GetAttributeValue<Money>(field)?.Value ?? 0);
If the fields are different types you could also create different sum methods like sumMoney, sumInt, etc. You'd then need to separate the fields by type and pass them to their appropriate method.
Populate the results on the parent Entity.
Update the parent record in Dynamics.
If the information is not that time sensitive maybe look into the out-of-box rollup fields.

Aggregate field in Tableau tool

I would like to receive your help in order to create an aggregate field in the Tableau tool.
I have a data source. The data source contains a few records (tuples/lines), and each record contains some attributes. I.e. a numeric value, a unique attribute (Key) and string.
For example, the first two rows share the same key (ID=A). This entity contains 2 records, the numeric value for each record is different (1 and 5).
I would like to create a new field. This new field should contain the minimum numeric value for each unique ID (entry).
In order to create this new field, I created a calculated field. The field’s formula is- min(NumericField).
The problem occurred when I was trying to use this calculated aggregated field aggregate in order to create chats. In this case, the aggregation is no longer performed on my key field.
For example, I would like to calculate the average for each category (The avg should be calculated for the aggregated new field)
Do you have any idea in order to resolve the mentioned problem?
Thank you! ☺️
An example
Try using LOD as below:
{fixed [id]: avg([newcalculatedfield])}
Let me know if you need more help. thanks!

Google refine cross-reference between row and column

I'm not sure if this can be achieved in Google Refine at all. But basically, I have data like this.
The first table is the table of all the users. The second table show all the friends. However, in the second table in "friends" column not all the id exists in the first table which I want to get rid of. So, how can I search each id in friends column in the second table and get rid of the id that doesn't exists in the table 1?
Put the two tables in different projects (we'll call them Table1 and Table2).
In Table2 on on the friends column:
use "split multi-valued cells" to get each value on a separate row
convert the visitors column to numbers (or conversely user_id in Table1 to string)
use "add a new column based on this column" with the expression cross(cell,'Table1','user_id').length()
This will return 0 if there's no match, 1 if there's a match or N>1 if there are duplicates in Table1
If you want the data back in the original format, set up a facet to filter on the validity column, blank out all the bad values and then use "join multi-valued cells" to reverse the split operation you did up front.
I fixed some caching bugs with cross() for OpenRefine 2.6, so if the cross doesn't work, try stopping and restarting the Refine server.

FileMaker - How to have distinct values listed on-screen?

I would like to have the distinct values of a repeating field listed in another field (in browse mode). The case is as follows:
I have a field that contains country names. The country names in this field may repeat themselves, thus when using the "List" function I get something like "France, France, France, Germany, Germany, Hungary".
How can I create a field that lists all the values from my country field, but has it grouped as "France, Germany, Hungary"?
In the case I could directly use a SQL query to interfere with the FileMaker databse I would use the GROUP BY statement.
To make a summary of all the values across every record, do as follows:
Make a new value list labeled 'Countries' (File Menu > Manage > Value Lists)
Make the value list 'Use Values From Field' and specify your repeating field
Create a new Calculation field, 'Listed Countries'
Set the calculation to type 'Text' and with the following code:
ValueListItems ( Get(FileName) ; "Countries" )
If you'd like to find the value for only the current record:
Make a new Table Occurrence, 'NewTO' of the same base table and link the two records by a unique index.
Change the 'Countries' value list so that it obtains values from 'NewTO' and your repeating field.
Select 'Only include related values starting from' and select your original Table Occurrence
If you'd like the list to update as the repeating field value changes, make certain that you Do Not Store Calculation Results for the field.

Create a new FileMaker layout showing unique records based on one field and a count for each

I have a table like this:
Application,Program,UsedObject
It can have data like this:
A,P1,ZZ
A,P1,BB
A,P2,CC
B,F1,KK
I'd like to create a layout to show:
Application,# of Programs
A,2
B,1
The point is to count the distinct programs.
For the life of me I can't make this work in FileMaker. I've created a summary field to count programs resetting after each group, but because it doesn't eliminate the duplicate programs I get:
A,3
B,1
Any help much appreciated.
Create a a summary field as:
cntApplicaiton = Count of Application
Do this by going into define fields, create a field called cntApplication, type summary. In the options dialogue make the summary field a count on application
Now create a new layout with a subsummary part and nobody. The subsummary should be sorted on Application. Put the Application and cntApplication fields in subsummary. If you enter browse mode and sort by Application you ought to get the data you want.
You can also create a calc field with the formula
GetSummary(cntApplication; Application)
This will allow you to use the total number of Applications with in a record
Since I also generate the data in this form, the solution I've adopted is to fill two tables in FileMaker. One provides the summary view, the other the detailed view.
I think that your problem is down to dupliate records and an inadequate key.
Create a text field called "App_Prog". In the options box set it to an auto-enter calc, unchecking the 'Do not replace...' option, and use the following calc:
Application & "_" & Program
Now create a self join to the table using App_Prog as the field on both sides, and call this 'MatchingApps'.
Now, create (if you don't alread have one) a unique serial number field, 'Counter' say, and make sure that you enter a value in each record. (Find all, click in the field, and use serial number option in'Replace Field Contents...')
Now add a new calc field - Is_Duplicate with the following calc...
If (Counter = MatchingApps::Counter; "Master Record" ; "Duplicate")
Finally, find all, click in the 'Application field, and use 'Replace Field Contents...' with a calculation to force the auto-enter calc for 'App_Prog' to come up with a value.
Where does this get you? You should now have a set of records that are marker either "Master Record" or "Duplicate". Do a find on "Master Record", and then you can perform your summary (by Application) to do a count of distinct application-program pairs.
If you have access to custom functions (you need FileMaker Pro Advanced), I'd do it like this:
Add the RemoveDuplicates function as found here (this is a recursive function that takes a list of strings and returns a list of unique values).
In the relationships graph, add another occurrence of your table and add an Application = Application relationship.
Create a calculated field in the table with the calculation looking something like this:
ValueCount(RemoveDuplicates(List(TABLE2::Program)))
You'll find that each record will contain the number of distinct programs for the given application. Showing a summary for each application should be relatively trivial from here.
I think the best way to do this is to create a separate applications table. So as you've given the data, it would have two records, one for A and one for B.
So, with the addition of an Applications table and your existing table, which I'll call Objects, create a relationship from Applications to Objects (with a table occurrence called ObjectsParent) based on the ApplicationName as the match field. Create a self join relationship between Objects and itself with both Application and Program as the match fields. I'll call one of the "table occurrences" ObjectsParent and the other ObjectsChildren. Make sure that there's a primary key field in Objects that is set to auto-enter a serial number or some other method to ensure uniqueness. I'll call this ID.
So your relationship graph has three table occurrences:
Applications::Applicaiton = ObjectsParent::Application
ObjectsParent::Application = ObjectsChildren::Application, ObjectsParent::Program = ObjectsChildren::Program
Now create a calculation field in Objects, and calculating from the context of ObjectsParent, give it the following formula:
AppCount = Count( ObjectsChildren::ID )
Create a calculation field in Applications and calculating from the context of the table occurrence you used to relate it to ObjectsParent with the following formula:
AppCount = ObjectsParent::AppCount
The count field in Objects will have the same value for every object with the same application, so it doesn't matter which one you get this data from.
If you now view the data in Applications in list view, you can place the Applications::Application and Applications::AppCount fields on the layout and you should get what you've requested.