how to calculate total balance per state per country using accumulate function in openedge - progress-4gl

I have tried the code.
DEFINE VARIABLE totalbalance AS DECIMAL NO-UNDO.
DEFINE FRAME f1
WITH CENTERED THREE-D SIZE 100 BY 50.
FOR EACH customer NO-LOCK BREAK BY country WITH FRAME f1:
ACCUMULATE balance(TOTAL BY country).
IF FIRST-OF(customer.country) THEN
DISPLAY customer.country.
DISPLAY customer.state customer.balance.
IF LAST-OF(customer.country) THEN
DO:
DISPLAY SKIP FILL("-", 25) AT 50 FORMAT "x(25)".
DISPLAY ACCUM TOTAL customer.balance AT 51.
DISPLAY SKIP FILL("-", 25) AT 50 FORMAT "x(25)".
END.
END.
It only gives total balance per country but I also want to display total balance per state.

You do not have a "break by state" so it will do nothing by state.
Something like this (not very tested):
define frame f1
with
centered
.
for each customer no-lock break by country by state with frame f1:
accumulate balance( total by country ).
accumulate balance( sub-total by state ).
if first-of( customer.country ) then
display customer.country.
if first-of( customer.state ) then
display customer.state.
display customer.balance.
if last-of( customer.country ) then
do:
display skip fill( "-", 25 ) at 50 format "x(25)".
display accum total customer.balance at 51.
display skip fill( "-", 25 ) at 50 format "x(25)".
end.
if last-of( customer.state ) then
do:
display skip fill( "-", 25 ) at 80 format "x(25)".
display accum sub-total customer.balance at 81.
display skip fill( "-", 25 ) at 80 format "x(25)".
end.
end.
(That probably doesn't actually work... but the multiple BY clauses are the main thing that you are missing.)
Personally I find the ACCUM syntax to be unpleasant and not worth bothering with. I usually just create and manage simple variables. I find that sort of coding to be much cleaner. So unless this is a "gotchya" question on a skills test I strongly suggest that you avoid coding in this manner. If it is a "gotchya" question move on to a more suitable prospective employer -- you don't want to work for anyone who thinks that this is a good idea.
I would do it more like this:
define variable countryBalance as decimal no-undo.
define variable stateBalance as decimal no-undo.
for each customer no-lock break by country by state with frame f1:
if first-of( customer.country ) then
do:
display customer.country.
countryBalance = 0.
end.
if first-of( customer.state ) then
do:
display customer.state.
statebalance = 0.
end.
assign
countryBalance = countryBalance + customer.balance
stateBalance = stateBalance + customer.balance
.
display customer.balance.
if last-of( customer.country ) then
do:
display countryBalance.
end.
if last-of( customer.state ) then
do:
display stateBalance.
end.
end.
I think that is a whole lot easier to read, understand and maintain.

Related

Sum in range until value change

I'am trying to use this formula to make it work
=ARRAYFORMULA(IF(ISDATE_STRICT(S2:S) ; (MATCH(MAX(AB2:AB),AB2:AB;0)-1) ; "" ))
If there is a date in Column "S" I want it to display the sum of the blanks that would appear if in Column "S" is text
=ARRAYFORMULA(IF(ISDATE_STRICT(S2:S) ; ArrayFormula(MATCH(FALSE ; ISBLANK(AB2:AB) ; 0)-1) ; "" ))
I've tried this one as well but I only get 0's as a result.
Any idea how I can make it work?
Here is the sample sheet.
https://docs.google.com/spreadsheets/d/19f5phXeAwXwrKbWz7njgbznmurOav72GUuo_5IGcbls/edit?usp=sharing
in Q2 use:
=ARRAYFORMULA(IF(ISBLANK(
I1:INDEX(I:I; ROWS(I:I)-1));
{N2:INDEX(N:N; ROWS(N:N))\
I1:INDEX(N:N; ROWS(N:N)-1)};
I1:INDEX(O:O; ROWS(O:O)-1)))
in X2 use:
=INDEX(LAMBDA(x; IFNA(VLOOKUP(x; QUERY(VLOOKUP(ROW(x);
IF(ISDATE_STRICT(x); {ROW(x)\x}); 2; 1);
"select Col1,count(Col1) group by Col1"); 2; 0)-1))
(Q2:INDEX(Q:Q; MAX((Q:Q<>"")*ROW(Q:Q)))))
UPDATE:
we start with column Q. we can take a range Q2:Q but that range contains a lot of empty rows. the next best thing is to check the last non-empty row and set it as the end of the range resulting in Q2:Q73. but static 73 won't do in case the dataset would grow or shrink so to get 73 dynamically we take the MAX of multiplication of Q:Q not being empty and row number of that case eg. Q:Q<>"" will output only TRUE or FALSE so what we are getting is
...
TRUE * 72 = 1 * 72 = 72
TRUE * 73 = 1 * 73 = 73
FALSE * 74 = 0 * 74 = 0
...
so the formula for getting Q2:Q73 is:
=Q2:INDEX(Q:Q; MAX((Q:Q<>"")*ROW(Q:Q)))
it could also be:
=INDEX(INDIRECT("Q2:Q"&MAX((Q:Q<>"")*ROW(Q:Q))))
but it's just long to type... next, we use the new LAMBDA function that allows us to reference cell/range/formula with a placeholder. simple LAMBDA syntax is:
=LAMBDA(x; x)(A1)
where x is A1 and we can do whatever we want with the 2nd (x) argument of LAMBDA like for example:
=LAMBDA(a, a+a*120-a/a)(A1)
you can think of it as:
LAMBDA(A1, A1+A1*120-A1/A1)(A1)
or as just:
=A1+A1*120-A1/A1
the issue here is that we repeat A1 4 times but with LAMBDA we do it only once. also, imagine if we would have 100 characters long formula instead of A1 so the final formula with lambda would be 300 characters shorter compared to "old way" formula.
back to our formula... x is the representation of Q2:Q73. now let's focus on VLOOKUP. basically, the idea here is that IF Q column contains a date we return that date, otherwise we return the last date from above. simply put:
=ARRAYFORMULA(VLOOKUP(ROW(Q2:Q73);
IF(ISDATE_STRICT(Q2:Q73); {ROW(Q2:Q73)\Q2:Q73}); 2; 1))
as you can see Y2, Y3 and Y4 are the same so all we need to do is to count them up and later take away one to exclude Q2 but include just Q3 and Q4 eg. 3-1=2. for that we use simple QUERY where the output is:
date count
30.06.2022 3
so all we need to do is to pair up dates from Q column to QUERY output for that we use the outer VLOOKUP where the output is as follows:
3
#N/A
#N/A
9
#N/A
#N/A
...
now is the right time for that -1 correction while we have these errors coz ERROR-1=ERROR and 3-1=2 so after this -1 correction the output is:
2
#N/A
#N/A
8
#N/A
#N/A
...
and all we need to do now is to hide errors with IFERROR and the output is column X

Power Query - remove characters from number values

I have a table field where the data contains our memberID numbers followed by character or character + number strings
For example:
My Data
1234567Z1
2345T10
222222T10Z1
111
111A
Should Become
123456
12345
222222
111
111
I want to get just the member number (as shown in Should Become above). I.E. all the digits that are LEFT of the first character.
As the length of the member number can be different for each person (the first 1 to 7 digit) and the letters used can be different (a to z, 0 to 8 characters long), I don't think I can SPLIT the field.
Right now, in Power Query, I do 27 search and replace commands to clean this data (e.g. find T10 replace with nothing, find T20 replace with nothing, etc)
Can anyone suggest a better way to achieve this?
I did successfully create a formula for this in Excel...but I am now trying to do this in Power Query and I don't know how to convert the formula - nor am I sure this is the most efficient solution.
=iferror(value(left([MEMBERID],7)),
iferror(value(left([MEMBERID],6)),
iferror(value(left([MEMBERID],5)),
iferror(value(left([MEMBERID],4)),
iferror(value(left([MEMBERID],3)),0)
)
)
)
)
Thanks
There are likely several ways to do this. Here's one way:
Create a query Letters:
let
Source = { "a" .. "z" } & { "A" .. "Z" }
in
Source
Create a query GetFirstLetterIndex:
let
Source = (text) => let
// For each letter find out where it shows up in the text. If it doesn't show up, we will have a -1 in the list. Make that positive so that we return the index of the first letter which shows up.
firstLetterIndex = List.Transform(Letters, each let pos = Text.PositionOf(text, _), correctedPos = if pos < 0 then Text.Length(text) else pos in correctedPos),
minimumIndex = List.Min(firstLetterIndex)
in minimumIndex
in
Source
In the table containing your data, add a custom column with this formula:
Text.Range([ColumnWithData], 0, GetFirstLetterIndex([ColumnWithData]))
That formula will take everything from your data text until the first letter.

MS Access 2010 Form Box Control Source IIF Statement

I am running into the Max Character Issue when trying to put my IIF statement into a box I have created for the calculation of a score on my form. The box code is:
=IIf([cbov1]="na" And [cbov2]="na" And [cbov3]="na" And [cbov4]="na" And [cbov5]="na" And [cboV6]="na" And [cboV7]="na" And [cboV8]="na" And [cboV9]="na" And [cboV10]="na" And [cboV11]="na" And [cboV12]="na" And [cboV13]="na" And [cboi1]="na" And [cboi2]="na" And [cboi3]="na" And [cboi4]="na" And [cbop1]="na" And [cbop2]="na" And [cbop3]="na" And [cbop4]="na" And [cbop5]="na" And [cbop6]="na" And [cbop7]="na" And [cbop8]="na" And [cbop9]="na" And [cbop10]="na" And [cbop11]="na" And [cbof1]="na" And [cbof2]="na" And [cbof3]="na" And [cbof4]="na" And [cbof5]="na" And [cbof6]="na" And [cbof7]="na" And [cbof8]="na" And [cbof9]="na" And [cbof10]="na" And [cbom1]="na" And [cbom2]="na" And [cbom3]="na" And [cbom4]="na" And [cbom5]="na" And [cbom7]="na" And [cbom8]="na" And [cbom9]="na" And [cbom10]="na" And [cbom6]="na",0,(IIf([cboV1]="yes",0,0)+IIf([cbov2]="yes",0,0)+IIf([cbov3]="yes",0,0)+IIf([cbov4]="yes",0,0)+IIf([cbov5]="yes",0,0)+IIf([cboV6]="yes",0,0)+ IIf([cboV7]="yes",0,0)+ IIf([cboV8]="yes",0,0)+ IIf([cboV9]="yes",0,0)+ IIf([cboV10]="yes",0,0)+ IIf([cboV11]="yes",0,0)+ IIf([cboV12]="yes",0,0)+ IIf([cboV13]="yes",0,0)+IIf([cboi1]="yes",5,0)+IIf([cboi2]="yes",3,0)+IIf([cboi3]="yes",3,0)+ IIf([cboi4]="yes",4,0)+IIf([cbop1]="yes",5,0)+IIf([cbop2]="yes",5,0)+IIf([cbop3]="yes",5,0)+IIf([cbop4]="yes",5,0)+ IIf([cbop5]="yes",5,0)+ IIf([cbop6]="yes",4,0)+ IIf([cbop7]="yes",4,0)+ IIf([cbop8]="yes",4,0)+ IIf([cbop9]="yes",4,0)+ IIf([cbop10]="yes",2,0)+IIf([cbop11]="yes",2,0)+IIf([cbof1]="yes",1,0)+IIf([cbof2]="yes",1,0)+IIf([cbof3]="yes",1,0)+IIf([cbof4]="yes",1,0)+ IIf([cbof5]="yes",1,0)+ IIf([cbof6]="yes",1,0)+ IIf([cbof10]="yes",0,0)+ IIf([cbof7]="yes",3,0)+ IIf([cbof8]="yes",3,0)+ IIf([cbof9]="yes",3,0)+IIf([cbom1]="yes",5,0)+IIf([cbom2]="yes",1,0)+IIf([cbom3]="yes",1,0)+IIf([cbom4]="yes",1,0)+IIf([cbom5]="yes",1,0)+IIf([cbom6]="yes",1,0) +IIf([cbom7]="yes",3,0) +IIf([cbom8]="yes",2,0) +IIf([cbom9]="yes",5,0) +IIf([cbom10]="yes",5,0))/(IIf([cboV1]="na",0,0)+IIf([cbov2]="na",0,0)+IIf([cbov3]="na",0,0)+IIf([cbov4]="na",0,0)+IIf([cbov5]="na",0,0)+IIf([cboV6]="na",0,0)+ IIf([cboV7]="na",0,0)+ IIf([cboV8]="na",0,0)+ IIf([cboV9]="na",0,0)+ IIf([cboV10]="na",0,0)+ IIf([cboV11]="na",0,0)+ IIf([cboV12]="na",0,0)+ IIf([cboV13]="na",0,0)+IIf([cboi1]="na",0,5)+IIf([cboi2]="na",0,3)+IIf([cboi3]="na",0,3)+ IIf([cboi4]="na",0,4)+IIf([cbop1]="na",0,5)+IIf([cbop2]="na",0,5)+IIf([cbop3]="na",0,5)+IIf([cbop4]="na",0,5)+ IIf([cbop5]="na",0,5)+ IIf([cbop6]="na",0,4)+ IIf([cbop7]="na",0,4)+ IIf([cbop8]="na",0,4)+ IIf([cbop9]="na",0,4)+ IIf([cbop10]="na",0,2)+ IIf([cbop11]="na",0,2)+IIf([cbof1]="na",0,1)+IIf([cbof2]="na",0,1)+IIf([cbof3]="na",0,1)+IIf([cbof4]="na",0,1)+ +IIf([cbof5]="na",0,1)+ +IIf([cbof6]="na",0,1)+ +IIf([cbof7]="na",0,3)+ +IIf([cbof8]="na",0,3)+ +IIf([cbof9]="na",0,3)+ +IIf([cbof10]="na",0,0)+IIf([cbom1]="na",0,5)+IIf([cbom2]="na",0,1)+IIf([cbom3]="na",0,1)+IIf([cbom4]="na",0,1)+IIf([cbom5]="na",0,1)+IIf([cbom6]="na",0,1) +IIf([cbom7]="na",0,3) +IIf([cbom8]="na",0,2) +IIf([cbom9]="na",0,5) +IIf([cbom10]="na",0,5)))*(IIf([cbov1]="no" Or [cbov2]="no" Or [cbov3]="no" Or [cbov4]="no" Or [cbov5]="no" Or [cboV6]="no" Or [cboV7]="no" Or [cboV8]="no" Or [cboV9]="no" Or [cboV10]="no" Or [cboV11]="no" Or [cboV12]="no" Or [cboV13]="no",0,1))
The purpose of the score is to score "Yes" with points, Score "No" as no points, and then have "NA" remove from the overall score. So if a person has 67 out of 67 points, they get a 100. The maximum points is 100 if all questions are answered "Yes" or "No". I need to have all portions of the calculation because CBOV1-13 have a stipulation of if they are "No" the score is automatically 0%.
I don't know of a work around for the MAX CHARACTER you get within the expression builder on the Control Source box via the properties sheet.
Any help on a work around for this issue or ways to make the code shorten and fit with the same end result would be a huge help.
First, I would be tempted to have my field as a numeric (0,1,2 instead of "na,Yes,No"
Then you could use addition...IIF{cvb01 + Cvb02 +cvb03 = 0, 0 ,...else
Using Strings
I can think of two ways. One in the formula using concatenation (ugly)
IIF[cvb01] & [cvb02] & [cvb03] = "NANANA", 0 , ...Else)
I would be more tempted to write a function to take care of it.
dim NAcount as integer
Dim YesCount as integer
Dim NoCount as integer
dim ctr as integer
dim StrAns as string
for ctr = 1 to 10
StrAns = Fields ("cvb" & Ctr)
Select case StrAns
Case "NA"
NaCount = NACount + 1
'...add values here
Case "Yes"
'...more values go here
Case "No"
'... more values
End Select
Next Ctr
...

Calculation of Previous field

New to CR and use CR v10 and SQL Server 2000.
For the first record i.e Beginning Balance , the calculation is sum(field) from the input date, which I have calculated in SP as BegDateSum
But for the rest of the records under a group, the calculation should be previous(balance)+IN+OUT
Sample has been given:
Date Doc Descrip IN OUT Balance
Group Header-------- Beginning Balance-------------- 50 <---- sum(field) from my inputdate
3/2/2012 A -1 0 49 <-- (50+(-1)+0)
4/2/2012 B -2 0 47 <-- (49+(-2)+0)
5/2/2012 C 0 3 50
6/2/2012 D -2 3 51
How do I achieve this?
I am not sure whether to use running total, in case I have to how to do it.
A running total field won't work in this case, they are designed to add up (or count, or average, etc) one field and give you the sub-totals automatically. But, we can do some custom functions that will give the results you need. Assuming that your initial 50 is a static value, you would set a variable to that amount, and then add the IN and OUT values as you go along (printing that result of that).
First, initialize the value in the report header with a formula like:
WhilePrintingRecords;
Global NumberVar Balance;
Balance := 50;
""; //print nothing on the screen
Then, the formula to calculate and show the new balance, in the bar where the data is:
WhilePrintingRecords;
Global NumberVar Balance;
Balance := Balance + {tableName.IN} + {tableName.OUT};
The last line both calculates the new value, and tells what the result of the formula should be.
If the "50" is calculated somehow, then that will have to be done before the formula that calculates the new balance. If it is based off of the first record read in, you'll want to use a formula that includes If PreviousIsNull({tableName.Balance}) Then ..., that is usually a good indicator of the first record in the data set (unless that field can be null).

T-SQL: How to make a positive value turn into the equivalent negative value (e.g "10.00" to "-10.00"

Ok so i have a DECIMAL field called "Score". (e.g 10.00)
Now, in my SP, i want to increment/decrement the value of this field in update transactions.
So i might want to do this:
SET #NewScore = #CurrentScore + #Points
Where #Points is the value im going to increment/decrement.
Now lets say #Points = 10.00.
In a certain scenario, i want 10.00 to become -10.00
So the statement would be translated to:
SET #NewScore = #CurrentScore + -10.00
How can i do that?
I know its a strange question, but basically i want that statement to be dynamic, in that i dont want to have a different statement for incrementing/decrementing the value.
I just want something like this:
SET #Points = 10.00
IF #ActivityBeingPerformedIsFoo
BEGIN
-- SET #Points to be equivalent negative value, (e.g -10.00)
END
SET #NewScore = #CurrentScore + #Points
Can't you just multiply it by -1?
I always do 0 - #Points. It was this way in some code I inherited. "A foolish consistency..."
Multiply #Points by -1 in that certain scenario.
I thought of subtracting it with a multiple of 2, i.e. x - 2x