I have a query, which I'd like to filter by the new date column I created.
Basically, anything which is 42 days earlier than that date is accepted.
I've tried doing a filter by date, and then substracting it by 42, but it does not work?
let
#"SQL-JM" = let
Source = Sql.Databases("xxx.xxx.xxx.xxx"),
MNH = Source{[Name="DBT"]}[Data],
#"DBO-JM" = DBT{[Schema="dbo",Item="DBO-JM"]}[Data]
in
#"DBO-JM",
#"Added Custom1" = Table.AddColumn(#"DBO-JM", "Start_of_QTR", each Date.StartOfQuarter(DateTime.LocalNow())),
in
#"Filtered Rows"
If I understand your task correctly, you don't need custom column at all.
I'd do like that:
let
#"SQL-JM" = let
Source = Sql.Databases("xxx.xxx.xxx.xxx"),
MNH = Source{[Name="DBT"]}[Data],
#"DBO-JM" = DBT{[Schema="dbo",Item="DBO-JM"]}[Data],
GetFilterDate = Date.From(Date.StartOfQuarter(DateTime.LocalNow())), //You can use any logic to get that date
FilterRows = Table.SelectRows(#"DBO-JM", each [DateStamp] < GetFilterDate) //You can also add some modifications to GetFilterDate using each row's values, if you need
in
FilterRows
Assume that you want to filter by the DateStamp column which 42 days earlier than Start_of_QTR is accepted, you can add the following line after the #"Added Custom1" line:
#"Filtered Rows" = Table.SelectRows(#"Added Custom1", each [DateStamp] < Date.AddDays([Start_of_QTR], -42))
Related
In PowerQuery I try to sum the quantité livréeby BL 2 (i have several rows with the same BL2 number but i can't delete doublons, regarding of details of the data)
The data looks like:
I tried:
=Table.Group(#"Somme quantité livrée", {"BL 2"},{{"quantité livrée", each List.Sum([#"quantitée livrée"]), type number}}
but the function doesnt work, have the same error message "RightParen token excepted" but i don't see what should i do here (or even if its the right function to do what i except)
Basically i want to obtain the sum of the quantité livrée, quantité retournée, quantité facturée by distinct BL 2
Any idea?
I tried the Group By table proposed in answers but using it i lost others columns:
before:
And after:
Why not use the group interface to create the code for you?
#"Grouped Rows" = Table.Group(#"previous_step_name", {"BL 2"}, {{"quantité livrée", each List.Sum([quantité livrée]), type number}, {"quantité retournée", each List.Sum([quantité retournée]), type number}, {"quantité facturée", each List.Sum([quantité facturé]), type number}})
== == ==
If you want to retain other columns then in the group use an All Rows operation.
then after, expand desired columns back into table using arrows on top and slightly to right of the new column
== == ==
a totally different way to do this is just adding in three custom columns to sum ql, qr and qf based on BL2 of each row. It does NOT do any grouping, so for each BL combination you'd see the same total on each row
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Added Custom" = Table.AddColumn(Source,"sum_ql",(i)=>List.Sum(Table.SelectRows(Source, each [bl 2]=i[bl 2]) [ql]), type number ),
#"Added Custom2"= Table.AddColumn(#"Added Custom" ,"sum_qr",(i)=>List.Sum(Table.SelectRows(#"Added Custom" , each [bl 2]=i[bl 2]) [qr]), type number ),
#"Added Custom3"= Table.AddColumn(#"Added Custom2" ,"sum_qf",(i)=>List.Sum(Table.SelectRows(#"Added Custom" , each [bl 2]=i[bl 2]) [qf]), type number )
in #"Added Custom3"
First time trying to use M in power query... what I have is this table
I need to create two columns that per each row (combination of CD_Loja x CD_Produto )returns me the sum of QT_VENDA for that combination divided by the # of days in the past 3 months. The other column is pretty much the same but with the sum of VL_VENDA_LIQ Instead.
I.e: For the first row I want to sum up all QT_VENDA that matches CD_PRODUTO =1001930 AND CD_LOJA = 151 in the past 3 months (the DATE column has daily data) and divide it by the number of days in those 3 months.
Is there a way to do so ? And how do I go about this ?
Thanks in advance.
In powerquery, M, something along these lines
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.Buffer(Table.TransformColumnTypes(Source,{{"DATA", type date}, {"CD_PRODUCTO", type text}, {"CD_LOIA", type text}, {"QT_VENDA", Int64.Type}, {"VL_VENDA_LIQ", Int64.Type}})),
#"Added Custom" = Table.AddColumn(#"Changed Type" ,"QT_VENDA_90",(i)=>List.Average(Table.SelectRows(#"Changed Type" , each [CD_PRODUCTO]=i[CD_PRODUCTO] and [CD_LOIA]=i[CD_LOIA] and [DATA] <= i[DATA] and [DATA] >= Date.AddDays(i[DATA] ,-90)) [QT_VENDA]), type number),
#"Added Custom2" = Table.AddColumn(#"Added Custom" ,"VL_VENDA_LIQ_90",(i)=>List.Average(Table.SelectRows(#"Changed Type" , each [CD_PRODUCTO]=i[CD_PRODUCTO] and [CD_LOIA]=i[CD_LOIA] and [DATA] <= i[DATA] and [DATA] >= Date.AddDays(i[DATA] ,-90)) [VL_VENDA_LIQ]), type number)
in #"Added Custom2"
You can try a Measure like below-
your_expected_value =
var current_row_data = MIN(your_table_name[DATA])
--I Guess the column name should be Date instead
var current_row_data_minus_90_day = MIN(your_table_name[DATA]) - 90
var current_row_cd_produto = MIN(your_table_name[CD_PRODUTO])
var current_row_cd_loja = MIN(your_table_name[CD_LOJA])
RETURN
CALCULATE(
SUM(your_table_name[QT_VENDA]),
FILTER(
ALL(your_table_name),
your_table_name[DATA] >= current_row_data_minus_90_day
&& your_table_name[DATA] <= current_row_data
&& your_table_name[CD_PRODUTO] = current_row_cd_produto
&& your_table_name[CD_LOJA] = current_row_cd_loja
)
)/90
--Here 90 used for static 3 month consideration
Say that you would have a table that has a value of a certain account code, which is updated every three months. However, the amount is accumulating over the year, where data of 31-12 would be a full year, even though there is also data over the past three quarters. Now I would like to transform that column of values into a delta per quarter, instead of the accumulated total up until that date. So quarter two would be the value of quarter two, minus the value of quarter one. Quarter three would be the value of quarter three, minus quarter two and one etc. For the example, I would like the value of 31-12 to be 517746,90 - 387949,44 = 129797,46. This would make it possible to sum all four quarters, and get the total that you would otherwise see as the value if you only select quarter four. How would one do this in M?
Example:-
The way I've done something like that is to use List.Zip to create a list of {current,prev} lists and then transform that into the difference between each pair of values. Then you have to add that list as a new column.
Query fAppendDeltaColumn
(Source as table, ColumnName as text) as table =>
let
Values = Table.Column(Source, ColumnName),
// All but the last value of Values, prepended with 0
Prevs = List.Combine({
{0},
List.FirstN(
Values,
List.Count(Values) - 1
)
}),
Deltas = List.Transform(
List.Zip({Values, Prevs}),
each _{0} - _{1}
),
// So now we've got a list of delta values per row. Append it to the table.
#"Appended Delta" = fAppendColumns(Source, {"Delta"}, {Deltas}, {type number})
in
#"Appended Delta"
Query fAppendColumns
This is something I've written and used when I want to append lists of values as new columns. I'm not convinced this is a good method. If there's a better way, apologies in advance. :-)
(table as table, NamePerColumn as list, ValuesPerColumn as list, optional TypePerColumn as nullable list) =>
let
#"Added Index for fAppendColumns" = Table.AddIndexColumn(table, "IndexForAppendColumns", 0, 1),
#"Added Columns" = List.Accumulate(
List.Zip({NamePerColumn, ValuesPerColumn, if TypePerColumn = null then {} else TypePerColumn)}),
#"Added Index for fAppendColumns",
(table as table, NameValuesType as list) => let
values = NameValuesType{1},
#"Value Type" = if (TypePerColumn = null) then
Value.Type(values{0})
else
NameValuesType{2}
in
Table.AddColumn(table, NameValuesType{0}, each values{[IndexForAppendColumns]}, #"Value Type")
),
#"Removed Columns" = Table.RemoveColumns(#"Added Columns", {"IndexForAppendColumns"})
in
#"Removed Columns"
From here, as long as there aren't any typos, you can do something like the following:
let
Source = YourTable,
// make sure we're sorting by period
#"Sorted Rows" = Table.Sort(Source, {{"Dim_period", Order.Ascending}}),
// Assuming we want to get deltas per account. This creates one row per account, where "Subtable" is a column that contains all of the rows for that account, sorted by period thanks to the previous line.
#"Grouped Rows" = Table.Group(#"Sorted Rows", {"ACCOUNT_CODE"}, {{"Subtable", each _, type table}}),
#"Appended Deltas" = Table.TransformColumns(
#"Grouped Rows",
{"Subtable", each fAppendDeltaColumn(_, "Value")}
),
#"Combined Subtables" = Table.Combine(#"Appended Deltas"[Subtable])
in
#"Combined Subtables"
I am new with power BI and need your help.
I have 3 columns which are:
date1
reviseDate
Shipped_Date
I need to compare column reviseDate to shipped_date if it is <> or =.
How to I do it, if reviseDate column is blank, must refer on column Date1 to compare with shipped_Date column?
Try this as a calculated column:
Status =
VAR DueDate =
IF(ISBLANK(Table1[Revised]), [Date1], [Revised])
RETURN
SWITCH(
TRUE(),
ISBLANK(DueDate), BLANK(),
DueDate = Table1[Shipped_Date], "On Time",
DueDate < Table1[Shipped_Date], "Late",
DueDate > Table1[Shipped_Date], "Early"
)
This defines the date you want to compare as a variable that we use to check different conditions.
The SWITCH(TRUE(),...) is a useful construction that returns the specified result for the first condition in the list that evaluates to TRUE().
You can define a new calculated column like this:
Status = IF (AND(ISBLANK(Table1[Date1]); ISBLANK(Table1[ReviseDate])); BLANK();
IF (Table1[Shipped_Date] = IF(ISBLANK(Table1[ReviseDate]); Table1[Date1]; Table1[ReviseDate]); "On Time";
IF (Table1[Shipped_Date] > IF(ISBLANK(Table1[ReviseDate]); Table1[Date1]; Table1[ReviseDate]); "Late";
IF (Table1[Shipped_Date] < IF(ISBLANK(Table1[ReviseDate]); Table1[Date1]; Table1[ReviseDate]); "Early"; BLANK())
)
)
)
The expression IF(ISBLANK(Table1[ReviseDate]); Table1[Date1]; Table1[ReviseDate]) will return Date1 value in case ReviseDate is empty. Then it is matter of comparing this reference date with Shipped_Date and returning appropriate status.
If you want to make status column using Power Query Editor, you can use Add Column -> Conditional Column. Make one helper column, named ReferenceDate or something, which will compute the date we need to use for comparison:
Then add the actual Status column like this:
The M code will look like this (the data is embedded in it):
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bY7RCQAhDEN36bcUUxTrLOL+aygenCkI/WjymtAxxKp2tQyXJHuunGnDFmFj+HldAQqUGzC89nPlsdYZuiITrL98PRRqQUkYCyqZCw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Date1 = _t, ReviseDate = _t, Shipped_Date = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Date1", type date}, {"ReviseDate", type date}, {"Shipped_Date", type date}}),
#"Added Conditional Column" = Table.AddColumn(#"Changed Type", "ReferenceDate", each if [ReviseDate] = null then [Date1] else [ReviseDate]),
#"Added Conditional Column1" = Table.AddColumn(#"Added Conditional Column", "Status", each if [ReferenceDate] = null then "" else if [ReferenceDate] = [Shipped_Date] then "On Time" else if [ReferenceDate] < [Shipped_Date] then "Late" else if [ReferenceDate] > [Shipped_Date] then "Early" else null),
Status = #"Added Conditional Column1"{2}[Status]
in
Status
Depending on your data source, you can even modify the query and retrieve this value from the database (e.g. with Transact-SQL query if your data source is SQL Server).
I am having some issues with how to approach my query so any help would be greatly appreciated.
I have a date column that I need to increase based on two other columns values.
e.g. Date Reported column - 17/12/2018
If my Impact Column = "Urgent" and my Department = "Stores" I would need to increase my Date Reported Column to 18/12/2018
However if my Impact Column = "Standard" and my Department = "Floor" I would need to increase my Date Reported Column to 20/12/208
I would ideally like to not touch the original Date Reported Column but move this new value to another column.
So Far I have created a custom column and this is my code however it doesnt work.
AmendedDateReported = if(And(SurveyCorrectiveAction[Impact] = "Urgent", SurveyCorrectiveAction[LookUp] = "Stores"), Date.AddDays([DateReported],1),Blank ())
Thanks
Paula
Updated code, The formula seems to be pulling ok but the date part wont update:
#"Sorted Rows" = Table.Sort(Source,{{"DateReported", Order.Ascending}}),
#"Changed Type" = Table.TransformColumnTypes(#"Sorted Rows",{{"DateReported", type date}}),
#"Sorted Rows1" = Table.Sort(#"Changed Type",{{"DateReported", Order.Descending}}),
#"Added Custom" = Table.AddColumn(#"Sorted Rows1", "Date Repaired", each ""),
#"Changed Type1" = Table.TransformColumnTypes(#"Added Custom",{{"Date Repaired", type text}}),
#"Duplicated Column" = Table.DuplicateColumn(#"Changed Type1", "DateReported", "DateReported - Copy"),
#"Renamed Columns" = Table.RenameColumns(#"Duplicated Column",{{"DateReported - Copy", "AmendedDateReported"}}),
#"Merged Amendments" = Table.NestedJoin(#"Renamed Columns",{"Impact", "Department"},TLU_FaultTimeScales,{"Impact", "Department"},"TLU_FaultTimeScales",JoinKind.LeftOuter),
#"Expanded Amendments" = Table.ExpandTableColumn(#"Merged Amendments", "TLU_FaultTimeScales", {"Amendment Day"}, {"Amendment Day"}),
AmendedDateReported = Table.AddColumn(#"Expanded Amendments", "AmendedDateReported", each try Date.AddDays([DateReported],[Amendment Day]) otherwise [DateReported], type date)
in
#"Renamed Columns"
You could try:
AmendedDateReported =
Table.AddColumn(
#"Previous Step",
"Amended Date Reported",
each Date.AddDays(
[Date Reported],
if [Impact] = "Urgent" and [Department] = "Stores" then 1
else if [Impact] = "Standard" and [Department] = "Floor" then 3
else 0
),
type date
)
If you have several combinations of Impact / Department which have variable effect on amending the date, it would make more sense to put those in a separate table:
+----------+------------+----------------+
| Impact | Department | Amendment Days |
+----------+------------+----------------+
| Urgent | Stores | 1 |
| Standard | Floor | 3 |
+----------+------------+----------------+
You can then join this table to retrieve the amendment days:
#"Merged Amendments" = Table.NestedJoin(#"Previous Step",{"Impact", "Department"},tblAmendments,{"Impact", "Department"},"tblAmendments",JoinKind.LeftOuter),
#"Expanded Amendments" = Table.ExpandTableColumn(#"Merged Amendments", "tblAmendments", {"Amendment Days"}, {"Amendment Days"}),
AmendedDateReported = Table.AddColumn(#"Expanded Amendments", "Amended Date Reported", each try Date.AddDays([Date Reported],[Amendment Days]) otherwise [Date Reported], type date)
in
AmendedDateReported
Remember to update the final variable name after the in clause.