List calculations on powerquery power bi - powerbi

How to make a calculations in Power Bi using whole list for all rows. I tried to do this way
but I got error "A cyclic reference was encountered during evaluation". Is this a way to put whole column in calculated fields and make with it some calculations?
This is my table
My desire is for example if(OrderID{index}="A" then Shippedwwo{index} else Shippedowo{index -1} like in a picture below.

To get your posted output, there is no need for the Index column (or for a custom function, for that matter).
#"Added Custom" = Table.AddColumn(previousStep, "test_column",
each if [Action]="A" then [Assignwwo] else null),
#"Filled Down" = Table.FillDown(#"Added Custom",{"test_column"})
orig data
output

Related

Power bi deleting rows and keeping first row

I imported a table from SQL which is more than 100k rows. i was connecting it to data model
but then i found that it has some duplicate rows. i identified those duplicate rows which are
they repeating twice i want to keep first and delete whole second copy of them. i tried for hours in query editor adding custom column but it just would let me add new column condition as data view in power bi did.
Hope, you'll get a better solution form SO contributors, if not you can try like this.
let
Source=...
filtered =
Table.RemoveMatchingRows(
Source
,{[Keycode= "qwe123", Desc="rty456"]}
,"Desc"
)
in
filtered
Edited: Group, and find the first occurrence, like here
#"Grouped Rows" = Table.Group(#"PriorStepNameHere", {"TestColumn1","TestColumn2"}, {{"data", each Table.RemoveColumns(Table.AddColumn(Table.AddIndexColumn(_, "Index", 0, 1, Int64.Type), "First Instance?", each if [Index]=0 then 1 else 0),{"Index"}), type table }}),
then expand

Change values from column in Power Query editor

I am working on a Power BI report and I am using a parquet file as the source. I have this column that holds info about the month:
As you see here, in MONTH_RUN, I have the data stored both as "202201" (yyyymm format) and "12/1/2021" (more like a date format).
I would need to change the values that appears like this: "12/1/2021" to "202112", so I can have all the values the same format.
I need to do this in a dynamic way, like searching for the rows that has values containing "/" character and based on that to change it into "yyyymm" format.
Is this possible?
I'm kinda new with power query and idk how should I implement this, but I would prefer to do it without creating any other additional columns.
Please, do not post sample data as images. It makes it difficult to answer your question.
One way to deal with this is to add a custom column, where the value will be either the same as MONTH_RUN (if it doesn't contain /), or it will parse the text to date, and then format it accordingly. For example:
if Text.Contains([MONTH_RUN], "/") then
Date.ToText(Date.FromText([MONTH_RUN], [Format="M/d/yyyy", Culture="en-US"]), [Format="yyyyMM", Culture="en-US"])
else
[MONTH_RUN]
Then use this custom column in your report.
I would just delete the existing MONTH_RUN column and create a new one based on the date in the DAY_RUN column.
eg:
#"Removed Columns" = Table.RemoveColumns(#"Changed Type",{"MONTH_RUN"}),
#"Added Custom" = Table.AddColumn(#"Removed Columns", "MONTH_RUN", each Date.ToText([DAY_RUN],"yyyyMM"), type text)
Or, from the UI:

List Dates and customizing tables in Power Query

I'm trying to Add Custom Column in Power Query with the objective to return a Table from a List of dates.
The syntax used is as follows below:
= Table.AddColumn(TypeDate, "AddTable", each Table.FromList(
List.Dates([Date_begin],1,#duration(1,0,0,0)
)))
where:
TypeDate is the name of last step in Power Query
"AddTable" is the name of added custom column
[Date_begin] is a column with dates to be considered as the start of my list
Although the syntax seems correct, Power Query returns an error described as follows:
Expression.Error: We could not convert the value #date(2021, 1, 1) on to Text.
Details:
Value=01/01/2021
Type=[Type]
Does anyone know how to handle this problem?
I'll show an image where Power Query shows the error.
Select here to see Power Query interface
Your question is unclear
You want to add a column that has a table of dates for each row, using Date_Begin and Mes_Final?
#"Added Custom" = Table.AddColumn(TypeDate, "AddTable", each Table.TransformColumnTypes(Table.FromList({Number.From([Date_Begin])..Number.From([Mes_Final])}, Splitter.SplitByNothing(), {"date"}),{{"date", type date}}))

Power Query - trying to remove a dynamic column that's created/updated when I access external data source

I have a table that's generated when I pull data from an accounting software - the example columns are months/years in the format as follows (It pulls all the way to current day, and the last month will be partial month data):
Nov_2020
Dec_2020
Jan_2021
Feb_1_10_2021 (Current month, column to remove)
... So on and so forth.
My goal I have been trying to figure out is how to use the power query editor to remove the last column (The partial month) - I tried messing around with the text length to no avail (The goal being to remove anything with text length >8, so the full months data would show but the last month shouldn't). I can't just remove based on a text filter, because if someone were to pull the data 1 year from now it would have to account for 2021/2022.
Is this possible to do in PQ? Sorry, I'm new to it so if I need to elaborate more I can.. Thanks!
You can do this with Table.SelectColumns where you use List.Select on the Table.ColumnNames.
= Table.SelectColumns(
PrevStep,
List.Select(Table.ColumnNames(PrevStep), each Text.Length(_) <= 8)
)
Although both Alexis Olson's and Justyna MK's answers are valid, there is another approach. Since it appears that you're getting data for each month in a separate column, what you will surely want to do is unpivot your data, that is transform those columns into rows. It's the only sensible way to get a good material for analysis, therefore, I would suggest to unpivot the columns first, then simply filter out rows containing the last month.
To make it dynamic, I would use unpivot other columns option - you select columns and it will transform remaining columns into row in such a way that two columns will be created - one that will contain column names in rows and the other one will contain values.
To illustrate what I mean by unpivoting, when you have data like this:
You're automatically transforming that into this:
You can try to do it through Power Query's Advanced Editor. Assign the name of the last column to LastColumn variable and then use it in the last step (Removed Columns).
let
Source = Excel.Workbook(File.Contents(Excel file path), null, true),
tblPQ_Table = Source{[Item="tblPQ",Kind="Table"]}[Data],
#"Changed Type" = Table.TransformColumnTypes(tblPQ_Table,{{"Nov_2020", Int64.Type}, {"Dec_2020", Int64.Type}, {"Jan_2021", Int64.Type}, {"Feb_1_10_2021", Int64.Type}}),
LastColumn = List.Last(Table.ColumnNames(#"Changed Type")),
#"Removed Columns" = Table.RemoveColumns(#"Changed Type",{LastColumn})
in
#"Removed Columns"

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.