VBA RANDOM SELECTION BASED ON MAX VALUE - vba

Hello being facing an issue with the following :
I need to select a list of product based on max budget,
Data
Item 1 : 1 usd
Item 2 : 3 usd
Item 3 : 0.5 usd
Item 4 : 40 usd
Item 5 : 20 usd
Item 6 : 5 usd
Budget = 50 usd
The needed output : different list of product i can get where the sum of price is equal to or less than 50 usd,list can contain 1 products as well.
Any help will be much appreciated.
Language : Vba or M query

In powerquery: Create every combination. Price them. Filter amount as needed
function Combinations
(Items as list) as table =>
// Bill Szysz 2017, all combinations of items in list, blows up with too many items to process due to large number of combinations
let AddIndex = Table.AddIndexColumn(Table.FromList(Items), "Index", 0, 1),
ReverseIndeks = Table.AddIndexColumn(AddIndex, "RevIdx", Table.RowCount(AddIndex), -1),
Lists = Table.AddColumn(ReverseIndeks, "lists", each
List.Repeat(
List.Combine({
List.Repeat({[Column1]}, Number.Power(2,[RevIdx]-1)),
List.Repeat( {null}, Number.Power(2,[RevIdx]-1))
})
, Number.Power(2, [Index]))
),
ResultTable = Table.FromColumns(Lists[lists])
in ResultTable
used with code:
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Item", type text}, {"Amount", type number}}),
GetCombos = Combinations(#"Changed Type"[Item]),
#"Added Index" = Table.AddIndexColumn(GetCombos, "Index", 0, 1, Int64.Type),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Added Index", {"Index"}, "Attribute", "Value"),
#"Merged Queries" = Table.NestedJoin(#"Unpivoted Other Columns", {"Value"}, #"Changed Type", {"Item"}, "ct", JoinKind.LeftOuter),
#"Expanded ct" = Table.ExpandTableColumn(#"Merged Queries", "ct", {"Amount"}, {"Amount"}),
#"Group"= Table.Group(#"Expanded ct", {"Index"}, {
{"Concat", each Text.Combine([Value],", "), type text},
{"Cost", each List.Sum([Amount]), type number}}
),
#"Removed Columns" = Table.RemoveColumns(Group,{"Index"}),
#"Filtered Rows" = Table.SelectRows(#"Removed Columns", each [Cost] <= 50)
in #"Filtered Rows"

Related

MDX Sum for an ordered Set

I want to create a measure for the running total of sales.
The customer dimension need to be ordered by their sales amount.
Want I want to get:
H1
Sales
CumSales
B
10
10
C
3
13
A
2
15
Thank you!
If I use
CREATE MEMBER CURRENTCUBE.[Measures].[CumSales]
AS sum({null: [Kunden].[H1].CurrentMember}, [Measures].[ABSTCK In Mille]),
FORMAT_STRING = "Numeric",
NON_EMPTY_BEHAVIOR = { [ABSTCK In Mille] },
VISIBLE = 1 , DISPLAY_FOLDER = 'Calculations' , ASSOCIATED_MEASURE_GROUP = 'SAP Sales';
(H1 is the customer hierachy name and represents the customer)
I get:
H1
Sales
CumSales
A
2
2
B
10
12
C
3
15
What I also managed (and might be used?) is to create the ordered customer Set ("KundenOrderedByABSTCK") and create a measure for the order index ("KundenOrderedByABSTCK_RowNr")
CREATE DYNAMIC SET CURRENTCUBE.[KundenOrderedByABSTCK]
AS order([Kunden].[H1].[H1].Members,[Measures].[ABSTCK In Mille],DESC) ;
//works fine
//Ranking Nr.
CREATE MEMBER CURRENTCUBE.[Measures].[KundenOrderedByABSTCK_RowNr]
AS RANK([Kunden].[H1].CurrentMember, KundenOrderedByABSTCK),
FORMAT_STRING = "0",
NON_EMPTY_BEHAVIOR = { [ABSTCK In Mille] },
VISIBLE = 1 , DISPLAY_FOLDER = 'Calculations' , ASSOCIATED_MEASURE_GROUP = 'SAP Sales';

