Power Query: Index adjustment solution - excel

I have multiple tables with index column and some matching ids. I need to combine all tables in one with adjusted index by applying ratio between matching ids.
The first step (the yellow one) is simple: we multiply Table2 index on ratio of fist 2 initial tables. The hard part is the next step (the reddish one): we need to find ratio between matching id of Table3 and the previously adjusted id of Table2.
Is there a creative way you can make this in Power Query?
See image below:
Thanks!

The red index is simply
(100/82)*(88/100) = 88/82 = 1.07317
You can continue this pattern with more tables. For example, with five tables, your last index would be:
(Index of Max Table1 id)/(Index of Min Table2 id) *
(Index of Max Table2 id)/(Index of Min Table3 id) *
(Index of Max Table3 id)/(Index of Min Table4 id) *
(Index of Max Table4 id)/(Index of Min Table5 id)

Related

Weighted Average in Excels pivot table

I have a table with 4 columns. Variable1, Variable2, Kpi1 and Kpi2.
Variable1 is one level above Varible2 (i.e, variable1 is the parent of variable2).
Kpi1 is an integer and Kpi2 is a float [ range (0,1) ].
When making a pivot table, variable2 looks fine with its values, but the column total (variable1) doesn't. Kpi2 can't be calculated as a simple sum or a simple average of its values of variable2. It needs to be a weighted average of it using kpi1.
To make it more clear I will leave here an example I did on Excel.
Is there any form I can achieve this?
You will need to add a helper column & a calculated field to pivot table to do this.
New Table Column = Product = kp1 * kp2
Calculated Field = Weight = Product / kp1
You can add or remove fields from the pivot table once completed

SQL - Insert a row into table only if not exists and count is less than a threshold

Other similar questions deal these two problems separately, but I want to merge them in a single statement. I'm using Python3 with psycopg2 library on a PostgreSQL database.
I have a table with 3 fields: ID, name, bool (BIT)
I want to insert a row in this table only if does not exists an other row with same 'name' and 'bool' = 0 and the total count of rows with same ID is less than a given threshold.
More specific my table should contain at most a given threshold number of rows with same ID. Those rows can have the same 'name', but only one of those rows with same ID and same 'name' can have 'bool'= 0.
I tried with this:
INSERT INTO table
SELECT 12345, abcdf , 0 FROM table
WHERE NOT EXISTS(
SELECT * FROM table
WHERE ID = 12345 AND name = abcdf AND bool = 0)
HAVING (SELECT count(*) FROM table
WHERE ID = 12345) < threshold
RETURNING ID;
but the row is inserted anyway.
Then I tried the same statement replacing 'HAVING' with 'AND', but it insert all the threshold rows together.

How do I select everything where two columns contain equal values in CQL?

I'm trying to select everything where two columns contain equal values. Here is my CQL query:
select count(someColumn) from somekeySpace.sometable where columnX = columnY
This doesn't work. How can I do this?
You can't query like that, cassandra don't support it
You can do this in different way.
First you have to create a separate counter table.
CREATE TABLE match_counter(
partition int PRIMARY KEY,
count counter
);
At the time of insertion into your main table if columnX = columnY then increment the value here. Though you have only a single count, you can use a static value of partition
UPDATE match_counter SET count = count + 1 WHERE partition = 1;
Now you can get the count of match column
SELECT * FROM match_counter WHERE partition = 1;

How to have a measure lookup a value based on the row and column context?

I need to Aggregate a number of multiplications which are based on the Row and Columns context. My best attempt at describing this is in pseudo-code.
For each cell in the Pivot table
SUM
Foreach ORU
Percent= Look up the multiplier for that ORU associated with the Column
SUMofValue = Add all of the Values associated with that Column/Row combination
Multiply Percent * SUMofValue
I tried a number of ways over the last few days and looked at loads of examples but am missing something.
Specifically, What won't work is:
CALCULATE(SUM(ORUBUMGR[Percent]), ORUMAP)*CALCULATE(SUM(Charges[Value]), ORUMAP)
because you're doing a sum of all the Percentages instead of the sum of the Percentages which are only associated with MGR (i.e., the column context)
Link to XLS
One way of doing that is by using nested SUMX. Add this measure to ORUBUMGR:
ValuexPercent :=
SUMX (
ORUBUMGR,
[PERCENT]
* (
SUMX (
FILTER ( CHARGES, CHARGES[ORU] = ORUBUMGR[ORU] ),
[Value]
)
)
)
For each row in ORUBUMGR you will multiply percent by ....
the sum of value for each row in Charges where ORUBUMGR ORU is the same as Charges ORU. Then you sum that product.

How to compute median value of sales figures?

In MySQL while there is an AVG function, there is no Median. So I need to create a way to compute Median value for sales figures.
I understand that Median is the middle value. However, it isn't clear to me how you handle a list that isn't an odd numbered list. How do you determine which value to select as the Median, or is further computation needed to determine this? Thanks!
I'm a fan of including an explicit ORDER BY statement:
SELECT t1.val as median_val
FROM (
SELECT #rownum:=#rownum+1 as row_number, d.val
FROM data d, (SELECT #rownum:=0) r
WHERE 1
-- put some where clause here
ORDER BY d.val
) as t1,
(
SELECT count(*) as total_rows
FROM data d
WHERE 1
-- put same where clause here
) as t2
WHERE 1
AND t1.row_number=floor(total_rows/2)+1;

Resources