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"
Related
I have two columns A and B
A is a normal column. Let's say it may have any of the following colors: black, white, orange
B each B record contain a list. Let's say "white shirt", "white trousers", "orange t-shirt"
I'm trying to get in column B the items related to the color in column A.
If A = white, then I want in one cell "white shirt" and "white trousers".
If I hard code "white", it works, but I can't pass [A] to Text.Contains (or I don't know how)
= Table.TransformColumns(#"Added Custom", {"B", each Text.Combine(List.Transform(List.Select(_,each Text.Contains(_,"white")), Text.From), "#(lf)"), type text})
Please, I appreciate any help.
The trick is to set value of the first column equal to a variable before starting to work with the list in the second column
so for your example
= Table.TransformColumns(#"Added Custom", {"B", each let search = [ColumnA] in Text.Combine(List.Transform(List.Select(_,each Text.Contains(_,search)), Text.From), "#(lf)"), type text})
but in a more general example:
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Column2", type text}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each
let search = [Column1] in
Text.Combine(
List.Select(
Text.Split([Column2],","),
each Text.Contains(_,search)
)
," ,")
)
in #"Added Custom"
I'm calculating QoQ Imp and QoQ %Eng in the below data table which is grouped by with the help of power query by adding "Index starting from 0" and "Index.1 starting from 1".
I have a "filter" column in the Filters pane this visual. Please help me in calculating QoQ Imp and QoQ %Eng in the above Table A. The expected result/output should look like this below table:-
In Power Query (M Code), assuming your data is representative, you can compute the QoQ values before filtering/grouping etc.
Add your Index column as you show
Then add a Modulo column (value = 4, since you have four quarters)
Then add custom columns for your two calculations:
Then filter as needed
Here is the M Code assuming the data source is an Excel Table. Modify the Source line as needed
let
Source = Excel.CurrentWorkbook(){[Name="Table7"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Qtr", type text}, {"Filter", type text}, {"Imp", Int64.Type}, {"Eng", Int64.Type}}),
//Compute % Eng column
#"Added Custom" = Table.AddColumn(#"Changed Type", "% Eng", each [Eng]/[Imp], Percentage.Type),
//Add Index and Modulo columns
#"Added Index" = Table.AddIndexColumn(#"Added Custom", "Index", 0, 1, Int64.Type),
#"Inserted Modulo" = Table.AddColumn(#"Added Index", "Modulo", each Number.Mod([Index], 4), type number),
//Compute QOQs -- (current row - previous row)/previous row (unless first row in the group in which case => null
#"Added Custom1" = Table.AddColumn(#"Inserted Modulo", "QoQ Imp", each if [Modulo]=0 then null
else ([Imp] - #"Inserted Modulo"[Imp]{[Index]-1}) / #"Inserted Modulo"[Imp]{[Index]-1}),
#"Added Custom2" = Table.AddColumn(#"Added Custom1", "QoQ %Eng", each if [Modulo]=0 then null
else ([#"% Eng"] - #"Inserted Modulo"[#"% Eng"]{[Index]-1}) / #"Inserted Modulo"[#"% Eng"]{[Index]-1}),
//remove now superfluous Index and Modulo columns and re-order the others
#"Removed Columns" = Table.RemoveColumns(#"Added Custom2",{"Index", "Modulo"}),
#"Reordered Columns" = Table.ReorderColumns(#"Removed Columns",{"Qtr", "Filter", "Imp", "QoQ Imp", "Eng", "% Eng", "QoQ %Eng"})
in
#"Reordered Columns"
If i have a code like this
reference = List.Buffer(#"table1"[Keywords]),
Custom1 = #"Renamed Columns3",
#"Added Custom1" = Table.AddColumn(Custom1, "Custom", each [ccenter] & " " & [vnumber] & " " & [Line Description]),
#"Added Custom" = Table.AddColumn(#"Added Custom1", "Taxonomy Reference", each List.Select(
reference,
(Taxonomy_Reference)=> Text.Contains([Custom],Taxonomy_Reference))),
#"Expanded Vendors" = Table.ExpandListColumn(#"Added Custom", "Taxonomy Reference"),
What this code is doing is from table1, it's making a list out of a column and then in added custom - its doing a text.contain comparison between keywords column of table 1 with the custom column of table 2. This is working fine - however i want to refine it further by doing a comparison between vnumber and ccenter (common in both tables) first and only if ccenter and vnumber are same then do a keyword and line description comparison for those vnumbers and ccenters.
The scenario and data structure is very simple.
I have a list with the product code and the month that this product have been retailed. A example of such data can be seen at the first two columns in green at the image below.
Then I need to check for each product If it was retailed also on the last month, on the last 3 months or in the last 12 months. The result would be the next three columns in yellow on the image.
These calculations (yellow columns) are easy to be computed at Excel by using some IF and COUNTIFS formulas, but when migrating it to Power BI I'm struggling with the performance of my code at Power Query. As there are thousands of products for each month, the Power Query calculation is taking too long.
Check below the code I've designed. The code snapshot would be for the second yellow column, to advise whether there was a retail on the last 3 months or not of that product.
In essence what I'm doing is adding a calculated column that is counting the rows of a table that is being filtered with the product code information and relevant date.
What would be a better approach in terms of performance to get the information I need?
Thank you.
Code:
// Add a calculated column.
AdicionarHits03Meses = Table.AddColumn(
AdicionarHits01Mes,
"Hit nos últimos 3 meses?",
(r)=>
// Check if...
if
// Returns the rows count of a table.
Table.RowCount(
// Returns a table with the condition.
Table.SelectRows(
ChangeType,
// Condition:
(q)=>
// Same Product Code.
q[#"Product Code"] = r[#"Product Code"]
and
// Check the retail month.
q[#"Retail month"] <= Date.AddMonths(r[#"Retail month"], -1) and
q[#"Retail month"] >= Date.AddMonths(r[#"Retail month"], -3)
)
)
= 0 then
// No retail found.
0 else
// Retail found.
1
,
Int64.Type
)
You can likely improve performance with some clever self-merges.
See if this makes sense to you:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUfJKzFMwMjAyVIrViVYyQhcwRhcAaXFLTUIV8E0sQjXDN7ESzdDSHKhALAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Product Code" = _t, #"Retail Month" = _t]),
Original = Table.TransformColumnTypes(Source,{{"Product Code", Int64.Type}, {"Retail Month", type date}}),
#"Added Offset Lists" = Table.AddColumn(Original, "Offset", each {1..12}),
#"Expanded Offset Column" = Table.ExpandListColumn(#"Added Offset Lists", "Offset"),
#"Added Prev Column" = Table.AddColumn(#"Expanded Offset Column", "Prev", each Date.AddMonths([Retail Month], -[Offset] ), type date),
#"Inner Join Prev to Original" = Table.NestedJoin(#"Added Prev Column", {"Product Code", "Prev"}, Original, {"Product Code", "Retail Month"}, "Retail", JoinKind.Inner),
#"Merge Original to Prev" = Table.NestedJoin(Original, {"Product Code", "Retail Month"}, #"Inner Join Prev to Original", {"Product Code", "Retail Month"}, "Min Offset", JoinKind.LeftOuter),
#"Expanded Min Offset" = Table.TransformColumns(#"Merge Original to Prev", {{"Min Offset", each List.Min([Offset]), Int64.Type}}),
#"Added Last Month" = Table.AddColumn(#"Expanded Min Offset", "Retail last month", each if [Min Offset] = 1 then "Yes" else "No", type text),
#"Added Last 3 Months" = Table.AddColumn(#"Added Last Month", "Retailed since last 3 months", each if [Min Offset] <> null and [Min Offset] <= 3 then "Yes" else "No", type text),
#"Added Last 12 Months" = Table.AddColumn(#"Added Last 3 Months", "Retailed since 12 months", each if [Min Offset] <> null and [Min Offset] <= 12 then "Yes" else "No", type text)
in
#"Added Last 12 Months"
I don't have time to fully explain it but the outline is roughly as follows:
Expand each row to 12 rows of prior months.
Match up those prior months with rows from the original table.
Join the original rows with all of the matches found for that row.
Find the most recent match (minimal offset) for each matching set.
Define the 1, 3, and 12-month lookback columns using this offset.
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