Is there a way to dynamically identify and expand an embedded table's columns? - powerquery

If I want to expand this embedded table...
...and I click on the expand button, I'm presented with the dropdown to select which columns I want to expand:
However, if I choose '(Select All Columns)' to expand them all, Power Query turns that into hard-coded column names of all the columns at the time I do that. Like this:
= Table.ExpandTableColumn(Source, "AllData", {"Column1", "Column2", "Column3", "Column4", "Custom"}, {"Column1", "Column2", "Column3", "Column4", "Custom"})
After that, if the underlying embedded table's columns change, the hard-coded column names will no longer be relevant and the query will "break."
So how can I tell it to dynamically identify and extract all of the current columns of the embedded table?

You can do something like this to get the list of column names:
List.Accumulate(Source[AllData], {}, (state, current) => List.Union({state, Table.ColumnNames(current)}))
This goes through each cell in the column, gets the column names from the table in that cell, and adds the new names to the result. It's easier to store this in a new step and then reference that in your next step.
Keep in mind that this method can be much slower than passing in the list of names you know about because it has to scan through the entire table to get the column names. You may also have problems if you use this for the third parameter in Table.ExpandTableColumn because it could use a column name that already exists.

Try using Table.Join which joins and expands the second table in one step.
"Merged Queries" = Table.Join(Source,{"Index.1"},Table2,{"Index.2"},JoinKind.LeftOuter)
You just need to make sure that the columns between the tables are unique.
Use Table.PrefixColumns to ensure column names are unique

Related

Create new column based on specific cell value

I need to create a new column containing repeatedly a specific cell value.
In Column1, Row1 the table contains the exam name. Lets say "Exam1". I now need a new column with this text value "Exam1" for all of my students. The new columns name would then be "Exam-Name" and all the rows should have as content the exam name "Exam1".
How can I reference to a specific content in a specific cell and have this text as value for all of my rows in a new column?
Concrete: I need the value "MMA20aL..." to be the value in the row on the right where actually you find the contents "KE21a..."
Thank you for your help. I pretty new to PowerQuery, thank you for a detailed explanation :-)
If you right click the first cell and drill down it will show you a formula. Make note of that formula then remove the drill down step.
Add column ... custom column ... with column name Exam-Name and the formula it showed you, such as :
= Source{0}[Column1]
Where Source in above formula is the name of prior step and Column1 is the name of the first column
So, for example, if prior step is #"Changed Type" and the first column is named Bob then use instead
= #"Changed Type"{0}[Bob]
(As a note, your data is in a terrible format and needs much massaging before it would be really useful)

Combining multiple sheets with different columns using Power Query

I have a workbook with multiple pages that need to get combined, i.e. stacked, into one table. While they have many similar column names, they do not all have the same columns and the column order differs. Because of this I cannot use the inherent merge functionality because it uses column order. Table.Combine will solve the problem, but I cannot figure out to create a statement that will use the "each" mechanic to do that.
For each worksheet in x workbook
Table.Combine(prior sheet, next sheet)
return all sheets stacked.
Would someone please help?
If you load your workbook with Excel.Workbook you can choose the Sheet Kind (instead of Table or DefinedName kinds) and ignore the sheet names.
let
Source = Excel.Workbook(File.Contents("C:\Path\To\File\FileName.xlsx"), null, true),
#"Filter Sheets" = Table.SelectRows(Source, each [Kind] = "Sheet"),
#"Promote Headers" = Table.TransformColumns(#"Filter Sheets", {{"Data", each Table.PromoteHeaders(_, [PromoteAllScalars=true])}}),
#"Combine Sheets" = Table.Combine(#"Promote Headers"[Data])
in
#"Combine Sheets"
Load each table into Power Query as a separate query
fix up the column names as needed for each individual query
save each query as a connection
in one of the queries (or in a separate query) use the Append command to append all the fixed up queries that now have the same column names.

Instead of selecting a column to sort a table by in a dropdown, how do I add the sorting column names in a separate column?

I have a workbook where I have displayed a table where the user can view the top 3 IDs based on a column they can select from a dropdown (Measure A, Measure B, ... , Measure H):
Now, instead of this, I want to add a separate column called Sorting Criterion to the table that would store the column names with which the respective rows of the table was sorted. So, the expected output is something like this:
How do I do this? I'm open to using Tableau Prep Builder, if needed, for intermediate steps that may be too convoluted for Tableau Desktop.
Create a calculated field that just contains the Sort By parameter. Add this calculated field to your view.

