Power Query Data from different columns - excel

i am new to Power query and i would like to learn a bit more about it. I am facing the following problem. My table looks like this (empty fields already removed):
What i'm trying is to get a new table where "Spalte2" holds my list of ISINs and S^"Spalte 8" but also "Spalte 9" and "Spalte 10" hold my portfolio share (komma separated).
EDIT: For clarification I hope to get something like this:
EDIT: I try to get a table in here, hope it works:
Spalte1
Spalte2
Spalte8
Spalte9
Spalte10
Bâloise Holding AG
CH0012410517
1,04
Null
Null
Barry Callebaut AG
CH0009002962
0,63
Null
Null
Galenica AG
CH0360674466
0,58
Null
Null
Givaudan SA
CH0010645932
1,24
Null
Null
HelloFresh SE
DE000A161408
527.705,26
1,85
Null
Kering S.A.
FR0000121485
431.145,00
1,51
Null
Standard Chartered PLC
GB0004082847
4,610 117.699,50
Null
0,41
Unilever PLC
GB00B10RZP78
42,305 315.241,76
Null
1,11
What i'm trying to get is this:
Spalte2
Spalte8
CH0012410517
1,04
CH0009002962
0,63
CH0360674466
0,58
CH0010645932
1,24
DE000A161408
1,85
FR0000121485
1,51
GB0004082847
0,41
GB00B10RZP78
1,11
Which way can i use in PQ to match the ISIN with its portfolio share? Thanks a lot!
Thomas

Am I correct in understanding that you simply want to consolidate the information from the rightmost populated column of each row into one column, and disregard any other information between it and the first column?
If so, then this might be one possible approach.
Starting with a sample table called Table1 in power query:
I just add a new column and use if then statements to select the rightmost populated column's information:
(In the above M code, I check that each column is both not null and not blank, to be thorough.)
I get this result:
Then I select the Spalte2 and Custom columns and remove the other columns to get this:

Related

I want to create a computed column based off a substring of another column in SQL

I have a column called TAG_
Data in the TAG_ column could like the below:
STV-123456
TV-12456
ME-666666
I want to create two computed columns
One that shows the first part of TAG_ before the hyphen
STV
TV
ME
One that shows the second part of TAG_ after the hyphen
123456
12456
666666
This shouldn't be hard but the light bulb is not on yet. Please help.
try this:
SELECT SUBSTRING(TAG_ ,0,CHARINDEX('-',TAG_ ,0)) AS before,
SUBSTRING(TAG_ ,CHARINDEX('-',TAG_ ,0)+1,LEN(TAG_ )) AS after from testtable
and the result:
Hope this helps!
Example for MySQL, syntax is likely different for other vendors:
create table t
( tag_ text not null
, fst text generated always as (substr(tag_, 1, locate('-', tag_)-1)) stored
, snd text generated always as (substr(tag_, locate('-', tag_)+1)) stored
);
Fiddle

Power Query excel

