Excel Count unique cells and display them in pivot column - powerquery

Could anyone help tell me how to do it in EXCEL? I could use pivot sql but not sure how to make it happen in Excel.
Here is initial data
ID
Type
1
Type1
1
Type2
2
Type1
3
Type1
3
Type2
4
Type2
5
Type1
5
Type2
New data layout
ID
Type1
Type2
1
Type1
Type2
2
Type1
3
Type1
Type2
4
Type2
5
Type1
Type2

In powerquery, it is called Pivot Column
Bring data into powerquery (data .. from table/range)
Right click Type column and duplicate column
Click select the new duplicate column
Transform .. Pivot Column ...
For values column pick Type then in Advanced...do not aggregate
File close and load
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Duplicated Column" = Table.DuplicateColumn(Source, "Type", "Type - Copy"),
#"Pivoted Column" = Table.Pivot(#"Duplicated Column", List.Distinct(#"Duplicated Column"[#"Type - Copy"]), "Type - Copy", "Type")
in #"Pivoted Column"

Related

Sorting and showing specific colums

I have data that I want to sort in my google sheets; but I want to show only specific columns and not others.
Here's an example :
Name
Type
HP
ATK
Hero1
type1
10
5
Hero2
type1
9
8
Hero3
type2
[null]
7
Hero4
type2
11
6
After a =SORT() in an other sheet; I'd like to have something like this :
Name
HP
Hero4
11
Hero1
10
Hero2
9
And with an other =SORT() have this :
Name
ATK
Hero2
8
Hero3
7
Hero4
6
Hero1
5
I tried with FILTER() But I can't manage to get rid of the Type column
Of course I don't want to modify the first table
But I could use sorted tables containing only the name and not wathever value was used to sort them
EDIT :
You can create a virtual array {A2:A,D2:D} and use
=SORT({A2:A,D2:D},2,0)
or even
=SORT({A2:A,C2:C},2,0)
(Do adjust the formula according to your ranges and locale)
In your case it would be:
=SORT({A2:A\D2:D};2;0)
One
=QUERY({A:D};"Select Col1,Col4 where Col1 <> '' Order by Col4 desc";1)
Two
=QUERY({A:D};"Select Col1,Col3 where Col1 <> '' Order by Col3 desc";1)
QUERY
use:
=SORT(FILTER({A2:A\C2:C}; C2:C<>""); 2; )
and:
=SORT(FILTER({A2:A\D2:D}; D2:D<>""); 2; )
pros: SORT with FILTER is faster and more reliable than QUERY that can eat your data
also, see how to convert commas to semicolons: https://stackoverflow.com/a/73767720/5632629

PowerQuery Table Transformation

I want to be able to make the first column in my table pair with all others, making a new row for every combination of the two.
I need to be able to turn this:
A | 1 | 2 | 3
B | 4 | 5 | 6
C | 6 | 7 | 9
Into this:
A | 1
A | 2
A | 3
B | 4
B | 5
B | 6
C | 7
C | 8
C | 9
Is there any way this can be done using just powerquery?
Just unpivot other columns:
unpivot = Table.UnpivotOtherColumns(Source, {"col1"}, "a", "b")[[col1],[b]]
Are they in separate columns? Then load into powerquery, right click first column, choose unpivot other columns.
Are they in a single column separated by |s? Then use below
Right click column, split column by delimiter, Select or enter delimeter --Custom-- |, Split at each occurrence of the delimiter, ignore advanced options
Remove the latter part of the query so that
= Table.SplitColumn(#"Changed Type", "Column1", Splitter.SplitTextByDelimiter("|", QuoteStyle.Csv), {"Column1.1", "Column1.2", "Column1.3", "Column1.4"}),
becomes
= Table.SplitColumn(#"Changed Type", "Column1", Splitter.SplitTextByDelimiter("|", QuoteStyle.Csv))
right Click first Column, unpivot other columns
right Click attribute column, remove column
Assuming data is in Table1 with no column headers or header column of Column1 then code is
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}}),
#"Split Column by Delimiter" = Table.SplitColumn(#"Changed Type", "Column1", Splitter.SplitTextByDelimiter("|", QuoteStyle.Csv)),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Split Column by Delimiter", {"Column1.1"}, "Attribute", "Value"),
#"Removed Columns" = Table.RemoveColumns(#"Unpivoted Other Columns",{"Attribute"})
in #"Removed Columns"

Power Query, row by row the sum of the next 3 values

I have a power query table, 1 column with integer values. In another column, the sum of the current row and the other 2 rows should be calculated row (cell) by row (cell). - In plain Excel, I calculate it like this:
B1: = SUM(B1:B3)
B2: = SUM(B2:B4)
B3: = SUM(B3:B5)
...
How can I solve this with Power Query? If an error occurs in the last 2 lines, this is negligible.
Thanks and regards
Guenther
Is this what you're looking for?
If you start with this as your Source table:
Then if you add a custom column set up like this:
You'll get this:
Here's the M code, loading it from a spreadsheet's workbook, where the data is in a table named Table1:
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Added Custom" = Table.AddColumn(Source, "Custom", each List.Sum(List.Range(Source[Column1],[Column1]-1,3)))
in
#"Added Custom"

