Hello I need to multiply two columns in a new column in Power Query where the columns can change name but not position. So for example I want to multiply the second column in a table with the fourth column in the same table independently and want to be able to change name on the table headers and the code should still work
Table.ColumnNames(Source){1} would be the name of the 2nd column
Table.ColumnNames(Source){3} would be the name of the 4th column
You can then wrap it with Record.Field (_,x) to get the value
so the value in the second column would be
Record.Field (_,Table.ColumnNames(Source){1})
So the code below would create a new column named Mult that multiplies columns 2 and 4 without knowing their column names
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Added Custom" = Table.AddColumn(Source, "Mult", each
Record.Field (_,Table.ColumnNames(Source){1}) *
Record.Field (_,Table.ColumnNames(Source){3}) )
in #"Added Custom"
Related
I have an unpivoted table in the Power BI Query with +20 columns and +10000 rows.
The first columns are related to KPI name, month, and other data of interest. The columns after are the columns that contain the actual values I want to display.
Some rows have the KPI description (name, month, etc.) but do NOT any values in the rest of the columns ("NULL").
Is there a way to remove these rows? Remove whenever all the values of the rest of the columns are "NULL". (First columns will never be empty)
Thank you!!
I'm still a beginner in this Power BI world :)
If the other columns are always nulls together, then just pick one of those columns and use the arrow atop the column to remove the tick mark from [ ] null and filter it out
= Table.SelectRows(#"PriorStepName", each (Total number of shipments inbound] <> null)
If you have to check that every column except certain columns have nulls, then you could do this, which counts the number of nulls on each row. Then filter that column for proper number of nulls using the drop down atop that new column
= Table.AddColumn(#"PriorStepName", "Count", each List.NonNullCount(Record.ToList(_)))
Similarly you could count the number of null columns excluding the first X columns (here the first 2 columns) then filter that column
= Table.AddColumn(#"PriorStepName", "Count", each List.NonNullCount(List.Skip(Record.ToList(_),2)))
I am dealing with a column which is having Text Value mixed with Table data.
Table value
I would like to operate on this column by
if value = Table : aggregate the Table by combining the value with a comma separator
if value = : keep the original value
The result would be
I use this function to aggregate the value for Table, but I got an error when the value is a text
= Table.AggregateTableColumn(#"Lignes filtrées1", "value.1", {{"Element:Text", each Text.Combine(List.Transform(_, (x) => Text.From(x)), ", "), "Desired Result"}})
Would you have some tips to help me with this problem ?
Thanks in advance
It is hard to tell what you have in your table
As an example if the embedded table looked like this, with a single column
then you could add a column like
#"Added Custom" = Table.AddColumn(#"PriorStepName", "Custom", each try Text.Combine([Column1][TableColumn1],",") otherwise [Column1])
and get
If your table is more complicated you'd probably have to unpivot it first or otherwise give us a hint how to transform it into a single cell
I am looking for a query which I can use in pymysql to UPDATE the rows in one column without the WHERE constraint.
This is the query I used but it replaces all the NULL into 1s:
UPDATE simplefocusstatistic_studentanswers
SET Player_id = null WHERE simplefocusstatistic_id = 1
How can I use a column value as a column name. I've tried this:
SELECT TableX.(
SELECT OdTable.columnamecell
from OdTable
where 1 =1
AND OdTable.KeyValue = TableX.SomeValue
) as MyValue
,TableX.OtherValue as OtherValue
, TableX.SomeValue
from TableX
WHERE 1 = 1
Or to say it another way: Can I use a table column value as a column name for another query or sub-query?
To clarify: The table: OdTable has a column with values that are the column name in another table.
No, and Yes. You can't do this with "standard" SQL; all table and column names must be known, as literals, when the query is compiled; they can't be provided at runtime. What you want is called "dynamic SQL"; sometimes it is the only solution to a problem, but most of the time it is used when it is not necessary. It has several disadvantages (security risk, performance penalty, difficulty to maintain, ...)
I can do
ALTER TABLE table_name ADD COLUMNS (user_id BIGINT)
to add a new column to the end of my non-partition columns and before my partition columns.
Is there any way to add a new column to anywhere among my non-partition columns?
For example, I would like to put this new column user_id as the first column of my table
Yes it is possible to change the location of columns but only after adding it in the table using CHANGE COLUMN
In your case, first add the column user_id to the table with below command:
ALTER TABLE table_name ADD COLUMNS (user_id BIGINT);
Now to make user_id column as the first column in your table use change column with FIRST clause:
ALTER TABLE table_name CHANGE COLUMN user_id user_id BIGINT first;
This will move the user_id column to the first position.
Similarly you can use After instead of first if you want to move the specified column after any other column. Like say, I want to move dob column after user_id column. Then my command would be:
ALTER TABLE table_name CHANGE COLUMN dob dob date AFTER user_id;
Please note that this commands changes metadata only. If you are moving columns, the data must already match the new schema or you must change it to match by some other means.
Ah, here's the explanation for why you listed user_id twice (it's not a type):
// Next change column a1's name to a2, its data type to string, and put it after column b.
ALTER TABLE test_change CHANGE a1 a2 STRING AFTER b;
// The new table's structure is: b int, a2 string, c int.
No, it is not possible.
One solution is to create new table using "CREATE TABLE AS SELECT" approach and drop older one.