Get output within multiple date ranges (and if statements) in Power Query - if-statement

Edited to include full code with the Add Column function in Power Query. The "Removed Columns1" refers to the previous step, and name the custom column as Price.
I'm trying to add a custom column in Power Query to output a price for specific date ranges. Tried the following M code but I get an error instead:
= Table.AddColumn(#"Removed Columns1", "Price", each
if [Date]<#date(08,12,2019) then "11.50"
else if [Date]>#date(08,11,2019) and [Date]<#date(10,14,2019) then "0.00"
else if [Date]>#date(10,13,2019) and [Date]<#date(12,30,2019) then "11.50"
else "2.50")
The error I get back:
Expression.Error: The Date operation failed because the resulting value falls outside the range of allowed values.

Use #date(2019,08,11) not #date(8/11/2019):
= Table.AddColumn(#"Removed Columns1", "Price", each
if [Date]<#date(2019,8,12) then "11.50"
else if [Date]>#date(2019,8,11) and [Date]<#date(2019,10,14) then "0.00"
else if [Date]>#date(2019,10,13) and [Date]<#date(2019,12,30) then "11.50"
else "2.50"
)

Related

Extract 3 tables from the source table by specific text in cell

I have a source table with more or less this format :
And I search how to extract the data for each section by using only the data from 'Name' column.
But like i'm pretty new to PowerQuery and PowerBi, I don't find the right command to reach my goal
The first possibillity is to add a new column and identify each line by the title like this :
Or the second possibility is to create 3 new table for each title and separate the data like this :
Thanks
Add a custom column that checks the Value1 column for a null, and if its a null, then return the value in Name
= if [Value 1]=null then [Name] else null
then right click and fill down that new column, and apply a filter to it
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Added Custom" = Table.AddColumn(Source, "Custom", each if [Value 1]=null then [Name] else null),
#"Filled Down" = Table.FillDown(#"Added Custom",{"Custom"})
// add row here to optionally filter on Custom column
in #"Filled Down"
Or just have one query and create more queries that have specific filters like this one
let Source = Table.SelectRows(OtherQueryNameHere, each [Custom] = "m") in Source

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}}))

List calculations on powerquery power bi

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

Create idex with repeated data

I have data from an external source that is downloaded in csv format. This data shows the interactions from several users and doesn't have an id column. The problem I'm having is that I'm not able to use index because multiple entries represent interactions and processes. The interactions would be the group of processes a specific user do and the process represents each actions taken in a specific interaction. Any user could repeat the same interaction at any time of day. The data looks likes this:
User1 has 2 processes but there were 3 interactions. How can I assign an ID for each interaction having into consideration that there might be multiple processes for a single user in the same day. I tried grouping them in Power Query but it groups the overall processes and I'm not able to distinguish the number of interactions. Is it better to do it in Dax?
Edit:
I notice that it is hard to understand what I need but I think this would be a better way to see it:
Process 2 are the steps done in an interaction. Like in the column in yellow I need to add an ID taking in to consideration where an interaction start and where it ends.
I'm not exactly sure I follow what you describe. It looks to me like user1 has 4 interactions--Processes AA, AB, BA, and BB--but you say 3.
Still, I decided to take a shot at providing an answer anyway. I started with a CSV file set up like you show.
Then brought the CSV into Power Query and, just to add a future point of reference so that you could follow the Id assignments better, I added an index column that I called startingIndex.
Then I added a custom column combining the processes that I understand actually define an interaction.
Then I grouped everything by users and Interactions into a column named allData.
Then I added a custom column to copy the column that was created from the earlier grouping, to sort the tables within it, and to add an index to each table within it. This essentially indexed each user's interaction group. (Because all of your interactions occur on the same date(s), the sorting doesn't help much. But I did it to show where you could do it if you included datetime info instead of just a date.)
Then I added a custom column to copy the column that was created earlier to add the interactions index, and to add an Id item within each table within it. I constructed each Id by combining the user, interactions, and interactionIndex for each.
Then I selected the latest column I had created (complexId) and removed all other columns.
Last, I expanded all tables without including the Interactions and Index columns. (The Index column was the index used for the interactions within the groups and no longer needed.) I included the startingIndex column just so you could see where items originally were at the start, in comparison to their final Id.
Given your new example, to create the Interaction ID you show, you only need the first two columns of the table. If not part of the original data, you can easily generate the third column (Process2))
It appears you want to increment the interaction ID whenever the Process changes
Please read the comments in the M code and explore the Applied Steps to better understand the algorithm:
M Code
let
//be sure to change table name in next row to your real table name
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{
{"User", type text},
{"Process", type text},
{"Process2", type text}
}),
//add an index column
idx = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
//Custom column returns the Index if
// the current Index is 0 (first row) or
// there has been no change in user or process comparing current/previous row
// else return null
#"Added Custom" = Table.AddColumn(idx, "Custom",
each if [Index]=0
then 0
else
if [Process] <> idx[Process]{[Index]-1} or [User] <> idx[User]{[Index]-1} then [Index]
else null),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Index"}),
//Fill down the custom column
// now have same number for each interactive group
#"Filled Down" = Table.FillDown(#"Removed Columns",{"Custom"}),
//Group by the "filled down" custom column with no aggregation
#"Grouped Rows" = Table.Group(#"Filled Down", {"Custom"}, {
{"all", each _, type table [User=nullable text, Process=nullable text, Process2=nullable text, Custom=number]}
}),
//add a one-based Index column to the grouped table
#"Added Index" = Table.AddIndexColumn(#"Grouped Rows", "Interaction ID", 1, 1, Int64.Type),
#"Removed Columns1" = Table.RemoveColumns(#"Added Index",{"Custom"}),
//Re-expand the table
#"Expanded all" = Table.ExpandTableColumn(#"Removed Columns1", "all",
{"User", "Process", "Process2"}, {"User", "Process", "Process2"})
in
#"Expanded all"
Source
Results

Is it possible to ignore case sensitivity of column headings in Power Query?

In the M language is there a way to ignore case when using commands like Table.TransformColumnTypes? I had a column named "Task" that was renamed "TASK" and is now causing an Expression Error. I didn't expect column headings to be case sensitive since that's not normally an issue in SQL.
The step formula looks like this:
= Table.TransformColumnTypes(#"Removed Columns",{{"Task", type text}, {"Employee Name", type text}})
which returns this error:
Expression.Error: The column 'Task' of the table wasn't found.
Details:
Task
As I go through and adjust the case of each instance of "Task", I also get errors with these commands that are still referencing the "Task" field:
Table.Group
Table.RemoveColumns
Table.Sort
Table.TransformColumns
Table.TransformColumnTypes
Text.Combine
Note that I found an article about using Comparer.OrdinalIgnoreCase but that appears to apply to data in the table and not the column headings.
It can't be ignored,but you can rename all the column names by this formula:
= Table.RenameColumns(YourTableName,List.Transform(Table.ColumnNames(YourTableName),each {_,Text.Proper(_)}))
Comparer.OrdinalIgnoreCase only can be used in this condition:
= Text.Contains("abc","A", Comparer.OrdinalIgnoreCase) //returns true