I have two tables to join and i need to substitute for {b} values of column a from another column named b

table err:
err id
err info
10001
has a decision of {decision} on the {tab} tab of the content
10002
has a decision of {decision} on the {tab} of the items
table main:
err id
tab
decision
id
10001
a
remove
1
10001
b
keep
2
I need the result table after join on err id to be like below: i.e UPDATE {decision} from the value in decision column and UPDATE {tab} from the value in tab column
err id
tab
decision
id
err info
10001
a
remove
1
has a decision of remove on the a tab of the content
10001
b
keep
2
has a decision of keep on the b of the items
import pandas as pd
err_data = {"errid": [10001, 10002],
"errinfo": ["has a decision of {decision} on the {tab} tab of the content",
"has a decision of {decision} on the {tab} of the items"],
}
table_data = {"errid": [10001, 10002],
"tab": ["a", "b"],
"decision": ["remove", "keep"],
"id": ["1", "2"],
}
df1 = pd.DataFrame(err_data)
df2 = pd.DataFrame(table_data)
# above can be replace by read csv
df3 = pd.merge(df1, df2, on=['errid'])
errinfo_new = []
for tup in zip(df3['decision'], df3['tab'], df3['errinfo']):
decision = tup[0]
tab = tup[1]
errinfo = tup[2]
errinfo = errinfo.replace("{decision}", decision).replace("{tab}", tab)
errinfo_new.append(errinfo)
df3 = df3.drop(columns=['errinfo'])
df3.insert(4, 'errinfo_new', errinfo_new)
print(df3)

How to find word frequency per country list in pandas?

