How to divide values using the total of a column - powerbi

Below is a small snippet of my data. Now i want to create a new column %ofSales that takes the total of every sub_group_name and divides it with every value in the column of that sub group.
For example, the first value of the new column would be
(303370.88/186391.46)*100

Lets use this example data
category,subcategory,sales
1, a, 2
1, b, 4
1, c, 5
1, d, 6
2, a, 3
2, b, 4
2, c, 2
2, d, 5
Add calculated columns
catSales =
var temp = Table2[category]
return CALCULATE(sum(Table2[sales]),all(Table2),Table2[category]=temp)
%ofSales = Table2[sales]/Table2[catSales]
Or use equivalent measures instead.
Result:
category,subcategory,sales,catSales,%ofSales
1, a, 2, 17, 0.11764705882352941
1, b, 4, 17, 0.23529411764705882
1, c, 5, 17, 0.29411764705882354
1, d, 6, 17, 0.35294117647058826
2, a, 3, 14, 0.21428571428571427
2, b, 4, 14, 0.2857142857142857
2, c, 2, 14, 0.14285714285714285
2, d, 5, 14, 0.35714285714285715
Edit: equivalent measure:
%ofSales2 =
var temp = FIRSTNONBLANK(Table2[category],1)
return
SUM(Table2[sales])/
CALCULATE(sum(Table2[sales]),
ALLSELECTED(Table2),
Table2[category]=temp)

Related

How to give a ranking to rows based on multiple criteria against weighted factors?

