I want to edit program thereby loop at gt_ugtyt table which is created by a database table. However I have a problem.
STDATE-STTIME-ENDDATE-ENDTIME (Field names)
I want to sum (ENDTIME-STTIME). It means "working time". But you can see that there are same rows as (STDATE-STTIME-ENDDATE-ENDTIME). Because we give twice confirmation in our multiple production process (That's not important. Just notice). I have to work with unique variables.
I defined a temporary table as gt_ugtyt_temp and wrote the following code
SORT gt_ugtyt_temp BY endat stdat sttim entim.
DELETE ADJACENT DUPLICATES FROM gt_ugtyt_temp COMPARING endat stdat sttim entim.
But i want to loop in gt_ugtyt . I want to read and calculate unique rows as (STDATE-STTIME-ENDDATE-ENDTIME). But I don't want to write
DELETE ADJACENT DUPLICATES FROM gt_ugtyt COMPARING endat stdat sttim entim.
Because other codes are using all rows on gt_ugtyt .
Is there any code like (This is not real code :) ) :
READ UNIQUE ROWS FROM FROM gt_ugtyt COMPARING endat stdat sttim entim.
I don't clearly understand what you want to do, but, can't you copy the rows from gt_ugtyt to a local temporary table lt_ugtyt_unique_rows and then do the operations you need on it?
DATA:
lt_ugtyt_unique_rows LIKE gt_ugtyt.
INSERT LINES OF gt_ugtyt INTO lt_ugtyt_unique_rows.
DELETE ADJACENT DUPLICATES FROM lt_ugtyt_unique_rows COMPARING endat stdat sttim entim.
*Sum what you need with lt_ugtyt_unique_rows
So gt_ugtyt_temp contains unique records, and gt_ugtyt contains all records, and you want to sum all unique records from gt_ugtyt?
how about this pseudo code:
loop at gt_ugtyt_temp assigning [line]
loop at gt_ugtyt where stdate = [line]-stdate sttime = [line]-sttime (etc)
[sum all values either changing [line] or put them in a new table]
endloop.
endloop
Related
I do not manage to update single values in a dataframe based on values of a dictionary before another calculation is executed
I am working with a dataframe and want to calculate values in different colums row per row. After the calculations of one row is finished, the values in the rows thereafter need to be changed before a new calculation can happen. This is since the values in the rows thereafter are dependent upon the results of the previous row.
To approach it, I am working with a dictionary. Currently I am working in Excel where I manage to update the values of a single cell with a dictionary. However to make the calculaton faster I want to work with a proper dataframe.
I manage to update the dictionary based on the results, but I do not manage to update my dataframe with these new values of the citionary
The code that works for my current model based on Excel is:
dict1={1:10,2:15.....38:29} #my dictionary
for row in range(2,sheet.max_row+1):
#updates the table with values of the dictionary before each calculation
sheet['F'+str(row)].value = dict1[sheet['C'+str(row)].value]
# calculations being executed
(.....)
#updating the dictionary with the results of the calculations in the row
dict1_1={sheet['C'+str(row)].value :sheet['F'+str(row)].value}
dict1.update(dict1_1)
What I tried so far with a dataframe looks like this:
for row in df.T.itertuples():
df.replace({"P_building_kg_y": dict1}) ##### <-----HERE IS THE PROBLEM!
# calculations being executed
(.....)
#updating the dictionary with the results of the calculations in the row
dict1_1=dict(zip(df.FacilityID, df.P_building_kg_y))
dict1.update(dict1_1)
I only want to update the values in the dataframe based on the dictionary. If you know a way of how to do it I would really appreciate it!
I have list of keywords; CORPORATE, REAL ESTATE, COMPETITION, TRADE, DISPUTE
I want to be able to count the number of occurrences these keywords appear between columns practice_area1—Practice_area10. Therefore, I want to scan across 10 columns and create new columns. Each new column will represent each of the keywords (above) and a count as a value e.g. Corporate 4
Once this statement has ran and we have our new columns I want to create a new variable “Practice Group” which is populated with the highest count of the new variables we have just created. The dataset below is an example of how the data should look:
Please could somebody offer me some advice of the best approach to do this?
Many Thanks
Chris
All you need to do is use makean array for all the columns which you want to check. Then do loop it through each column for the word you want to check by using count function and add the count in the loop.
Code below is checking for three columns and three values. You can apply this code to as many columns as you want.
data have(drop= i);
col1 = 'CORPORATE, REAL ESTATE, REAL ESTATE';
col2= 'CORPORATE, CORPORATE, TRADE, TRADE, TRADE, REAL ESTATE';
col3= 'TRADE, TRADE, DISPUTE,REAL ESTATE';
array col[*] col1 - col3;
realestate=0;/*starting with zero*/
trade=0;
corporate= 0;
do i = 1 to 3;
realestate =realestate+count( col(i), 'REAL ESTATE');/* adding through the loop*/
TRADE =trade+count( col(i), 'TRADE');
CORPORATE= corporate+count( col(i), 'CORPORATE');
end;
run;
I want to check in a powerquery new column if a string like "This is a test string" contains any of the strings list items {"dog","string","bark"}.
I already tried Text.PositionOfAny("This is a test string",{"dog","string","bark"}), but the function only accepts single-character values
Expression.Error: The value isn't a single-character string.
Any solution for this?
This is a case where you'll want to combine a few M library functions together.
You'll want to use Text.Contains many times against a list, which is a good case for List.Transform. List.AnyTrue will tell you if any string matched.
List.AnyTrue(List.Transform({"dog","string","bark"}, (substring) => Text.Contains("This is a test string", substring)))
If you wished that there was a Text.ContainsAny function, you can write it!
let
Text.ContainsAny = (string as text, list as list) as logical =>
List.AnyTrue(List.Transform(list, (substring) => Text.Contains(string, substring))),
Invoked = Text.ContainsAny("This is a test string", {"dog","string","bark"})
in
Invoked
Another simple solution is this:
List.ContainsAny(Text.SplitAny("This is a test string", " "), {"dog","string","bark"})
It transforms the text into a list because there we find a function that does what you need.
If it's a specific (static) list of matches, you'll want to add a custom column with an if then else statement in PQ. Then use a filter on that column to keep or remove the columns. AFAIK PQ doesn't support regex so Alexey's solution won't work.
If you need the lookup to be dynamic, it gets more complicated... but doable you essentially need to
have an ID column for the original row.
duplicate the query so you have two queries, then in the newly created query
split the text field into separate columns, usually by space
unpivot the newly created columns.
get the list of intended names
use list.generate method to generate a list that shows 1 if there's a match and 0 if there isn't.
sum the values of the list
if sum > 0 then mark that row as a match, usually I use the value 1 in a new column. Then you can filter the table to keep only rows with value 1 in the new column. Then group this table on ID - this is the list of ID that contain the match. Now use the merge feature to merge in the first table ensuring you keep only rows that match the IDs. That should get you to where you want to be.
Thanks for giving me the lead. In my own case I needed to ensure two items exist in a string hence I replaced formula as:
List.AllTrue(List.Transform({"/","2017"},(substring) => Text.Contains("4/6/2017 13",substring)))
it returned true perfectly.
You can use regex here with logical OR - | expression :
/dog|string|bark/.test("This is a test string") // retruns true
I'm creating a ticketing system and I want to populate a table for people to view all of the open tickets. I want them to be sorted by priority, from critical --> high --> medium --> low.
I'm reusing code from a test database program I made before, which ordered entries in a table by "last name."
while(currentRow < rows){
qry.prepare("SELECT * FROM users ORDER BY lastname LIMIT :f1, :f2");
qry.bindValue(":f1", currentRow);
qry.bindValue(":f2", currentRow+1);
qry.exec();
qry.next();
currentCol = 0;
//this loop will populate all columns in the current row
while(currentCol < ui->table->columnCount()){
QTableWidgetItem *setdes = new QTableWidgetItem;
setdes->setText(qry.value(currentCol).toString());
ui->table->setItem(count, currentCol-1, setdes);
currentCol++;
}
currentRow++;
}
Obviously I can't just change the query from lastname to priority, because they would be sorted alphabetically instead of the order that I want them to be sorted in. Is there a way I can execute an SQL query to sort them in the order I provided above, or am I going to have to populate the table and then sort it myself?
You can use a CASE WHEN ... THEN construct to map your strings to numeric values to sort on, e.g.
... ORDER BY CASE
WHEN priority='critical' THEN 1
WHEN priority='high' THEN 2
WHEN priority='medium' THEN 3
WHEN priority='low' THEN 4
ELSE 5
END
In one spreadsheet I have 3 columns with a first and last name of a person combined. In the 2nd spreadsheet, I have column a = first name and column b = last name.
I want to know which names in spreadsheet one cannot be found in spreadsheet two. I also need to verify the data to make sure that the formula was accurate on finding the correct lookup.
Do I have to combine my columns in spreadsheet 2 to make the first and last name in the same column to make this work?
Which formula would you use for either scenario?
Use this:
=ISNA(MATCH($A1&" "&$B1,Sheet2!$A:$A,FALSE)))
Where (in order):
A1 is the first name column in Sheet1
B1 is the last name column in Sheet1
Sheet2 is the sheet that has the data stored as names separately
$A:$A is the rows that have the two names together
FALSE is because it's an exact match
This will return FALSE if the element does not exist, and TRUE if it does
You can also use:
=VLOOKUP($A1&" "&$B1,Sheet2!$A:$D,3,FALSE)
If you want to retrieve data for a match.
Finally, if you need to do your lookups the other way, take a look at this thread for some ideas on how to split the string into two pieces.