Let's say I have a .CSV which has three columns: tidytext, location, vader_senti
I was already able to get the amount of *positive, neutral and negative text instead of word* pero country using the following code:
data_vis = pd.read_csv(r"csviamcrpreprocessed.csv", usecols=fields)
def print_sentiment_scores(text):
vadersenti = analyser.polarity_scores(str(text))
return pd.Series([vadersenti['pos'], vadersenti['neg'], vadersenti['neu'], vadersenti['compound']])
data_vis[['vadersenti_pos', 'vadersenti_neg', 'vadersenti_neu', 'vadersenti_compound']] = data_vis['tidytext'].apply(print_sentiment_scores)
data_vis['vader_senti'] = 'neutral'
data_vis.loc[data_vis['vadersenti_compound'] > 0.3 , 'vader_senti'] = 'positive'
data_vis.loc[data_vis['vadersenti_compound'] < 0.23 , 'vader_senti'] = 'negative'
data_vis['vader_possentiment'] = 0
data_vis.loc[data_vis['vadersenti_compound'] > 0.3 , 'vader_possentiment'] = 1
data_vis['vader_negsentiment'] = 0
data_vis.loc[data_vis['vadersenti_compound'] <0.23 , 'vader_negsentiment'] = 1
data_vis['vader_neusentiment'] = 0
data_vis.loc[(data_vis['vadersenti_compound'] <=0.3) & (data_vis['vadersenti_compound'] >=0.23) , 'vader_neusentiment'] = 1
sentimentbylocation = data_vis.groupby(["Location"])['vader_senti'].value_counts()
sentimentbylocation
sentimentbylocation gives me the following results:
Location vader_senti
Afghanistan negative 151
positive 25
neutral 2
Albania negative 6
positive 1
Algeria negative 116
positive 13
neutral 4
TO GET THE MOST COMMON POSITIVE WORDS, I USED THIS CODE:
def process_text(text):
tokens = []
for line in text:
toks = tokenizer.tokenize(line)
toks = [t.lower() for t in toks if t.lower() not in stopwords_list]
tokens.extend(toks)
return tokens
tokenizer=TweetTokenizer()
punct = list(string.punctuation)
stopwords_list = stopwords.words('english') + punct + ['rt','via','...','…','’','—','—:',"‚","â"]
pos_lines = list(data_vis[data_vis.vader_senti == 'positive'].tidytext)
pos_tokens = process_text(pos_lines)
pos_freq = nltk.FreqDist(pos_tokens)
pos_freq.most_common()
Running this will give me the most common words and the number of times they appeared, such as
[(good, 1212),
(amazing, 123)
However, what I want to see is how many of these positive words appeared in a country.
For example:
I have a sample CSV here: https://drive.google.com/file/d/112k-6VLB3UyljFFUbeo7KhulcrMedR-l/view?usp=sharing
Create a column for each most_common word, then do a groupby location and use agg to apply a sum for each count:
words = [i[0] for i in pos_freq.most_common()]
# lowering all cases in tidytext
data_vis.tidytext = data_vis.tidytext.str.lower()
for i in words:
data_vis[i] = data_vis.tidytext.str.count(i)
funs = {i: 'sum' for i in words}
grouped = data_vis.groupby('Location').agg(funs)
Based on the example from the CSV and using most_common as ['good', 'amazing'] the result would be:
grouped
# good amazing
# Location
# Australia 0 1
# Belgium 6 4
# Japan 2 1
# Thailand 2 0
# United States 1 0

I am stuck on my programming project

A large company pays its salespeople on a commission basis. The salespeople receive $200 per week plus 9% of their gross sales for that week. For example, a salesperson who sells $5000 worth of merchandise in a week receives $200 plus 9% of $5000, or a total of $650. Your program will allow the user to enter item numbers for each item sold by the salesperson for that week, print out the total of each item sold by the salesperson, and the total weekly earnings for that salesperson. The items and values are: Item 1 equals to 5.00, Item 2 equals to 10.00, Item 3 equals to 1.00, Item 4 equals to 25.00. Program should use JOptionPane for all input.
I have some programmed but I only get one input.
--- Update ---
//This is what I have so far
import javax.swing.JOptionPane;
public class KingRocker {
public static void main( String[]args )
{
double gross = 0.0, earnings;
int product = 0, number;
String input;
while( product < 4 ){
product++;
input = JOptionPane.showInputDialog("Enter the number of products sold #" + product + " ");
number = Integer.parseInt( input );
if (product == 1)
gross = gross + number * 5.00;
else if (product == 2)
gross = gross + number * 10.00;
else if (product == 3)
gross = gross + number * 1.00;
else if (product == 4)
gross = gross + number * 25.00;
earnings = 0.09 * gross + 200;
String result = "Week earnings: " + earnings;
JOptionPane.showMessageDialog(null, result, "Sales", JOptionPane.INFORMATION_MESSAGE );
System.exit( -1);
}
}
}
From what I can tell, you are calling System.exit( -1); before you run though the rest of your while loop. Try moving System.exit( -1); outside of the loop.
Try closing the loops here
if (product == 1)
gross = gross + number * 5.00;
else if (product == 2)
gross = gross + number * 10.00;
else if (product == 3)
gross = gross + number * 1.00;
else if (product == 4)
gross = gross + number * 25.00;
}
Doing so will allow the loop to run four times. Giving you what you ask for in the comments.

R equivalent of a SQL UPDATE query

I want to do the equivalent of an UPDATE query in SQL using R. If I have data like this:
# Example Data
df <- data.frame(x <- c("My Name", "My Name"), y <- c("AZ", "MI"))
colnames(df) <- c("Name", "State")
print(df)
> print(df)
Name State
1 My Name AZ
2 My Name MI
I'd like to do the R equivalent of this in SQL :
update df
set Name = 'My Name1'
where Name = 'My Name'
and State = 'MI
So the final output is:
> print(df)
Name State
1 My Name AZ
2 My Name1 MI
Any ideas?
Because you're using factors in your data frame, you are going to need to change the factor levels first using
levels(df$Name) <- c(levels(df$Name), "My Name1")
before actually changing the data frame using
df[df$Name == "My Name" & df$State == "MI", "Name"] <- "My Name1"
The syntax is:
df[logical condition OR rownumbers, column name or number] <- assignment
So you could do:
df[df$Name == "MyName" & df$State == "MI", "State"] <- "My Name1"