As the title states, I am trying to do a merge of 2 tables. I want a nested joint where the values from the first table are always there and rows matching the second table are added to the first. I believe this is known as the nested join.
Unfortunately, it only allows for 1 key to 1 key matching where as I need it for 1 key in table 1 to 2 keys in table 2
Here is an example
Table1:
Group
..
..
Time
Date
Table2:
Group 1
Group 2
..
..
..
Other Info
What I want is where "Group = Group 1 OR Group = Group 2" and display the matching row from table 2 nested into Table 1
I looked at the following example but I must be confused by the syntax because it doesn't seem to be working for me.
How to join two tables in PowerQuery with one of many columns matching?
So after further investigation of the answer post I linked earlier, I will add an explanation of it here:
Table.AddColumn(Source, "Name_of_Column",
(Q1) => Table.SelectRows(Query2,
each Q1[Col_from_q1] = [Col_from_q2] or Q1[Col_from_q1] = [2_Col_from_q2]
)
)
So this did work for me and it adds an extra column that needs to be expanded to get all the values from the table. What i would add is that I don't know / haven't tested if there are multiple matches and how it treats it, based on nestedjoin, I would assume that it will duplicate rows in the first table.
Related
Below is part of my table schema.
my table schema
I can use statement select factor.column(2) as code from factor to get the second column.
I wonder if I can SELECT the 2nd column, and the 4th to the last column.
Using metaprogramming is a good choice.
Get the column names of the table first using function columnNames.
Access column names by index and join them.
Create and execute a SQL statement using function sql and eval with the metacode generated by function sqlCol.
factor=table(2015.01.15 as date,`00000.SZ as code,-1.05 as factor_value,1.1 as factor01,1.2 as factor02)
colNames = factor.columnNames()
finalColNames = colNames[1] join colNames[3:]
sql(sqlCol(finalColNames), factor).eval()
code factor01 factor02
-------- -------- --------
00000.SZ 1.1 1.2
I have a SharePoint list as a datasource in Power Query.
It has a "AttachmentFiles" column, that is a table, in that table i want the values from the column "ServerRelativeURL".
I want to split that column so each value in "ServerRelativeURL"gets its own column.
I can get the values if i use the expand table function, but it will split it into multiple rows, I want to keep it in one row.
I only want one row per unique ID.
Example:
I can live with a fixed number of columns as there are usually no more than 3 attachments per ID.
I'm thinking that I can add a custom column that refers to "AttachmentFiles ServerRelativeURL Value(1)" but I don't know how.
Can anybody help?
Try this code:
let
fn = (x)=> {x, #table({"ServerRelativeUrl"},List.FirstN(List.Zip({{"a".."z"}}), x*2))},
Source = #table({"id", "AttachmentFiles"},{fn(2),fn(3),fn(1)}),
replace = Table.ReplaceValue(Source,0,0,(a,b,c)=>a[ServerRelativeUrl],{"AttachmentFiles"}),
cols = List.Transform({1..List.Max(List.Transform(replace[AttachmentFiles], List.Count))}, each "url"&Text.From(_)),
split = Table.SplitColumn(replace, "AttachmentFiles", (x)=>List.Transform({0..List.Count(x)-1}, each x{_}), cols)
in
split
I manged to solve it myself.
I added 3 custom columns like this
CustomColumn1: [AttachmentFiles]{0}
CustomColumn2: [AttachmentFiles]{1}
CustomColumn3: [AttachmentFiles]{2}
And expanded them with only the "ServerRelativeURL" selected.
It would be nice to have a dynamic solution. But this will work fine for now.
In MS Excel I have one table with few values (i.e. 3, no fix length) and in second table these values has to be repeated lot of time (i.e. 7 times). I do not understand how to provide this with normal Excel formulas/functions.
Table 1:
ABC
Table 2 (target table):
ABCABCA
how stupid am I. Is used wrong name of function index: the correct is =INDEX(Table1!A$1:A$100; 1; MOD(ROW();COUNTA(Table1!A$1:A$100))+1)
and this is added in each row of table 2
I can rank my data with this formula, which groups by Year, Trust and ID, and ranks the Areas.
rankx(
filter(Table,
[Year]=earlier([Year])&&[Trust]=earlier([Trust])&&[ID]=earlier([ID])),
[Area], ,1,Dense)
This works fine - unless you have data where the same Area appears more than once in the same group, whereupon it gives all rows the rank of 1. Is there any way to force unique rank values? So two rows that have the same Area would be given the rank of 1 and 2 (in an arbitrary order)? Thank you for your time.
Assuming you don't have duplicate rows in your table, you can add another column as a tie-breaker in your expression.
Suppose your table has an additional column, [Name], that is distinct between your multiple [Area] rows. Then you could write your formula like this:
= RANKX(
FILTER(Table,
[Year] = EARLIER([Year]) &&
[Trust] = EARLIER([Trust]) &&
[ID] = EARLIER([ID])),
[Area] & [Name], , 1, Dense)
You can append as many columns as you need to get the tie-breaking done.
I have Query A and Query B, and both have data items level 1, level 2 and level 3.
And I have three joined queries using query A and query B as below.
Joined Query 1 ---- a.level 1 = b.level 1
Joined Query 2 ---- a.level 1 = b.level 1 and a.level 2 = b.level 2
Joined Query 3 ---- a.level 1 = b.level 1 and a.level 2 = b.level 2 and a.level 3 = b.level 3
When the user select level 1 on the prompt page, the report then use Joined Query 1 to retrieve data.
When the user select level 2 on the prompt page, the report then use Joined Query 2 to retrieve data.
When the user select level 3 on the prompt page, the report then use Joined Query 3 to retrieve data.
However, in this way I have to create 3 pages and 3 lists and use different Joined Queries. The maintenance effort is too high when a requirement change occurs cause I have to modify triple times.
Is there any idea to reuse query and page in this situation? I am wondering if there is conditional join functionality in the Cognos Report Studio?
I have a novel solution to your problem. Instead of creating three pages you can get away with one by manipulating your join columns.
Let's simplify your example to two cases determined by radio button:
You want to join on one column ([Level1])
You want to join on two columns ([Level1] & [Level2])
For the column you always want to join on, we don't change anything. For the second join column we create a new data item to be used just for the join. For this example, we'll call it 'Join2'. For the expression we put in a CASE statement (or if..then if you prefer):
CASE ?radioButton?
WHEN 1 THEN '1'
WHEN 2 THEN [Level2]
END
Create the same data item in both queries to be joined. Obviously, the names should be adjusted to match your columns. Also, I assumed the level was a string, thus the '1' above. It should match the type of the optional join column or you will get a type mismatch error. Change the join expression to join on this second column in addition to the [Level1] column you always want to join on.
Let's examine the effect.
If the user selects 1, the join condition will be:
a.[Level1] = b.[Level1] AND a.[Join2] = b.[Join2]
...but the effective join will be:
a.[Level1] = b.[Level1] AND a.'1' = b.'1'
We've rendered the second join condition superfluous, exactly what we want.
If the user selects 2, the join condition will be:
a.[Level1] = b.[Level1] AND a.[Join2] = b.[Join2]
...but the effective join will be:
a.[Level1] = b.[Level1] AND a.[Level2] = b.[Level2]
In this case, we enforce the second-level join condition.
This technique assumes an inner join is used. Additional join conditions can be added in a similar way.