MS Access table list order is defaulted in subform

I have a subform with a table, where some fields are restricted to tables. The tables are sorted in the correct order, but when it is used in the subform, it is set to default sorting. I can't find the location of the correct place to set the sorting criteria. I tried using query instead of a table, but i could only sort either by ascending or descending (or nothing). All three options resultet in undesired order. Example of the phase being sorted as default despite the table settings:
Suggestions?
As Gustav suggests, add another field to your t_Phase table, which will be the order you want this list to appear in the drop-down (make sure it is a number field):
Supply the numeric order:
In your other table, you can use the ID field from your t_Phase table by selecting it through the lookup wizard:
Move across the ID and the new "Order" field you just created:
Use the "Order" field as the field you want to use for sorting and make sure "Ascending" is selected:
Continue through the wizard:
Select the field that has the data you want to go in to the record of your table (I assume it's the ID field rather than the Order field, so make sure ID is selected):
Finish the lookup wizard and go in to Datasheet View on your table and check that the dropdown is now in numeric order. You've probably noticed a problem though as it is showing both the ID and the Order columns:
To hide the unnecessary Order column, go back in to Design View and click on your phase field to select it.
At the bottom should be a "Lookup" tab. Click on this and notice the Column Widths property... it's showing 2 different widths values; the first one is the ID field and the second one is the Order field. Set the Order field to 0cm to hide it. I'd then recommend setting the remaining Id field width to something that will be wide enough to show all your ID field's options. Then I'd set the List Width property to the same width.
8cm, for example:
This should result in something like the following, which is hopefully what you're after.
If you're not adding a lookup field to the design structure of a table, but are instead using a combo-box on a form or subform, the principle is pretty much the same; you'll just have to amend the combo-box properties from the form's Property Sheet "Format" tab instead:
Combo- and listboxes always contain text, thus your column is not sorted numeric.
To have a numeric sort, add a column with the number only and - in the source - sort only on that. If you don't the number separately, create it from this expression:
Val(Mid([PhaseField], 7))

How to check whether a column was deleted completely in a webtable?

How to check whether a column was deleted completely in a webtable using VB script?
I've created a row in a webtable and deleted it. I'd like to check whether the column is deleted from that webtable or not. I've written a script and having a tough time to write the logic. I can get complete row count from a webtable and can loop through one by one. But how to check whether that column was deleted from the webtab?
rowCount = SwfWindow(obj).SwfTable(tbl).RowCount
For i = 0 To rowCount - 1
names = SwfWindow(obj).SwfTable(tbl).GetCellData(i, 1)
If names = mycolname 'mycolname is the name of the column deleted Then
SwfWindow(obj).SwfTable(tbl).ClickCell i,1
Print "col name is present in the table"
Exit for
else
Print "col name is deleted completely from the table"
End If
Next
How do you want to identify the column? Probably by name. (The code shown in the question indexes by numeric index.)
It then depends on the webtable´s structure:
There is a WebTable runtime property returning the name of all columns in a string separated by a semicolon; iterate over the components of this string and query their names in the loop. If you find the to-be-deleted column in the process, it is still present, if not, it´s deleted.
See http://www.sqaforums.com/forums/hp-unified-functional-testing-uft-mercury-quicktest-pro-qtp/148239-qtp-function-web-table-get-column-number-giving-column-name.html for an example.
Sometimes, the table is organized a bit different, and the getROProperty call returns garbage. Then, you usually find the column names as cell data in row 1. There, you would need to iterate over the columns and use getCellData to look at the column name.
As pointed out by #TheBlastOne, the table you see in the AUT (application under test) does not necessarily tell the whole truth about how it is actually structured. The following is a neat trick, which should work for both WebTable and swfTable, to be able to see the actual structure of a table:
When you have both the AUT and QTP open, click the "Record" button in QTP
Choose to start recording on the open application
On the toolbar menu in QTP, select "Insert" --> "Checkpoint" --> "Standard Checkpoint" and click on the table in the AUT
A new window should now have opened, which, among other things, shows you exactly how the table is structured, i.e. row indexes and/or names, column indexes and/or names, etc.

Resources