How to subtract current value from previous value in a column using C/AL code - microsoft-dynamics

I have a column that I need to subtract a current value from the previous value in Bid Price LCY column and save the result on another column called Daily Return(Offer). The value is incrementing instead of decrementing in Daily Return(Offer) column.
Here is the image of what I am getting
Here is my code:
LastDailyReturn := 0;
TempFundPriceNew.RESET;
TempFundPriceNew.SETCURRENTKEY("Fund No.",Date);
TempFundPriceNew.SETRANGE(TempFundPriceNew."Fund No.","Temp Fund Price New"."Fund No.");
TempFundPriceNew.SETRANGE(TempFundPriceNew.Date,StartDate,"Temp Fund Price New".Date);
IF TempFundPriceNew.FINDFIRST THEN REPEAT
LastDailyReturn += "Temp Fund Price New"."Bid Price LCY";
UNTIL TempFundPriceNew.NEXT = 0;

i think that this could be a solution:
Filter the current table with the range of data that you want;
Do the FindFirst, in this case should find the 8/1/2021;
Create a counter, and inside the loop increment it by one;
Use the function XRec, in order to have the previous record obtained or use the function Until.Next() - 1 in order to go back or still in the loop you can save the previous record value into a dummy variable in order to have it the next cycle of loop;
Do the difference between each other and insert it inside the table;

Related

I want to use a calculated field as a constant reference value, but it keep changing with the filter

I have a column Sales and a column date, I want to use the average sales from month 10 (October) as reference to compare with the others months.
I made a calculated field Sales_december like avgIf(Sales,date<parseDate('10/31/2022','MM/dd/yyyy'))
*10/01/2022 is the first date in the date column
I'm using Sales_december as a reference line in a line chart, every time I filter the date to a specific month Sales_december goes to zero. The only solution I found was to create a field Sales_december_const with the average value of Sales_december, Ex.: Sales_december_const =2000
avgOver(ifelse({date} < parseDate('10/31/2022', 'MM/dd/yyyy'), {sales}, NULL), [], PRE_FILTER)
You need to use PRE_FILTER in this position to avoid the filters affecting the value.
PRE_FILTER and PRE_AGG are not supported with avgif, but using ifelse here should work how you have it set up in your calculation.

In the Films table create a calculated column called NumberBreaks which shows for each film the number of breaks needed

The Films table looks like this
There is a ComfortBreaks table looking like image
In the Films table I need to create a calculated column called NumberBreaks which shows for each film the number of breaks needed. To do this I have to pick out the value of the Breaks column where:
The value of the lower limit in the ComfortBreaks table is less than or equal to this film's running time in minutes
and
The value of the upper limit in the ComfortBreaks table is greater than this film's running time in minutes.
the result should look like the image below
There cannot be a relationship between the two tables. so this has to be done without creating relationship between them.
I tried lookup function but it showed error:A table of multiple values was supplied where a single value was expected.
You can use this below code for your custom column. Considering
number_of_breaks =
VAR current_row_run_time_minutes = Films[RunTimeMinutes]
RETURN
MINX (
FILTER(
ComfortBreaks,
ComfortBreaks[LowerLimit] <= current_row_run_time_minutes
&& ComfortBreaks[UperLimit] > MonthNumber
),
ComfortBreaks[Breaks]
)
You can perform your further average calculation on the new custom column now.

grouping table by a field and finding sum in Dynamics NAV 2018

I'd like to group a table by a field- employer code and then calculate the sum of a decimal field- total contribution in that group from C/AL code. Here's my table structure
Employer No_ Total Contribution
PRTEMP005022 1817.64
PRTEMP005022 1782
PRTEMP005022 2049.3
PRTEMP005022 1568.16
PR0000247148 47750.62
PR0000247148 47532.81
PU0000400011 5314.52
PU0000400011 5314.52
PU0000400011 17225.83
PU0000400011 4509.61
STRV00000000 6088.72
STRV00000000 4065.36
STRV00000000 2191.18
STRV00000000 3485.42
STRV00000000 4709.77
How can I assign the employer code and the sum of the total contributions for that employer to variables from C/AL code
I'll assume that you have the employers in a seperate table called Employers. Otherwise you will have to create the values to group by in a temporary table (table Name/Value Buffer is userful there).
Employer.RESET();
//Employer.SETRANGE(... filtering if needed
IF Employer.FINDSET() THEN BEGIN
REPEAT
YourTable.RESET();
YourTable.SETRANGE("Employer No.", Employer."No.");
YourTable.CALCSUMS("Total Contribution");
TotalContribution := YourTable."Total Contribution";
EmployerNo := YourTable."Employer No.";
UNTIL Employer.NEXT() = 0;
END;
Please be aware that the variables TotalContribution and EmployerNo will be overwritten on every iteration of the loop so you will have to do something with them.

Power BI remove duplicates based on max value

I have 2 column; ID CODE, value
Remove duplicates function will remove the examples with the higher value and leave the lower one. Is there any way to remove the lower ones? The result I expected was like this.
I've tried Buffer Table function before but it doesn't work. Seems like Buffer Table just works with date-related data (newest-latest).
You could use SUMMARIZE which can be used similar to a SQL query that takes a MIN value for a column, grouped by some other column.
In the example below, MIN([value]) is taken, given a new column name "MinValue", which is grouped by IDCode. This should return the min value for each IDCode.
NewCalculatedTable =
SUMMARIZE(yourTablename, yourTablename[IDCode], "MinValue", MIN(yourTablename[value]) )
Alternatively, if you want the higher values just replace the MIN function with MAX.

How to split records based on a value field in a column

i have a scenario like the below ...where based on value of num_seats i have to split the rows in target into that amount of data along with another field(seat_num) which will be having counter which will increment by 1.Please suggest..
You can clone the rows based on a field value (in this case, num_seats), remove the original (non-cloned) row, then calculate the seat number and replace the original fields (num_seats, seat_num, last_seat, etc.) with the new values:
Here's a Gist of the above transformation: https://gist.github.com/mattyb149/e4cf796ff45983ebf87e