UPDATE with JOIN syntax for Oracle Database

First, I execute the following SQL statements.
drop table names;
drop table ages;
create table names (id number, name varchar2(20));
insert into names values (1, 'Harry');
insert into names values (2, 'Sally');
insert into names values (3, 'Barry');
create table ages (id number, age number);
insert into ages values (1, 25);
insert into ages values (2, 30);
insert into ages values (3, 35);
select * from names;
select * from ages;
As a result, the following tables are created.
ID NAME
---------- ----------
1 Harry
2 Sally
3 Barry
ID AGE
---------- ----------
1 25
2 30
3 35
Now, I want to update increment the age of Sally by 1, i.e. set it to 31. The following query works fine.
update ages set age = age + 1 where id = (select id from names where name = 'Sally');
select * from ages;
The table now looks like this.
ID AGE
---------- ----------
1 25
2 31
3 35
I want to know if there is a way it can be done by joins. For example, I tried the following queries but they fail.
SQL> update ages set age = age + 1 from ages, names where ages.id = names.id and names.name = 'Sally';
update ages set age = age + 1 from ages, names where ages.id = names.id and names.name = 'Sally'
*
ERROR at line 1:
ORA-00933: SQL command not properly ended
SQL> update ages set age = age + 1 from names join ages on ages.id = names.id where names.name = 'Sally';
update ages set age = age + 1 from names join ages on ages.id = names.id where names.name = 'Sally'
*
ERROR at line 1:
ORA-00933: SQL command not properly ended
The syntax of the UPDATE statement is:
http://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_10007.htm
where dml_table_expression_clause is:
Please pay attention on ( subquery ) part of the above syntax.
The subquery is a feature that allows to perform an update of joins.
In the most simplest form it can be:
UPDATE (
subquery-with-a-join
)
SET cola=colb
Before update a join, you must know restrictions listed here:
https://docs.oracle.com/cd/B28359_01/server.111/b28286/statements_8004.htm
The view must not contain any of the following constructs:
A set operator
A DISTINCT operator
An aggregate or analytic function
A GROUP BY, ORDER BY, MODEL, CONNECT BY, or START WITH clause
A collection expression in a SELECT list
A subquery in a SELECT list
A subquery designated WITH READ ONLY
Joins, with some exceptions, as documented in Oracle Database Administrator's Guide
and also common rules related to updatable views - here (section: Updating a Join View):
http://docs.oracle.com/cd/B19306_01/server.102/b14231/views.htm#sthref3055
All updatable columns of a join view must map to columns of a
key-preserved table. See "Key-Preserved Tables" for a discussion of
key-preserved tables. If the view is defined with the WITH CHECK
OPTION clause, then all join columns and all columns of repeated
tables are not updatable.
We can first create a subquery with a join:
SELECT age
FROM ages a
JOIN names m ON a.id = m.id
WHERE m.name = 'Sally'
This query simply returns the following result:
AGE
----------
30
and now we can try to update our query:
UPDATE (
SELECT age
FROM ages a
JOIN names m ON a.id = m.id
WHERE m.name = 'Sally'
)
SET age = age + 1;
but we get an error:
SQL Error: ORA-01779:cannot modify a column which maps to a non key-preserved table
This error means, that one of the above restriction is not meet (key-preserved table).
However if we add primary keys to our tables:
alter table names add primary key( id );
alter table ages add primary key( id );
then now the update works without any error and a final outcome is:
select * from ages;
ID AGE
---------- ----------
1 25
2 31
3 35

Oracle Update Where Target Has Duplicates

Suppose I have the following tables
Target table
sales
ID ItemNum DiscAmt OrigAmt
1 123 20.00 NULL
2 456 30.00 NULL
3 123 20.00 NULL
Source Table
prices
ItemNum OrigAmt
123 25.00
456 35.00
I tried to update the OrigAmt in the Target Table using the OrigAmt in the Source Table using
UPDATE
( SELECT s.OrigAmt dests
,p.OrigAmt srcs
FROM sales s
LEFT JOIN prices p
ON s.ItemNum = p.ItemNum
) amnts
SET amnts.dests = amnts.srcs
;
but i get: ORA-01779: cannot modify a column which maps to a non key-preserved table
i also tried using the merge but i get: ORA-30926: unable to get a stable set of rows in the source tables
You cannot generally UPDATE the result of an arbitrary SELECT.
Single statement, assuming ItemNum is a primary key for prices:
UPDATE sales WHERE (SELECT count(price.ItemNum) FROM price
WHERE price.ItemNum = sales.ItemNum) > 0
SET OrigAmt =
(SELECT MAX(OrigAmt) FROM price
WHERE price.ItemNum = sales.ItemNum)
You might get away with omitting the WHERE and/or MAX.
Less convoluted: loop a cursor over
SELECT ItemNum, OrigAmt FROM price
performing a number of updates for each ItemNum from table prices:
UPDATE sales SET OrigAmt=? WHERE ItemNum=?

Resources