Power Query formula - powerquery

I would like to create a formula in power query
this is the data
value :
High-back|low-back|Mid-back|No Back
description:
Venus Comfort Mesh High-Back Fabric Chair.
I would like the query to look at the data under value and if its in the description the output should say "fix" How can I create that rule? so this example should say "fix" because High-back is in the description and the value. If you can help me create an output to say the High-back value would be perfect.. can you show me both outputs?

So, if I'm understanding you correctly, you are asking that if you have a table like this:
...
You want to know how to derive this:
(This output example assumes you want to ignore text case.)
The answer is to add a new custom column with a formula like this:
(The Comparer.OrdinalIgnoreCase is what causes this to ignore the text case. So if case matters, then remove the , Comparer.OrdinalIgnoreCase from the formula.)

Is this output more in line with what you are asking, Shanna?
If so, here's one way to get there. Starting with this table:
Add an index column:
(Add Column -> Index Column)
Then split by delimiter, on the pipes:
(Home -> Split Column -> By Delimiter ->
->
OK)
Then make sure the value and description columns are text type:
(Select both the value and description columns then -> Home -> Data Type -> Text)
Add a column containing the result whether each row's description contains its value (has a value-description match):
(Add Column -> Custom Column ->
->
OK)
Group on the Index column, using All Rows:
(Transform -> Group By ->
->
OK)
Add a column and identify whether the information in the row's grouped table has a value-description match listed in it by returning "fix" for the row:
(Add Column -> Custom Column ->
->
OK)
Merge the earlier table state from the applied step where the index column was added and the last state where the "fix" was (or wasn't) added for each row:
(You can set this up by clicking: Home -> Merge Queries ->
Note that the second table that is selected here is the same table as the first, which is the current table.
-> OK
Change the merge made above by editing the formula from:
= Table.NestedJoin(#"Added Custom1", {"Index"}, #"Added Custom1", {"Index"}, "Added Custom1", JoinKind.LeftOuter)
to
= Table.NestedJoin(#"Added Index", {"Index"}, #"Added Custom1", {"Index"}, "Added Custom1", JoinKind.LeftOuter))
Expand the tables in the Added Custom column from the merge:
(Click ->
->
OK)
Remove the Index column:
(Select the Index column then -> Home -> Remove Columns)
Here's the M code for it:
let
Source = Table1,
#"Added Index" = Table.AddIndexColumn(Source, "Index", 0, 1, Int64.Type),
#"Split Column by Delimiter" = Table.ExpandListColumn(Table.TransformColumns(#"Added Index", {{"value", Splitter.SplitTextByDelimiter("|", QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "value"),
#"Changed Type" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"value", type text}, {"description", type text}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "Custom.1", each Text.Contains([description],[value],Comparer.OrdinalIgnoreCase)),
#"Grouped Rows" = Table.Group(#"Added Custom", {"Index"}, {{"AllData", each _, type table [value=nullable text, description=text, Custom=table, Index=number, Custom.1=logical]}}),
#"Added Custom1" = Table.AddColumn(#"Grouped Rows", "fix?", each if List.AnyTrue([AllData][Custom.1]) then "fix" else null),
#"Merged Queries" = Table.NestedJoin(#"Added Index", {"Index"}, #"Added Custom1", {"Index"}, "Added Custom1", JoinKind.LeftOuter),
#"Expanded Added Custom1" = Table.ExpandTableColumn(#"Merged Queries", "Added Custom1", {"fix?"}, {"fix?"}),
#"Removed Columns" = Table.RemoveColumns(#"Expanded Added Custom1",{"Index"})
in
#"Removed Columns"

Related

How to make a pivot table with power query

I have a table with number like below ,
Phone Number
123, 456, 890
123453
902, 423
so i would like to do the pivot table with can show all the phone number (delimiter is ",") and count how many time it appear in the list ? can someone assist for that?
I just have a initial step with the code below
let
Source = Excel.CurrentWorkbook(){[Name="Phone_Number"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Phone Number", type text}})
in
#"Changed Type"
updated: question solved.
In powerquery,
right click the column,
home .. split column by delimiter ... delimiter:comma, Advanced Options:rows
then right click column and group by...
use default options and hit ok
let Source = Excel.CurrentWorkbook(){[Name="Phone_Number"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Phone Number", type text}}),
#"Split Column by Delimiter" = Table.ExpandListColumn(Table.TransformColumns(Table.TransformColumnTypes(#"Changed Type", {{"Phone Number", type text}}, "en-US"), {{"Phone Number", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Phone Number"),
#"Grouped Rows" = Table.Group(#"Split Column by Delimiter", {"Phone Number"}, {{"Count", each Table.RowCount(_), Int64.Type}})
in #"Grouped Rows"

Power query, increment column value based on change in value in another column

In power query I would use the Excel formula as shown in the screen shot link below.
Is there an easy way to do this in Power Query M Code? It doesn't seem so!
It looks like you want to add an index by group on Type
Right click Type column, and Group
Use new column name:data, Operation:All Rows and hit ok
Add column .. index column .. custom and put in your starting numbers and increment
Use arrows atop the data column to expand to new rows
Remove the extra column
sample code:
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Grouped Rows" = Table.Group(Source, {"Type"}, {{"data", each _, type table}}),
#"Added Index" = Table.AddIndexColumn(#"Grouped Rows", "Index", 23, 1),
#"Expanded data" = Table.ExpandTableColumn(#"Added Index", "data", {"Type"}, {"Type.1"}),
#"Removed Columns" = Table.RemoveColumns(#"Expanded data",{"Type.1"})
in #"Removed Columns"
another way to do it that requires editing code
Right-click the Type column and remove duplicates (or group, whichever is easier)
Add index
Merge that result back into original data and expand
sample code
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
// remove dupes and create index
#"Removed Duplicates" = Table.Distinct(Source),
#"Added Index" = Table.AddIndexColumn(#"Removed Duplicates", "Index", 23, 1),
//merge the numbered table back into original
#"Merged Queries" = Table.NestedJoin(Source,{"Type"},#"Added Index",{"Type"},"TT",JoinKind.LeftOuter),
#"Expanded Table1" = Table.ExpandTableColumn(#"Merged Queries", "TT", {"Index"}, {"Index"})
in #"Expanded Table1"

Power Query: Duplicate Rows Based on Value

I have a column that contains the Total Stock of an item. I'd like to expand this out into 1 row per item (i.e. the item has 6 in stock and therefore appears as 6 line items).
Is this possible with power query?
The M-Code below will expand this input table
to this
let
Source = Excel.CurrentWorkbook(){[Name="tblData"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"ColA", type text}, {"Stock", Int64.Type}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "Col", each List.Repeat({[ColA]},[Stock])),
#"Expanded Col" = Table.ExpandListColumn(#"Added Custom", "Col")
in
#"Expanded Col"

How to add external data as new columns in Power Query? Followup question about syntax

I saw this question answered but the solution I did not work for me. Below is my query but the new column displays "error" for each row of new column:
let
Source = Excel.Workbook(File.Contents(fnGetParameter("File Path")), null, true),
Table1 = Source{[Item="Devices_PLC_ID",Kind="Table"]}[Data],
#"Removed Table 1 Blank Rows" = Table.SelectRows(Table1, each not List.IsEmpty(List.RemoveMatchingItems(Record.FieldValues(_), {"", null}))),
#"Added Index" = Table.AddIndexColumn(#"Removed Table 1 Blank Rows", "Index", 1, 1),
Table2 = Source{[Item="Devices",Kind="Table"]}[Data],
#"Removed Table 2 Blank Rows" = Table.SelectRows(Table2, each not List.IsEmpty(List.RemoveMatchingItems(Record.FieldValues(_), {"", null}))),
#"Added Custom" = Table.AddColumn(#"Removed Table 2 Blank Rows", "PLC_ID", each #"Added Index"[PLC_ID]{[Index]})
in
#"Added Custom"
I'm not quite sure what you are attempting to do, but this code:
#"Added Custom" = Table.AddColumn(#"Removed Table 2 Blank Rows", "PLC_ID", each #"Added Index"[PLC_ID]{[Index]})
creates a column called [PLC_ID], with a formula (each ... ) that refers to the same (not yet created) column. Which you cant do. Did you mean to use Devices_PLC_ID in the second instance?

Iterating over each cell in a column in Power Query

I created a table called Table3 with two columns named URL which is empty and Value which contains a list of websites. The following query retrieves data from the websites stored in Table 3.
let
Parameter = Excel.CurrentWorkbook(){[Name="Table3"]}[Content],
URL= Parameter{1}[Value],
Source = Web.Page(Web.Contents(URL)),
Data0 = Source{0}[Data],
#"Changed Type" = Table.TransformColumnTypes(Data0,{{"Date", type date}, {"Open", type number}, {"High", type number}, {"Low", type number}, {"Close", type number}, {"Volume", type number}, {"Market Cap", type number}}),
#"Removed Columns" = Table.RemoveColumns(#"Changed Type",{"Market Cap", "Open", "High", "Low"}),
#"Sorted Rows" = Table.Sort(#"Removed Columns",{{"Date", Order.Ascending}})
in
#"Sorted Rows"
The second line runs the query for the first website in the Value column.
Is it possible to introduce a loop that would run the query for all the websites?
If not is it possible to run the query for all the websites in sequence by manually pasting the above code and changing the number in the brackets for each website?
If it is possible I assume it would load the contents in the same sheet, is there any way to load the content in different sheets for each iteration?
Thanks for reading my question.
Loops aren't really a thing in Power Query, but you can still do what you're after. I don't know what URLs you're pulling from, so let me give you an example using publicly available ones.
Let's suppose my Table3 is the following:
URL
--------
https://finance.yahoo.com/quote/AAPL?p=AAPL
https://finance.yahoo.com/quote/AAPL?p=GOOG
I can load this into the query editor and create a custom column that reads the webpage for each URL.
= Web.Page(Web.Contents([URL])){0}[Data]
(The table I want is the first one (hence the {0} row index) and is in the [Data] column.)
Now I have a table like this where the bottom table is a preview of the cell I have selected.
Click the arrows icon to expand the tables.
From here you can filter Column1 to pick which values you are interested in (let's say Ask, Bid, Open, and Volume) and then pivot that column (Transform > Pivot Column). Choose Column2 as the values column and select "Don't Aggregate" under Advanced options.
The result should be the table you see above the pivot dialogue box.
Here's the full M code for the query that shows up in the Advanced Editor
let
Source = Excel.CurrentWorkbook(){[Name="Table3"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"URL", type text}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each Web.Page(Web.Contents([URL])){0}[Data]),
#"Expanded Custom" = Table.ExpandTableColumn(#"Added Custom", "Custom", {"Column1", "Column2"}, {"Column1", "Column2"}),
#"Filtered Rows" = Table.SelectRows(#"Expanded Custom", each ([Column1] = "Ask" or [Column1] = "Bid" or [Column1] = "Open" or [Column1] = "Volume")),
#"Pivoted Column" = Table.Pivot(#"Filtered Rows", List.Distinct(#"Filtered Rows"[Column1]), "Column1", "Column2")
in
#"Pivoted Column"

Resources