cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
OverwatchMedia
Frequent Visitor

Can I do a Calculated field based on a Category?

Is the following possible? In my CDS I created a new entity with the fields "ID" "Owed" and "Category", an example is provided in the table below. 

I want to be able to create a new field "Total_Category" that shows up in each record and it calculates all of the Owed fields of the same category together. that would look something like below in the end

 

IDOWEDCategoryTotal_Category
110Apples20
210Apples20
310Kiwi30
410Kiwi30
510Kiwi30
610Banana10

 

This is only an example and I'm pretty sure its how I need it to work. I need to create a flow that when I click one record to email, it will indicate the amount I owe for one record as well as the amount for all records combined with the same exact category.

2 ACCEPTED SOLUTIONS

Accepted Solutions
EricRegnier
Super User
Super User

Hi @OverwatchMedia.

If you need to store the total as a separate field then you'll have to calculate the total in a separate process then save the result in the field. The tricky part is any time the OWED or Category are updated, you'll have to recalculate the total for all those rows. If you only need that total in the email, then suggest to calculate that total before when sending the email (i.e. in the same process). The calculation can be done in a Power Automate flow on create or update. Use the "Common Data Service (current environment)" connector where you'll have to create the flow from a solution. Here's the high level logic:

  1. Create a new whole number field in your entity if you decide to store the results
  2. On create where OWED and Category contains data or on update of OWED or Category using the "when a record is created, updated or deleted" trigger
  3. Retrieve the records where Category = the Category of the trigger using the list record action
  4. Loop through the records and calculate the total. 
    1. You'll need to "initialize variable" and "set variable" actions for the total count
  5. Loop through the records again and update the each total using the update a record action
    1. If you only decided to calculate for the email then this step can be omitted

Other things to note are: flow runs in the background (async) so you might not always have the latest and greatest total as it might be in the process of calculating. There could be a race condition where the records got updated after the new total recalculated by an older update. This can be a problem depending on the number of transactions and records.

Hope this helps....

View solution in original post

v-bofeng-msft
Community Support
Community Support

Hi @OverwatchMedia :

Could you tell me how many records are in your entity?

I have a simple way to add a new column(Just add a column when referencing without changing the data source😞

Using the AddColumns function to call the data source.I've made a test for your reference:

 

RenameColumns(
    AddColumns(
        RenameColumns(
            YourEntity,
            "Category",
            "TheCategory"
        ),  /*Rename the Category column to TheCategory to prevent ambiguity*/
        "Total_Category",
        Sum(
            Filter(
                YourEntity,
                TheCategory = Category
            ),
            OWED
        )/*Add column otal_Category and sum*/
    ),
    "TheCategory",
    "Category"
)/*Change the name of TheCategory back to Category*/

 

1.JPG

Best Regards,

Bof

View solution in original post

8 REPLIES 8
EricRegnier
Super User
Super User

Hi @OverwatchMedia.

If you need to store the total as a separate field then you'll have to calculate the total in a separate process then save the result in the field. The tricky part is any time the OWED or Category are updated, you'll have to recalculate the total for all those rows. If you only need that total in the email, then suggest to calculate that total before when sending the email (i.e. in the same process). The calculation can be done in a Power Automate flow on create or update. Use the "Common Data Service (current environment)" connector where you'll have to create the flow from a solution. Here's the high level logic:

  1. Create a new whole number field in your entity if you decide to store the results
  2. On create where OWED and Category contains data or on update of OWED or Category using the "when a record is created, updated or deleted" trigger
  3. Retrieve the records where Category = the Category of the trigger using the list record action
  4. Loop through the records and calculate the total. 
    1. You'll need to "initialize variable" and "set variable" actions for the total count
  5. Loop through the records again and update the each total using the update a record action
    1. If you only decided to calculate for the email then this step can be omitted

Other things to note are: flow runs in the background (async) so you might not always have the latest and greatest total as it might be in the process of calculating. There could be a race condition where the records got updated after the new total recalculated by an older update. This can be a problem depending on the number of transactions and records.

Hope this helps....

View solution in original post

v-bofeng-msft
Community Support
Community Support

Hi @OverwatchMedia :

Could you tell me how many records are in your entity?

I have a simple way to add a new column(Just add a column when referencing without changing the data source😞

Using the AddColumns function to call the data source.I've made a test for your reference:

 

RenameColumns(
    AddColumns(
        RenameColumns(
            YourEntity,
            "Category",
            "TheCategory"
        ),  /*Rename the Category column to TheCategory to prevent ambiguity*/
        "Total_Category",
        Sum(
            Filter(
                YourEntity,
                TheCategory = Category
            ),
            OWED
        )/*Add column otal_Category and sum*/
    ),
    "TheCategory",
    "Category"
)/*Change the name of TheCategory back to Category*/

 

1.JPG

Best Regards,

Bof

View solution in original post

@v-bofeng-msft , sorry I am super late, right now I might have 10 records. I could be more moving forward. How would that affect your suggestion?

@EricRegnier Apologies on being so late: My setup is a little different based on what you said. What am I doing wrong here, or am I totally looking at this the wrong way and using the wrong actions? It is getting stuck at listrecords action

 

Like you said, I do not need to store the Total_Category value since it can just be sent out in an email. 

 

As a result, A bunch of values are being created from an email and a new record is created in my CDS based on those values formatted in the table as I originally posted. For reference, in the listrecords action, the InitilaizeVariable named EmailCompany is the Category field of my CDS table. 

 

Annotation 2020-09-08 185523.jpg

A couple of things from what I can see from your screenshot

  1. Filter expression is invalid. Use the physical field name instead (e.g. "new_category"). What data type is Category field?
    • if Category is an optionset, filter on the integer value of the optionset item:
      new_category eq 1000100​
    • if Category is a lookup, filter based on the GUID of the Category record. Prefix with _ and suffix with _value and don't forget to wrap the GUID with '
      _new_category_value eq '00000000-0000-0000-0000-000000000000'
  2. After the List Record step, you need to loop through the records to get the count. Here's an example:
    2020-09-09_11-23-12.png

 

By field_name, do you mean something like cr5cc_category?

 

 

Also, The Category field is actually regular text. Does that make a difference in the filter query text? I currently have "cr5cc_category eq @{variables('EmailCompany')}" where the @EmailCompany is my initialize variable mentioned in the previous photo.

 

I currently get a 400 error, "Could not find a property named 'Apples' on type 'Microsoft.Dynamics.CRM.cr2bb_fruit'."

Yes exactly: cr5cc_category

If it's text you just need to enclose the text after the "eq" with ': cr5cc_category eq '@{variables('EmailCompany')}'

Thank you very much for the help. Took me a while to get it set up since I needed to implement a few extra things. Just realized Microsofts post was incorrectly accepted as the answer so just changed it to your initial one.

Helpful resources

Announcements
UG GA Amplification 768x460.png

Launching new user group features

Learn how to create your own user groups today!

Power Apps Community Call Jan. 2022 768x460.png

Power Apps Community Call

Please join us on Wednesday, January 19th, at 8a PDT. Come and learn from our amazing speakers!

Community Connections 768x460.jpg

Community & How To Videos

Check out the new Power Platform Community Connections gallery!

Users online (2,133)