i am trying to use a if formula in power query. For example, if the Column contains “guy” then value is male and the false value is “female”. I tried different ways and I can’t find the right formula to use in power query. Can anyone help me please?
If you are entering this into the Add Custom Column dialog, something like (for case-insensitive):
= if Text.Contains([Column],"guy", Comparer.OrdinalIgnoreCase) then "male" else "female"
It sounds like you are wanting to conditionally replace values in a column, based on the existing values in that column? If so, you can use the Table.ReplaceValue function in Power Query:
= Table.ReplaceValue(Source,
each [Gender],
each if [Gender] = "guy" then "male" else "female",
Replacer.ReplaceText,{"Gender"})
That will change all values of "guy" to "male', and ALL other values to "female", as you stated.
You can also leave values in place that don't meet the criteria, by simply referencing the column name instead of a specifying a new value:
= Table.ReplaceValue(Source,
each [Gender],
each if [Gender] = "guy" then "male" else [Gender],
Replacer.ReplaceText,{"Gender"})
Create a table with a column called Gender, and load it to power query. Right-click on the column header and choose Replace Values to get the UI to build your statement for you, then replace the generated code with the above modification(s) and apply to your actual requirements. The key is using the each expression to tell Power Query to test at the row value level. If you omit each, you'll see the error:
"Expression.Error: There is an unknown identifier. Did you use the [field] shorthand for a _[field] outside of an 'each' expression?"
= Table.AddColumn(#"Changed Type","ColumnName",each if[Column] ="""Guy""" then"""Male""" else"""Female""")

Excel Power Query - from web with dynamic worksheet cell value

We have a spreadsheet that gets updated monthly, which queries some data from our server.
The query url looks like this:
http://example.com/?2016-01-31
The returned data is in a json format, like below:
{"CID":"1160","date":"2016-01-31","rate":{"USD":1.22}}
We only need the value of 1.22 from the above and I can get that inserted into the worksheet with no problem.
My questions:
1. How to use a cell value [contain the date] to pass the date parameter [2016-01-31] in the query and displays the result in the cell next to it.
2. There's a long list of dates in a column, can this query be filled down automatically per each date?
3. When I load the query result to the worksheet, it always load in pairs. [taking up two cells, one says "Value", the other contains the value which is "1.22" in my case]. Ideally I would only need "1.22", not the title, can this be removed? [Del won't work, will give you a "Column 1" instead, or you have to hide the entire row which will mess up with the layout].
I know this is a lot to ask but I've tried a lot of search and reading in the last few days and I have to say the M language beats me.
Thanks in advance.
Convert your Web.Contents() request into a function:
let
myFunct = ( param as date ) => let
x = Web.Contents(.... & Date.ToText(date) & ....)
in
x
in
myFunct
Reference your data request function from a new query, include any transformations you need (in this case JSON.Document, table expansions, remove extraneous data. Feel free to delete all the extra data here, including columns that just contain the label 'value'.
(assuming your table of domain values already exists) add a custom column like
=Expand(myFunct( [someparameter] ))
edit: got home and got into my bookmarks. Here is a more detailed reference for what you are looking to do: http://datachix.com/2014/05/22/power-query-functions-some-scenarios/
For a table - Add column where you get data and parse JSON
let
tt=#table(
{"date"},{
{"2017-01-01"},
{"2017-01-02"},
{"2017-01-03"}
}),
add_col = Table.AddColumn(tt, "USD", each Json.Document(Web.Contents("http://example.com/?date="&[date]))[rate][USD])
in
add_col
If you need only one value
Json.Document(Web.Contents("http://example.com/?date="&YOUR_DATE_STRING))[rate][USD]

Filter based on existence in one table and non-existence in another

I have the following data model:
Record: Id, ..., CreateDate
FactA: RecordId, CreateDate
FactB: RecordId, CreateDate
Relationships exist from FactA to Record and FactB to Record.
I've written measures on Records such as this with no issues:
FactA's:=CALCULATE(DISTINCTCOUNT(Records[Id]), FactA)
FactB's:=CALCULATE(DISTINCTCOUNT(Records[Id]), FactB)
Now I'd like a count of Records with FactA but no FactB, in SQL I'd do a LEFT JOIN WHERE FactB.RecordId IS NULL but I can't figure out how to do similar in DAX. I've tried:
-- this returns blank, presumably because when there is a FactB then RecordId isn't blank, and when there is no Fact B then RecordId a NULL which isn't blank either
FactA_No_FactB:=CALCULATE(DISTINCTCOUNT(Records[Id]), FactA, FILTER(FactB, ISBLANK([RecordId])))
-- this returns the long "The value for columns "RecordId" in table "FactB" cannot be determined in the current context" error.
FactA_No_FactB:=CALCULATE(DISTINCTCOUNT(Records[Id]), FILTER(FactA, ISBLANK(FactB[RecordId])))
I've also tried various ways of using RELATED and RELATEDTABLE but I don't really understand enough about DAX and context to know what I'm doing.
Can someone explain how I can write the calculated measure to count Records with FactA but no FactB?
Thanks in advance.
Edit - Workaround
I've come up with this, it looks correct so far but I'm not sure if it is the generally correct way to do this:
-- Take the count with FactA and subtract the count of (FactA and FactB)
FactA_No_FactB:=CALCULATE(DISTINCTCOUNT(Records[Id]), FactA) - CALCULATE(DISTINCTCOUNT(Records[Id]), FactA, FactB)
Here's an alternative, that might still not be the best way of doing it:
FactA_No_FactB:=CALCULATE(DISTINCTCOUNT(Records[ID]), FILTER(Records,CONTAINS(FactA, FactA[RecordID],Records[ID]) && NOT(CONTAINS(FactB,FactB[RecordID],Records[ID]))))
The difference between my version and yours is that mine returns a value of 1 for those items in and A but not B and BLANK for everything else. Your version returns 1 for those items in A but not B, 0 for those in both A and B and BLANK for everything else. Depending on your use case, one outcome may be prefereable over the other.

Excel Power Query -- Select value in column specified in related table -- INDEX+MATCH alternative

Problem
I have two queries, one contains product data (data_query), the other (recode_query) contains product names from within the data_query and assigns them specific id_tags. id_tags are also column names within the data_query.
What I need to achieve and fail at
I need the data_query to look at the id_tag of the specific product name within the data_query, as parsed from the recode_query (this is already working and in place) and input the retrieved value within the specific custom column cell. In Excel, I would be using INDEX/MATCH combo:
{=INDEX(data_query[#Data];; MATCH(data_query[#id_tag]; data_query[#Headers]; 0))}
I have searched near and far, but I probably can't even spot the solution, even if I have come across it, as I am not that deep in the data manipulation and power query myself.
Is this what you're wanting?
let
DataQuery = Table.FromColumns({{1,2,3}, {"Boxed", "Bagged", "Rubberbanded"}}, {"ID","Pkg"}),
RecodeQuery = Table.FromColumns({{"Squirt Gun", "Coffee Maker", "Trenching Tool"}, {1,2,3}}, {"Prod Name", "ID2"}),
Rzlt = Table.Join(DataQuery, "ID", RecodeQuery, "ID2", JoinKind.Inner)
in
Rzlt

Resources