Power query merge tables any-to-any - excel

I new in MS Excel Power Query and i cant finde solution in google for this problem.
Join tables any-to-any row
Table1 Table2
+-----+ +-----+
| A | | 1 |
| B | | 2 |
+-----+ +-----+
Merge Table1 and Table2 to Table3
Table3
+-----+-----+
| A | 1 |
| A | 2 |
| B | 1 |
| B | 2 |
+-----+-----+

The link Hakan provided is great, so I'll just summarize it here.
Starting with your Table1, go to Add Column > Custom Colum and simply input Table2 as the formula.
Once that column is created, click the expand button and choose which columns from Table2 to expand.
This should result in the desired table.

Related

excel pivot table with unique data so don't weant to see the count but the actual data

I have a csv file like this
| Date | PO |
|21-04-21| A |
|21-04-21| B |
|21-04-21| C |
|22-04-21| B |
|22-04-21| C |
|23-04-21| B |
|23-04-21| D |
atm, when I'm making a pivot table in excel, I'm getting the count number
|21-04-21|22-04-21|23-04-21|
A | 1 | 0 | 0 |
B | 1 | 1 | 1 |
C | 1 | 1 | 0 |
D | 0 | 0 | 1 |
I'd like formatted this way since each row are unique:
|21-04-21|22-04-21|23-04-21|
| A | B | B |
| B | C | D |
| C | | |
Is it possible using a pivot table or I need another way to do so...
thks gorgeous people!!
You can add a measure to show the information.
Make a table and create pivot from the table.
Choose "Add this data to the Data Model". The table will be named Table1
Create a new measure for the pivot table.
Then enter the following DAX formula:
=CALCULATE(CONCATENATEX(VALUES(Table1[PO]),Table1[PO]," "))
Then enter move the field as the picture:

How to group by rollup on only some columns in Apache Spark SQL?

I'm using the the SQL API for Spark 3.0 in a Databricks 7.0 runtime cluster. I know that I can do the following:
select
coalesce(a, "All A") as colA,
coalesce(b, "All B") as colB,
sum(c) as sumC
from
myTable
group by rollup (
colA,
colB
)
order by
colA asc,
colB asc
I'd then expect an output like:
+-------+-------+------+
| colA | colB | sumC |
+-------+-------+------+
| All A | All B | 300 |
| a1 | All B | 100 |
| a1 | b1 | 30 |
| a1 | b2 | 70 |
| a2 | All B | 200 |
| a2 | b1 | 50 |
| a2 | b2 | 150 |
+-------+-------+------+
However, I'm trying to write a query where only column b needs to be rolled up. I've written something like:
select
a as colA,
coalesce(b, "All B") as colB,
sum(c) as sumC
from
myTable
group by
a,
rollup (b)
order by
colA asc,
colB asc
And I'd expect an output like:
+-------+-------+------+
| colA | colB | sumC |
+-------+-------+------+
| a1 | All B | 100 |
| a1 | b1 | 30 |
| a1 | b2 | 70 |
| a2 | All B | 200 |
| a2 | b1 | 50 |
| a2 | b2 | 150 |
+-------+-------+------+
I know this sort of operation is supported in at least some SQL APIs, but I get Error in SQL statement: UnsupportedOperationException when trying to run the above query. Does anyone know whether this behavior is simply as-of-yet unsupported in Spark 3.0 or if I just have the syntax wrong? The docs aren't helpful on the subject.
I know that I can accomplish this with union all, but I'd prefer to avoid that route, if only for the sake of elegance and brevity.
Thanks in advance, and please let me know if I can clarify anything.
Try this GROUPING SETS option:
%sql
SELECT
COALESCE( a, 'all a' ) a,
COALESCE( b, 'all b' ) b,
SUM(c) c
FROM myTable
GROUP BY a, b
GROUPING SETS ( ( a , b ), a )
ORDER BY a, b
My results (with updated numbers):

How to combine two columns into one in Sqlite and also get the underlying value of the Foreign Key?

