evaluating a textual equation into a number - powerbi

AS a newbie to m I still can't get my head around it. Here is the query I have. It's gone through a number of steps to get to the below. How do I use Expression.Evaluate against the whole query against the column EntryFee, which for the sake of simplicity the query is called #"Nearly There". I want to Evaluate the entire column Entry Fee. To reiterate it needs to be done in Power Query "M"
Snapshot of table/query

You can either add a new custom column with the code
Expression.Evaluate([Entry Fee])
or do a column transformation
= Table.TransformColumns(#"Nearly There", {{"Entry Fee", Expression.Evaluate, type number}})
To generate this step, you can select the column and then Transform tab > Format > Trim and then replace Text.Trim with Expression.Evaluate in the generated code.

Related

if and search function on power BI token literal expected

I have a table called "food suppliers" with a column called "sites" that have multiple countries ending in ".com", ".es", ".co.uk".
I want to create a new column that separates these sites into their corresponding country names using the if and search function on power query.
so far in power query custom column I have:
Country = IF (SEARCH ("*.com", foodsuppliers[sites],,0) = 0, IF (SEARCH ("*.es", foodsuppliers[sites],, 0)= 0, "Spain","UK"),"USA")
But I am getting a "token literal expected" under the first = sign in "IF (SEARCH ("*.com", foodsuppliers[sites],,0) = 0"
does any one have ideas why or a better way to run this code on power query/power bi?
thanks.
Nothing in your code seems relevant
There is no SEARCH function, you want to use Text.Contains
You cant write foodsuppliers[sites] or you will be getting the entire column of all rows. You probably want each current row, which you would get with [sites]
This is not excel where you can do =if (xxx,fff,zzz) the format is if x then y else z
I recommend some tutorials

Conditionally Filtering Out Rows based on 2 Parameters in w/ Power Query

I have a table similar to the one attached below:
What I would like to do, using power query, is conditionally filter out (remove) rows where CenterNum = 1101 and DepCode = 257. I figured Table.SelectRows() would work but it doesn't and the query just returns, this table is empty. The #"Expanded AccountLookup" ,in my formula below, is referencing the power query applied step before the one I am trying to create. I'm hoping to get some input on how to remove rows based on these two paramters.
= Table.SelectRows(#"Expanded AccountLookup", each [CostCenterNumber] = "1111001" and [NoteTypeCode] = "257")
Thank you!
You didn’t post a screenshot so it is hard to tell if the column format is text or numerical but try removing the quotes around the numbers
= Table.SelectRows(#"Expanded AccountLookup", each [CostCenterNumber] = 1111001 and [NoteTypeCode] = 257)
If that doesn't work, check the actual column names against what you are using, especially for case (upper/lower) and leading/trailing spaces. The best way to do that is to temporarily rename it, and look at the code for the "from name"

Power Query / Power BI - How to move a cell value to a separate cell the easiest way?

I want to move a single value from column B to column A, how can I achieve it in the most simplest way in Power Query / Query Editor (Power BI)?
Please see attached images.
I know I might need to declare a variable so please enlighten me. By the way, I will delete row 1 afterwards, promote my headers, and rename column2 as PERIOD.
Thank you.
This might be along the lines of what you want to do.
If I start with this table named as Table1:
Then I click on the fx to the left of the formula bar:
And type = Table.InsertRows(Source, Table.RowCount(Source), {[Column2 = Source[KP20 rate]{0}, KP20 rate = null, Column4 = null]}) into the formula bar:
I used Table.InsertRows to create a new row in Table1. Source is the name of the latest state of Table1 after it is pulled into Power Query and before I do this step. So I actually use Source as the name of the table for this step instead of Table1. (Each applied step basically results in its own table. You probably know this already, but others may not.) So for this step I use Source as the table name in the Table.InsertRows statement. Then, since I want the new row to appear at the bottom of Source, I just enter the Table.RowCount of Source as the row number location for the new row. Then I enter each of the Columns' names and their values to be added. For Column2, I entered the value "Source[KP20 rate]{0}." Source[KP20 rate]{0} basically treats column KP20 rate as a list, where {0} serves as a pointer to the first item in the list. To target the second item in Source[KP20 rate] you would use Source[KP20 rate]{1}. You can see that I set the values for the other two columns (KP20 rate and Column4) to null.
The result:
Here's the M code in case you want to see it:
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
Custom1 = Table.InsertRows(Source, Table.RowCount(Source), {[Column2 = Source[KP20 rate]{0}, KP20 rate = null, Column4 = null]})
in
Custom1

POWER BI - How to add manual columns/data to existing table instead of adding columns/data to the source csv file

The picture I have attached shows what my power query table looks like (exactly the same as source file) and then underneath what I would like the final end product to look like.
Correct me if I'm wrong but I thought the purpose of power query/power bi was to not manipulate the source file but do this in power query/power bi?
If that's the case, how can I enter new columns and data to the existing table below?
You can add custom columns without manipulating source file in power bi. Please refer to below link.
https://learn.microsoft.com/en-us/power-bi/desktop-add-custom-column
EDIT: Based on your comment editing my answer - Not sure if this helps.
Click on edit queries after loading source file to power bi.
Using 'Enter Data' button entered sample data you provided and created new table. Data can be copy pasted from excel. You can enter new rows manually. Using Tag number column to keep reference.
Merge Queries - Once the above table is created merged it with original table on tag number column.
Expand Table - In the original table expand the merged table. Uncheck tag number(as it is already present) and uncheck use original column name as prefix.
Now the table will look like the way you wanted it.
You can always change data(add new columns/rows) manually in new table by clicking on gear button next to source.
Here is the closest solution to what I found from "manual data entry" letting you as much freedom as you would like to add rows of data, if the columns that you want to create do not follow a specific pattern.
I used an example for the column "Mob". I have not exactly reproduced the content of your cells but I hope that this will not be an issue to understand the logic.
Here is the data I am starting with:
Here is the Power Query in which I "manually" add a row:
#"Added Conditional Column" = Table.AddColumn(#"Changed Type", "Mob", each if [Tag Number] = "v" then null else null),
NewRows = Table.InsertRows(#"Added Conditional Column", 2, {[Mob="15-OHIO", Tag Number="4353654", Electronic ID=1.5, NLIS="", Date="31/05/2015", Live Weight="6", Draft="", Condition store="", Weighing Type="WEAN"]})
in
NewRows
1) I first created a column with only null values:
#"Added Conditional Column" = Table.AddColumn(#"Changed Type", "Mob", each if [Tag Number] = "v" then null else null),
2) With the "Table.InsertRows" function:
I indicated the specific line: 2, (knowing that power Bi start counting at zero, at the "headers" so it will the third line in the file)
I indicated the column at which I wanted to insert the value, i.e "Mob"
I indicated the value that all other other rows should have:
NewRows = Table.InsertRows(#"Added Conditional Column", 2, {[Mob="15-OHIO", Tag Number="4353654", Electronic ID=1.5, NLIS="", Date="31/05/2015", Live Weight="6", Draft="", Condition store="", Weighing Type="WEAN"]})
Here is the result:
I hope this helps.
You can apply this logic for all the other rows.
I do not think that this is very scalable however, becaue you have to indicate each time the values of the rows in the other columns as well. There might be a better option.

Remove duplicates values based on multiple column with a condition in query editor Power BI

I am new to power bi and would require your help to sort out below issue which I am facing.
Basically I am taking three columns into consideration as below:
Question: I would like to remove duplicate values from above table based on conditon "
Equal value for "Time" ,"ID" and Absolute difference in "Time spent" is lower or equal than 1"
as you can see in the image Rows highlighted falls in this category.
I would like to get these below rows removed based upon condition.
Question: I would like to remove duplicate values from above table based on conditon "
Equal value for "Time" ,"ID" and Absolute difference in "Time spent" is lower or equal than 1"
as you can see in the image Rows highlighted falls in this category.
I would like to get these below rows removed based upon condition.
I am able to perform this in excel by making us of a fourth column with formulae =IF(AND(A3=A2,B3=B2,ABS(F3-F2)<1),"problem",0) and then filtering out the rows marked as probelm. Please help!!
Regards
Mahi
I bet the suggestion from #Alexis Olson works just fine, but since you specifically mentioned the Query Editor, here's how I would do it there:
Have your data loaded like below, and just accept the changes made under Changed Type:
Don't worry about the other steps under the Query Settings. We'll get to that eventually.
Select Add Column and click Index Column, so that you get this:
Select Add Column, click Custom Column and insert this little formula in the appearing dialog box Table.AddColumn(#"Added Index", "Custom", each #"Added Index"[Time Spent]{[Index]}-#"Added Index"[Time Spent]{[Index]-1}):
Click OK, and make sure that you're getting this:
I think this step is a little weird, but you'll have to click 'Table' there in the column:
You will get an Error message in the first row, but you can remove that by right-clicking that column, and clicking Remove Errors:
Now you can click the drop-down menu in the Custom Column, select Number Filter and Does Not Equal
And insert 0, or select 0 from the drop-down menu in the dialog box:
This is it, your required numbers should now be filtered away:
Note, however, that this procedure comes at a cost since you're losing the first value due to the first step in the indexing. If the rest of this is something you can use, I can see if we can fix that last little part as well.
You can pick a representative [Time Spent] value from each unique set of rows by taking a max or min over the list of "duplicate" values. Here's the formula for such a custom column, which I'll call [Min Time]:
= List.Min(
Table.SelectRows(#"Previous Step",
(C) => (C[Time] = [Time] and
C[ID] = [ID] and
Number.Abs(C[Time Spent] - [Time Spent]) < 1)
)[Time Spent])
Once you have this custom column, you can group by [Time], [ID], and [Min Time] to roll up the duplicates and then rename the [Min Time] column to [Time Spent].