I'm setting up an excel file that lets you input values, and then it will provide a ranking of 1 to 5 on each value, and then compare all values based on a set of weighted values.
It is a business problem that I'm struggling with because I somehow need to devise a ranking system that needs to take into account all factors to decide which of the options I want to do is the best one.
However, some of the factors require a different arrangement of the IF function. Because for some of the column, the higher the value = the lower the ranking and vice versa.
I have already set the criteria for rankings 1 to 5 of each factor.
Example: the higher the number of prospects, the higher the ranking. the higher the cost per conversion, the lower the ranking. And so on. Below is an example of the code that I have made: It differs for each column (I will provide a link to the file)
=IF(D7<1," ",IF(D7<=500,"1",IF(D7<=2500,"2",IF(D7<=3750,"3",IF(D7<=5000,"4",IF(D7>5000,"5"))))))
=IF(E7<1," ",IF(E7<=250,"1",IF(E7<=499,"2",IF(E7<=999,"3",IF(E7<=2500,"4",IF(E7>2500,"5"))))))
=IF(F7>7,"1",IF(F7>5,"2",IF(F7>3,"3",IF(F7>1,"4",IF(F7=1,"5",IF(F7<1," "))))))
=IF(G7>29,"1",IF(G7>19,"2",IF(G7>9,"3",IF(G7>4,"4",IF(G7>0.1,"5",IF(G7=0," "))))))
=IF(H7<1," ",IF(H7<=500000,"1",IF(H7<=1200000,"2",IF(H7<=2500000,"3",IF(H7<=4000000,"4",IF(H7>4000000,"5"))))))
So now we have a new table which includes all the rankings 1 to 5. But, I have not (or will have to) somehow put the weighted factors into the equation to consider the weights (as this will really affect the final ranking results).
This is the part that I struggle the most with, I could not find a function that somehow factors in the weighted values in order to give an overall value.
Thank you in advance for anyone who is able to help me with this.
for rank only you can use in L1 cell:
=ARRAYFORMULA(RANK(D5:D9, D5:D9, 1))
based on D11:H18 rules it will look like:
then to account for weight and rules in L12:L16 you will get score per column like:
=ARRAYFORMULA(RANK(D5:D9,D5:D9,1)*L3*
IF(D5:D9< 1, ,
IF(D5:D9<=500, 1,
IF(D5:D9<=2500, 2,
IF(D5:D9<=3750, 3,
IF(D5:D9<=5000, 4,
IF(D5:D9> 5000, 5)))))))
and then by summing scores per row, you will get a master score which you can rank like:
=ARRAYFORMULA(RANK(R21:R25, R21:R25))
spreadsheet demo
delete everything in L5:S9 range
paste this in L5 cell:
=ARRAYFORMULA({L3*
IF(D5:D9< 1, ,
IF(D5:D9<=500, 1,
IF(D5:D9<=2500, 2,
IF(D5:D9<=3750, 3,
IF(D5:D9<=5000, 4,
IF(D5:D9> 5000, 5)))))),
M3*
IF(E5:E9< 1, ,
IF(E5:E9<=250, 1,
IF(E5:E9<=499, 2,
IF(E5:E9<=999, 3,
IF(E5:E9<=2500, 4,
IF(E5:E9> 2500, 5)))))),
N3*
IF(F5:F9< 1, ,
IF(F5:F9> 7, 1,
IF(F5:F9> 5, 2,
IF(F5:F9> 3, 3,
IF(F5:F9> 1, 4,
IF(F5:F9= 1, 5)))))),
O3*
IF(G5:G9= 0, ,
IF(G5:G9> 29, 1,
IF(G5:G9> 19, 2,
IF(G5:G9> 9, 3,
IF(G5:G9> 4, 4,
IF(G5:G9> 0.1, 5)))))),
P3*
IF(H5:H9< 1, ,
IF(H5:H9<=500000, 1,
IF(H5:H9<=1200000, 2,
IF(H5:H9<=2500000, 3,
IF(H5:H9<=4000000, 4,
IF(H5:H9> 4000000, 5))))))})
paste this in R5 cell:
=ARRAYFORMULA(L5:L9+M5:M9+N5:N9+O5:O9+P5:P9)
paste this in S5 cell:
=ARRAYFORMULA(RANK(R5:R9, R5:R9))
spreadsheet demo
or alternative (if you don't want to alter L5:P9 range):
=ARRAYFORMULA({L3*
IF(D5:D9< 1, ,
IF(D5:D9<=500, 1,
IF(D5:D9<=2500, 2,
IF(D5:D9<=3750, 3,
IF(D5:D9<=5000, 4,
IF(D5:D9> 5000, 5))))))+
M3*
IF(E5:E9< 1, ,
IF(E5:E9<=250, 1,
IF(E5:E9<=499, 2,
IF(E5:E9<=999, 3,
IF(E5:E9<=2500, 4,
IF(E5:E9> 2500, 5))))))+
N3*
IF(F5:F9< 1, ,
IF(F5:F9> 7, 1,
IF(F5:F9> 5, 2,
IF(F5:F9> 3, 3,
IF(F5:F9> 1, 4,
IF(F5:F9= 1, 5))))))+
O3*
IF(G5:G9= 0, ,
IF(G5:G9> 29, 1,
IF(G5:G9> 19, 2,
IF(G5:G9> 9, 3,
IF(G5:G9> 4, 4,
IF(G5:G9> 0.1, 5))))))+
P3*
IF(H5:H9< 1, ,
IF(H5:H9<=500000, 1,
IF(H5:H9<=1200000, 2,
IF(H5:H9<=2500000, 3,
IF(H5:H9<=4000000, 4,
IF(H5:H9> 4000000, 5))))))})

Question about Eigen::MatrixXd column-wise calculation

Is there anyway to apply the column-wise calculation as follows?
(each column divided by the last entry of the column)
Eigen::MatrixXd A(3,5), B(3,5);
A << 1, 4, 9, 16, 25,
2, 4, 6, 8, 10,
1, 2, 3, 4, 5;
B = (A.col) / (A.bottomerows<1>).col;
and B would be:
B = 1, 2, 3, 4, 5,
2, 2, 2, 2, 2,
1, 1, 1, 1, 1;
The functions you are looking for are .hnormalized() and .homogeneous(). Both can be applied .colwise() like this:
Eigen::MatrixXd B = A.colwise().hnormalized().colwise().homogeneous();
You can achieve the same with some .replicate() magic like this:
Eigen::MatrixXd B = A.array() / A.row(2).replicate(A.rows(),1).array();
(if A was an ArrayXXd, instead of a MatrixXd, you don't need to write the .array())

Mathematica: How to combine lists of unequal length into single data structure

I have multiple lists, for instance:
a = {1, 2, 3};
b = {4, 5, 6, 7, 8};
I would like to combine these lists so that I can export a datafile of the following format:
a, b
1, 4
2, 5
3, 6
, 7
, 8
I can't for the life of me figure it out.

How to add sum of rows of a matrix into vector?

Since the question would be a bit long, ill add that here, I also want to add a row in a vector to the Finald vector.
MatrixXf ProdA(7, 7);;
VectorXf Intd(7);
VectorXf Finald(7);
ProdA <<
7, 5, 1, 9, 11, 2, 0,
5, 2, 8, 3, 11, 3, 3,
3, 9, 0, 1, 3, 1, 7,
6, 0, 1, 9, 11, 33, 3,
3, 5, 3, 3, 4, 3, 3,
3, 9, 1, 1, 0, 1, 15,
6, 2, 6, 2, 5, 12, 3,
Intd << 4, 5, 2, 12, 4, 1, 6;
Finald << 0, 0, 0, 0, 0, 0, 0;
for (int i = 0; i < 7; i++){
Finald.row(i) += ProdA.rowwise().sum();
Finald.row(i) += Intd.row(i);
}
So far this is what I have got. Obviously I get an error if I put i in rowwise. So as an example, I want to add the first row of ProdA , and the first number of Intd into the first space in the Finald vector, and then loop through every row of ProdA and Intd, and sum them all into Finald.
Thanks in advance!
I'm not 100% certain that I correctly understand your problem, but the way I understood it, this should work:
VectorXf ones(7);
ones << 1, 1, 1, 1, 1, 1, 1;
Finald = ProdA * ones + Intd;
I'm not sure if your matrix library (which seems to be Eigen) stores vectors as row or column vectors. So you might have to use ones.transpose() instead.

Trying to simulate python combinations in C++ with next_permutation

I need to port a snippet written in Python to C++
but that snippet is using combinations from itertools in python.
The line that I'm really interested to porting over to C++ is this one:
for k in combinations(range(n-i),2*i):
range(n-i) in Python will generate a list from 0 to (n-i) - 1
Let n = 16, i = 5
print range(n-i)
outputs:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
and python combinations will generate all possible combinations in that list.
e.g.
print list(combinations(range(n-i),2*i))
outputs:
[(0, 1, 2, 3, 4, 5, 6, 7, 8, 9),
(0, 1, 2, 3, 4, 5, 6, 7, 8, 10),
(0, 1, 2, 3, 4, 5, 6, 7, 9, 10),
(0, 1, 2, 3, 4, 5, 6, 8, 9, 10),
(0, 1, 2, 3, 4, 5, 7, 8, 9, 10),
(0, 1, 2, 3, 4, 6, 7, 8, 9, 10),
(0, 1, 2, 3, 5, 6, 7, 8, 9, 10),
(0, 1, 2, 4, 5, 6, 7, 8, 9, 10),
(0, 1, 3, 4, 5, 6, 7, 8, 9, 10),
(0, 2, 3, 4, 5, 6, 7, 8, 9, 10),
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)]
I want to generate similar output using std::vector and next_permutation from C++ but I'm still getting erroneous results. This is my current approach:
for(int j = 0; j < n-i; j++) {
temp_vector.push_back(j);
}
That snippet is equivalent to range(n-i) in Python.
But the following snippet:
do {
myvector.push_back(temp_vector);
} while(next_permutation(temp_vector.begin(),temp_vector.begin()+2*i));
cout<<myvector.size()<<endl;
Is not equivalent to combinations(range(n-i),2*i)) in Python, and I've tried many variations and still haven't been able to come up with the results I'm expecting.
For example:
Let n = 16
i = 5
Python
>>> print len(list(combinations(range(n-i),2*i)))
11
C++
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> temp_vector;
vector< vector<int> > myvector;
int n = 16, i = 5;
for(int j = 0; j < n - i; j++) {
temp_vector.push_back(j);
}
do {
myvector.push_back(temp_vector);
} while(next_permutation(temp_vector.begin(), temp_vector.begin()+2*i));
cout<<myvector.size()<<endl;
return 0;
}
g++ combinations.cpp
./a.out
3628800
Any guidance will be greatly appreciated! Thanks a lot!
combinations and permutations are not the same thing.
A combination is an unordered list of a subset of the items from another set. A permutation is a unique order of the items in the list.
You're generating all combinations of 10 things from a list of 11 things, so you'll get 11 results, each one missing a different one of the original 11 items.
Generating every permutation will generate every unique order of the original 11 items. Since the items in this case are all unique that means the result would be 11! lists where each contains all 11 items. You're only generating permutations from the first 10 items however, so you're getting 10! lists, none of which contain the 11th item.
You need to find an algorithm for generating combinations instead of permutations.
There's no built-in algorithm for combinations. std::next_permutation can be used as part of an algorithm to generate combinations: See Generating combinations in c++.
Here's an old draft proposal for algorithms for combinations, including code.