I want to be able to combine two columns from a table into one column then to to be able to get the actual value of the foreign keys. I can do these things individually but not together.
Following the answer below I was able to combine the two columns into one using the first sql statement below.
How to combine 2 columns into a new one in sqlite
The combining process is shown below:
+---+---+
|HT | AT|
+---+---+
|1 | 2 |
|5 | 7 |
|9 | 5 |
+---+---+
into one column as shown:
+---+
|HT |
+---+
| 1 |
| 5 |
| 9 |
| 2 |
| 7 |
| 5 |
+---+
The second SQL statement show's the actual value of each foreign key corresponding to each foreign key id. The Foreign Key Table.
+-----+------------------------+
|T_id | TN |
+-----+------------------------+
| 1 | 'Dallas Cowboys |
| 2 | 'Chicago Bears' |
| 5 | 'New England Patriots' |
| 7 | 'New York Giants' |
| 9 | 'New York Jets' |
+-----+------------------------+
sql = "SELECT * FROM (SELECT M.HT FROM M UNION SELECT M.AT FROM Match)t"
The second sql statement lets me get the foreign key values for each value in M.HT.
sql = "SELECT M.HT, T.TN FROM M INNER JOIN T ON M.HT = T.Tid WHERE strftime('%Y-%m-%d', M.ST) BETWEEN \'2015-08-01\' AND \'2016-06-30\' AND M.Comp = 6 ORDER BY M.ST"
Result of second SQL statement:
+-----+------------------------+
| HT | TN |
+-----+------------------------+
| 1 | 'Dallas Cowboys |
| 5 | 'New England Patriots' |
| 9 | 'New York Jets' |
+-----+------------------------+
But try as I might I have not been able to combine these queries!
I believe the following will work (assuming that the tables are Match and T and baring the WHERE and ORDER BY clauses for brevity/ease) :-
SELECT DISTINCT(m.ht), t.tn
FROM
(SELECT Match.HT FROM Match UNION SELECT Match.AT FROM Match) AS m
JOIN T ON t.tid = m.ht
JOIN Match ON (m.ht = Match.ht OR m.ht = Match.at)
/* WHERE and ORDER BY clauses using Match as m only has columns ht and at */
WHERE strftime('%Y-%m-%d', Match.ST)
BETWEEN \'2015-08-01\' AND \'2016-06-30\' AND Match.Comp = 6
ORDER BY Match.ST
;
Note only tested without the WHERE and ORDER BY clause.
That is using :-
DROP TABLE IF EXISTS Match;
DROP TABLE IF EXISTS T;
CREATE TABLE IF NOT EXISTS Match (ht INTEGER, at INTEGER, st TEXT DEFAULT (datetime('now')));
CREATE TABLE IF NOT EXISTS t (tid INTEGER PRIMARY KEY, tn TEXT);
INSERT INTO T (tn) VALUES('Cows'),('Bears'),('a'),('b'),('Pats'),('c'),('Giants'),('d'),('Jets');
INSERT INTO Match (ht,at) VALUES (1,2),(5,7),(9,5);
/* Directly without the Common Table Expression */
SELECT
DISTINCT(m.ht), t.tn,
Match.st /*<<<<< Added to show results of obtaining other values from Matches >>>>> */
FROM
(SELECT Match.HT FROM Match UNION SELECT Match.AT FROM Match) AS m
JOIN T ON t.tid = m.ht
JOIN Match ON (m.ht = Match.ht OR m.ht = Match.at)
/* WHERE and ORDER BY clauses here using Match */
;
Noting that limited data (just the one extra column) was used for brevity
Results in :-

Select record from one column which are not in another column

Let's say I have 2 excel tabs (A) & (B):
TAB (A)
+----------+
|City |
+----------+
| Seattle |
| New York |
| Boston |
| Miami |
+----------+
TAB (B)
+------------+---------+
|City | Name |
+------------+---------+
| Seattle | Klay |
| Seattle | Walis |
| New York | Walis |
| Boston | Klay |
| Miami | John |
| New York | Klay |
+------------+---------+
I am trying to group them in order to obtain a new tab (result) where I have the list of city where people NEVER went group by name:
TAB (RESULT)
+------------+---------+
|Name | City |
+------------+---------+
| Klay | Miami |
|----------------------|
| Walis | Boston |
| | Miami |
|----------------------|
| John |Seattle |
| |New York |
| |Boston |
+------------+---------+
The only solution I came with was using a pivot table but I am looking for opposite result! I have also use Index & Match but it's not working.
Since you mentioned you are trying to do this in Excel, here's an Excel solution. Let's pretend you have your data setup all in one tab, like so:
In cell G2 and copied over and down is this formula:
=IF(COLUMN(A2)>ROWS($A$2:$A$5)-COUNTIF($D$2:$D$7,$F2),"",INDEX($A$2:$A$5,MATCH(1,INDEX((COUNTIFS($D$2:$D$7,$F2,$C$2:$C$7,$A$2:$A$5)=0)*(COUNTIF($F2:F2,$A$2:$A$5)=0),),0)))
You can cut and paste each section to a different tab if desired.
in sql server it would be something like this
--tsql
with tableC AS
(
SELECT
a.City
,b.name
FROM tableA a
cross join (select distinct name from tableB) b
)
SELECT
c.*
FROM tableC c
LEFT JOIN tableB b
ON c.City = b.City
AND c.name = b.name
WHERE b.city IS NULL
If this is indeed a MySQL problem, you need to get every combination of name and city, and then eliminate combinations that have visits.
SELECT bNames.Name, tableA.City
FROM (SELECT DISTINCT Name FROM tableB) AS bNames
CROSS JOIN tableA
WHERE (bNames.Name, tableA.City) NOT IN (SELECT Name, City FROM tableB)
ORDER BY bNames.Name, tableA.City
;
The result will not omit a repeated user name on successive entries, but that is something almost always better handled by post processing the results anyway.
One possible solution
Select b.name, a.city city_to_visit
From a join b on 1 = 1
Minus — some db use except
Select b.name, b.city city_visited
From b
Is this your desired ?
SELECT NAME,
CASE WHEN (SELECT
CITY
FROM TAB1) NOT IN
CITY
Then City
END CASE From Tab1 LEFT JOIN
TAB2 ON TAB1.CITY=Tab2.CITY
GROUP BY NAME;

Excel find all where col b = x

I have had this problem a little while and any offset etc is a bit messy and leaves gaps in what I'm attempting.
Sheet 1
+-------+------+
|Name | team |
+-------+------+
|Abel | a |
|Bravo | b |
|Charle | a |
|Delta | a |
|Echo | b |
+-------+------+
Sheet 2
+---------+
|Team a |
+---------+
|Abel |
|Charlie |
|Delta |
+---------+
+---------+
| Team b |
+---------+
|Bravo |
|Echo |
+---------+
I insert the names manually in Sheet 1, then locate the team from another spreadsheet using offset. Unfortunately they don't come in order and someone's team a will be 20 people and team b 5 - these values vary but not more than 30 total
Apologies for formatting I'm using my phone, sheet 1 is 2 column, sheet 2 is 1
Go for a pivot table and pivot chart.
For this dataset
and setting this
you would get the following, where you can filter on team

Resources