Current Year Ytd:

We are calculating the total amount from current year to date then following these steps

Steps:

1.Go to PowerApps create the fields amount, year, ytd (total year to date) in one table main form

  1. We should compulsorily create the fields amount and ytd in whole number then the year field is single line of text field.

3.Then save these table in your app

4.Create some records in previous year and current year with some amount value

  1. Create a new web resource and write java script code given below
java script Code:

function Totalcurrentyearamount(executionContext) {

    var formContext = executionContext.getFormContext();

    var totalytd = 0; // Initialize total YTD to 0

    const currentDate = new Date(); // Get the current date

    const currentYear = currentDate.getFullYear(); // Get the current year

    var apiContext = (typeof Xrm !== “undefined”) ? Xrm : parent.Xrm;

   // Retrieve all records from vijay_income entity

    apiContext.WebApi.retrieveMultipleRecords(“vijay_income”, “?$select=vijay_amount,vijay_year”).then(

        function success(results) {

            console.log(results);

        // Loop through the retrieved records

            for (var i = 0; i < results.entities.length; i++) {

                var result = results.entities[i];

                // Convert vijay_amount to an integer

                var vijay_amount = parseInt(result[“vijay_amount”], 10); // Ensure the amount is treated as an integer

                var vijay_year = parseInt(result[“vijay_year”],10); // Year field

              // Ensure vijay_year exists and matches the current year

                if (  vijay_year == currentYear) {

                    // Add the amount to total YTD if year matches current year

                    totalytd += vijay_amount;

                  }

            }

          // Log the total YTD for debugging purposes

          console.log(“Total YTD: ” + totalytd);

         // Set the total YTD value in the ‘vijay_ytd’ field on the form

            formContext.getAttribute(“vijay_ytd”).setValue(totalytd >0 ? totalytd.toString() : null);

        },

        function (error) {

            console.log(“Error in retrieveMultipleRecords: ” + error.message);

        }

    );

}

  1. Then add the web resource in tour main form in form libraries and the function name in event handlers form ON – load.

7.Save and publish all customizations

All Previous year and current year amount will be added and set to the Total ytd field.

Previous Year:

 

Current Year

Previous Year Amount will be added and set to the Total Ytd Field

Current Year:

Current Year Ytd

Current Year Amount will be added and set to the Total Ytd Field

For any Help or Queries Contact us on info@crmonce.com or +91 8096556344

https://www.crmonce.com/field-parameter-in-power-bi-desktop/